Quản lý Định kỳ

Aspose.Email hỗ trợ tạo các mẫu chu kỳ MAPI cho mục lịch và tác vụ. Một mẫu được gán cho recurrence thuộc tính của một MapiTask hoặc một MapiCalendar đối tượng và được lưu cùng với mục.

Các lớp mẫu sau có sẵn:

| Lớp | Mục đích | |——-|———| | MapiCalendarDailyRecurrencePattern | Các chu kỳ hàng ngày | | MapiCalendarWeeklyRecurrencePattern | Các chu kỳ hàng tuần | | MapiCalendarMonthlyRecurrencePattern | Các chu kỳ hàng tháng vào ngày cố định | | MapiCalendarMonthlyNthRecurrencePattern | Các chu kỳ hàng tháng vào ngày trong tuần thứ n | | MapiCalendarYearlyAndMonthlyRecurrencePattern | Các chu kỳ hàng năm |

Mỗi mẫu hỗ trợ ba kiểu kết thúc do MapiCalendarRecurrenceEndType enumeration:

  • END_AFTER_N_OCCURRENCES – chuỗi kết thúc sau số lần lặp đã cho;
  • END_AFTER_DATE – chuỗi kết thúc vào ngày đã cho;
  • NEVER_END – chuỗi không bao giờ kết thúc.

Làm việc với Lặp lại Hàng ngày

Trong loại chu kỳ này, số lần lặp được đặt cùng với các thông tin khác:

  1. Đặt ngày bắt đầu và ngày đến hạn.
  2. Tạo một MapiTask.
  3. Đặt trạng thái tác vụ thành NOT_ASSIGNED.
  4. Tạo đối tượng chu kỳ hàng ngày và đặt pattern_type, period, end_type và occurrence_count các thuộc tính.
  5. Gán đối tượng chu kỳ cho recurrence thuộc tính của tác vụ.
  6. Lưu mục vào đĩa.

Lặp lại hàng ngày với số lần xuất hiện

import datetime
from aspose.email.mapi import (MapiTask, MapiTaskState, MapiCalendarDailyRecurrencePattern,
                               MapiCalendarRecurrencePatternType, MapiCalendarRecurrenceEndType,
                               TaskSaveFormat)

start_date = datetime.datetime(2025, 7, 16)
due_date = datetime.datetime(2025, 7, 16)
end_by_date = datetime.datetime(2025, 8, 1)

task = MapiTask("This is test task", "Sample Body", start_date, due_date)
task.state = MapiTaskState.NOT_ASSIGNED

# Set the daily recurrence
recurrence = MapiCalendarDailyRecurrencePattern()
recurrence.pattern_type = MapiCalendarRecurrencePatternType.DAY
recurrence.period = 1
recurrence.end_type = MapiCalendarRecurrenceEndType.END_AFTER_N_OCCURRENCES
recurrence.occurrence_count = 5

task.recurrence = recurrence
task.save("Daily_out.msg", TaskSaveFormat.MSG)

Các chu kỳ hàng ngày: Kiểu Kết Thúc END_AFTER_DATE

Tùy chọn "Kết thúc trước" được đạt được bằng cách đặt end_date thuộc tính. The period thuộc tính xác định khoảng thời gian — mỗi ngày, mỗi hai ngày, v.v.

import datetime
from aspose.email.mapi import (MapiCalendarDailyRecurrencePattern, MapiCalendarRecurrencePatternType,
                               MapiCalendarRecurrenceEndType)

end_by_date = datetime.datetime(2025, 8, 1)

# Every day until the end date
recurrence = MapiCalendarDailyRecurrencePattern()
recurrence.pattern_type = MapiCalendarRecurrencePatternType.DAY
recurrence.period = 1
recurrence.end_type = MapiCalendarRecurrenceEndType.END_AFTER_DATE
recurrence.end_date = end_by_date

# Every second day until the end date
recurrence = MapiCalendarDailyRecurrencePattern()
recurrence.pattern_type = MapiCalendarRecurrencePatternType.DAY
recurrence.period = 2
recurrence.end_type = MapiCalendarRecurrenceEndType.END_AFTER_DATE
recurrence.end_date = end_by_date

Các chu kỳ hàng ngày: Kiểu Kết Thúc NEVER_END

from aspose.email.mapi import (MapiCalendarDailyRecurrencePattern, MapiCalendarRecurrencePatternType,
                               MapiCalendarRecurrenceEndType)

recurrence = MapiCalendarDailyRecurrencePattern()
recurrence.pattern_type = MapiCalendarRecurrencePatternType.DAY
recurrence.period = 1
recurrence.end_type = MapiCalendarRecurrenceEndType.NEVER_END

Làm việc với Lặp lại Hàng tuần

Các chu kỳ hàng tuần được tạo bằng MapiCalendarWeeklyRecurrencePattern. Các ngày trong tuần được chọn thông qua day_of_week tài sản sử dụng các giá trị của MapiCalendarDayOfWeek liệt kê cờ, có thể kết hợp với | toán tử.

Định kỳ Hàng tuần: Kiểu Kết thúc END_AFTER_N_OCCURRENCES

import datetime
from aspose.email.mapi import (MapiTask, MapiTaskState, MapiCalendarWeeklyRecurrencePattern,
                               MapiCalendarRecurrencePatternType, MapiCalendarRecurrenceEndType,
                               MapiCalendarDayOfWeek, TaskSaveFormat)

start_date = datetime.datetime(2025, 7, 16)
due_date = datetime.datetime(2025, 7, 16)
end_by_date = datetime.datetime(2025, 9, 1)

task = MapiTask("This is test task", "Sample Body", start_date, due_date)
task.state = MapiTaskState.NOT_ASSIGNED

recurrence = MapiCalendarWeeklyRecurrencePattern()
recurrence.pattern_type = MapiCalendarRecurrencePatternType.WEEK
recurrence.period = 1
recurrence.day_of_week = MapiCalendarDayOfWeek.FRIDAY
recurrence.end_type = MapiCalendarRecurrenceEndType.END_AFTER_N_OCCURRENCES
recurrence.occurrence_count = 7

task.recurrence = recurrence
task.save("Weekly_out.msg", TaskSaveFormat.MSG)

Chọn Nhiều Ngày trong Một Tuần và Đặt Khoảng Thời gian

from aspose.email.mapi import (MapiCalendarWeeklyRecurrencePattern, MapiCalendarRecurrencePatternType,
                               MapiCalendarRecurrenceEndType, MapiCalendarDayOfWeek)

recurrence = MapiCalendarWeeklyRecurrencePattern()
recurrence.pattern_type = MapiCalendarRecurrencePatternType.WEEK
recurrence.period = 2
recurrence.day_of_week = MapiCalendarDayOfWeek.FRIDAY | MapiCalendarDayOfWeek.MONDAY
recurrence.end_type = MapiCalendarRecurrenceEndType.END_AFTER_N_OCCURRENCES
recurrence.occurrence_count = 10

Định kỳ Hàng tuần: Kiểu Kết thúc END_AFTER_DATE và NEVER_END

import datetime
from aspose.email.mapi import (MapiCalendarWeeklyRecurrencePattern, MapiCalendarRecurrencePatternType,
                               MapiCalendarRecurrenceEndType, MapiCalendarDayOfWeek)

end_by_date = datetime.datetime(2025, 9, 1)

# End by a given date
recurrence = MapiCalendarWeeklyRecurrencePattern()
recurrence.pattern_type = MapiCalendarRecurrencePatternType.WEEK
recurrence.period = 1
recurrence.day_of_week = MapiCalendarDayOfWeek.FRIDAY
recurrence.end_type = MapiCalendarRecurrenceEndType.END_AFTER_DATE
recurrence.end_date = end_by_date

# Never end
recurrence = MapiCalendarWeeklyRecurrencePattern()
recurrence.pattern_type = MapiCalendarRecurrencePatternType.WEEK
recurrence.period = 1
recurrence.day_of_week = MapiCalendarDayOfWeek.FRIDAY
recurrence.end_type = MapiCalendarRecurrenceEndType.NEVER_END

Làm việc với Lặp lại Hàng tháng

Một định kỳ hàng tháng vào ngày cố định của tháng được tạo bằng MapiCalendarMonthlyRecurrencePattern và day thuộc tính:

import datetime
from aspose.email.mapi import (MapiTask, MapiCalendarMonthlyRecurrencePattern,
                               MapiCalendarRecurrencePatternType, MapiCalendarRecurrenceEndType,
                               TaskSaveFormat)

start_date = datetime.datetime(2025, 7, 16)
due_date = datetime.datetime(2025, 7, 16)

task = MapiTask("This is test task", "Sample Body", start_date, due_date)

recurrence = MapiCalendarMonthlyRecurrencePattern()
recurrence.pattern_type = MapiCalendarRecurrencePatternType.MONTH
recurrence.period = 1
recurrence.day = 16
recurrence.end_type = MapiCalendarRecurrenceEndType.NEVER_END

task.recurrence = recurrence
task.save("Monthly_out.msg", TaskSaveFormat.MSG)

Để lặp lại vào ngày trong tuần thứ n của tháng — ví dụ thứ Ba thứ hai — sử dụng MapiCalendarMonthlyNthRecurrencePattern với day_of_week và position các thuộc tính. Vị trí là một giá trị của DayPosition enumeration — FIRST, SECOND, THIRD, FOURTH hoặc LAST:

from aspose.email.mapi import (MapiCalendarMonthlyNthRecurrencePattern, MapiCalendarRecurrencePatternType,
                               MapiCalendarRecurrenceEndType, MapiCalendarDayOfWeek)
from aspose.email.calendar.recurrences import DayPosition

recurrence = MapiCalendarMonthlyNthRecurrencePattern()
recurrence.pattern_type = MapiCalendarRecurrencePatternType.MONTH_NTH
recurrence.period = 1
recurrence.day_of_week = MapiCalendarDayOfWeek.TUESDAY
recurrence.position = DayPosition.SECOND
recurrence.end_type = MapiCalendarRecurrenceEndType.NEVER_END

Làm việc với Lặp lại Hàng năm

Các định kỳ hàng năm được tạo bằng MapiCalendarYearlyAndMonthlyRecurrencePattern. Một khoảng thời gian hàng năm được biểu thị là 12 tháng:

import datetime
from aspose.email.mapi import (MapiTask, MapiCalendarYearlyAndMonthlyRecurrencePattern,
                               MapiCalendarRecurrencePatternType, MapiCalendarRecurrenceEndType,
                               TaskSaveFormat)

start_date = datetime.datetime(2025, 7, 16)
due_date = datetime.datetime(2025, 7, 16)

task = MapiTask("This is test task", "Sample Body", start_date, due_date)

recurrence = MapiCalendarYearlyAndMonthlyRecurrencePattern()
recurrence.pattern_type = MapiCalendarRecurrencePatternType.MONTH
recurrence.period = 12
recurrence.day = 16
recurrence.end_type = MapiCalendarRecurrenceEndType.NEVER_END

task.recurrence = recurrence
task.save("Yearly_out.msg", TaskSaveFormat.MSG)

Xem Thêm