การจัดการรายการปฏิทินในไฟล์ PST

เพิ่มเหตุการณ์ปฏิทินไปยังไฟล์ PST

สร้างไฟล์ PST ใหม่และเพิ่มโฟลเดอร์ย่อย แสดงวิธีสร้างไฟล์ PST และรวมโฟลเดอร์ย่อยภายใน ด้วย Aspose.Email คุณยังสามารถเพิ่ม MapiCalendar ลงในโฟลเดอร์ย่อย Calendar ของไฟล์ PST ที่มีอยู่หรือสร้างใหม่ ด้านล่างเป็นขั้นตอนการเพิ่ม MapiCalendar ลงในไฟล์ PST:

  1. สร้าง MapiCalendar อ็อบเจ็กต์.
  2. ตั้งค่าคุณสมบัติของ MapiCalendar โดยใช้คอนสตรัคเตอร์และเมธอดต่างๆ.
  3. สร้าง PST ด้วย PersonalStorage.create() เมธอด.
  4. สร้างโฟลเดอร์ที่กำหนดไว้ล่วงหน้า (Calendar) ที่รากของไฟล์ PST โดยเข้าถึงโฟลเดอร์รากแล้วเรียกเมธอด add_mapi_message_item()

โค้ดตัวอย่างต่อไปนี้แสดงวิธีสร้าง MapiCalendar แล้วเพิ่มลงในโฟลเดอร์ปฏิทินของไฟล์ PST ที่สร้างใหม่:

บันทึกรายการปฏิทิน Outlook เป็นไฟล์ ICS

บทความนี้อธิบายวิธีเข้าถึงรายการปฏิทินจากไฟล์ Outlook PST และบันทึกลงดิสก์ในรูปแบบ ICS คุณจะต้องใช้ PersonalStorage และ MapiCalendar คลาสเพื่อดึงข้อมูลปฏิทิน ทำตามขั้นตอนต่อไปนี้เพื่อบันทึกรายการปฏิทิน:

  1. โหลดไฟล์ PST โดยใช้ PersonalStorage คลาส.
  2. นำทางไปยังโฟลเดอร์ Calendar.
  3. ดึงคอลเลกชันข้อความจากโฟลเดอร์ Calendar.
  4. วนลูปผ่านชุดข้อความ.
  5. ใช้ PersonalStorage.extract_message() เมธอดเพื่อรับข้อมูลรายชื่อใน MapiCalendar คลาส.
  6. ใช้ MapiCalendar.save() เมธอดเพื่อบันทึกแต่ละรายการปฏิทินลงดิสก์ในรูปแบบ ICS.

โปรแกรมด้านล่างโหลดไฟล์ PST จากดิสก์และบันทึกรายการปฏิทินทั้งหมดเป็นรูปแบบ ICS ไฟล์ ICS สามารถใช้ในโปรแกรมอื่นที่สนับสนุนไฟล์ปฏิทินมาตรฐาน ICS เมื่อเปิดใน Microsoft Outlook ไฟล์ ICS จะแสดงตามภาพหน้าจอด้านล่าง.

todo:image_alt_text

โค้ดตัวอย่างต่อไปนี้สาธิตวิธีส่งออกรายการปฏิทินจาก Outlook PST ไปเป็นรูปแบบ ICS:

บันทึกไฟล์ ICS พร้อมแสดงเวลาเดิม

เมธอด keep_original_date_time_stamp ของ MapiCalendarIcsSaveOptions คลาสอนุญาตให้รักษาเวลาประทับวันที่และเวลาต้นฉบับของรายการปฏิทินเมื่อบันทึกเป็นไฟล์ ICS (iCalendar). ตัวอย่างโค้ดต่อไปนี้แสดงการนำเมธอดนี้ไปใช้:

import aspose.email as ae

pst = ae.storage.pst.PersonalStorage.from_file("my.pst")

calendar_folder = pst.get_predefined_folder(ae.storage.pst.StandardIpmFolder.APPOINTMENTS)

for msg_info in calendar_folder.enumerate_messages():
    cal = pst.extract_message(msg_info).to_mapi_message_item()

    save_options = ae.mapi.MapiCalendarIcsSaveOptions()
    save_options.keep_original_date_time_stamp = True

    if not (cal is None):
      cal.save("cal.ics", save_options)

แก้ไขหรือ ลบ การเกิดซ้ำในไฟล์ PST

คุณสามารถเพิ่มข้อยกเว้นให้กับรูปแบบการเกิดซ้ำที่มีอยู่หรือลบเหตุการณ์ที่เฉพาะเจาะจงในไฟล์ PST โดยใช้ Aspose.Email สำหรับ .NET API ตัวอย่างโค้ดต่อไปนี้แสดงวิธีทำการแก้ไขเหล่านั้น:

from datetime import datetime, timedelta
from aspose.email.storage.pst import PersonalStorage, StandardIpmFolder, FileFormatVersion
from aspose.email.mapi import MapiCalendar, MapiCalendarEventRecurrence, \
    MapiCalendarDailyRecurrencePattern, MapiCalendarRecurrenceEndType, \
    MapiCalendarExceptionInfo, MapiCalendarRecurrencePatternType, \
    MapiRecipientCollection, MapiRecipientType

start_date = datetime.now().date()

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

exception_date = start_date + timedelta(days=1)

# adding one exception
exception_info = MapiCalendarExceptionInfo()
exception_info.location = "London"
exception_info.subject = "Subj"
exception_info.original_start_date = exception_date
exception_info.start_date_time = exception_date
exception_info.end_date_time = exception_date + timedelta(hours=5)
pattern.exceptions.append(exception_info)
pattern.modified_instance_dates.append(exception_date)
# every modified instance also has to have an entry in the DeletedInstanceDates field with the original instance date.
pattern.deleted_instance_dates.append(exception_date)

# adding one deleted instance
pattern.deleted_instance_dates.append(exception_date + timedelta(days=2))

rec_coll = MapiRecipientCollection()
rec_coll.add("receiver@domain.com", "receiver", MapiRecipientType.TO)
new_cal = MapiCalendar(
    "This is Location",
    "This is Summary",
    "This is recurrence test",
    start_date,
    start_date + timedelta(hours=3),
    "organizer@domain.com",
    rec_coll
)
new_cal.recurrence = recurrence

with PersonalStorage.create("output.pst", FileFormatVersion.UNICODE) as pst:
    calendar_folder = pst.create_predefined_folder("Calendar", StandardIpmFolder.APPOINTMENTS)
    calendar_folder.add_message(new_cal)