PST ファイル内のカレンダー アイテムの管理
カレンダーイベントを PST ファイルに追加
新規 PST ファイルの作成とサブフォルダーの追加 PST ファイルを作成しサブフォルダーを含める方法を示しました。Aspose.Email を使用すると、既存または新規作成した PST ファイルの Calendar サブフォルダーに MapiCalendar を追加することもできます。以下は PST ファイルに MapiCalendar を追加する手順です。
- 作成する MapiCalendar オブジェクト。
- コンストラクタとメソッドを使用して MapiCalendar のプロパティを設定します。
- 次を使用して PST を作成します PersonalStorage.create() メソッド。
- ルートフォルダーにアクセスし、add_mapi_message_item() メソッドを呼び出すことで、PST ファイルのルートに事前定義フォルダー(Calendar)を作成します。
以下のコードスニペットは、MapiCalendar を作成し、新規作成した PST ファイルのカレンダーフォルダーに追加する方法を示しています。
Outlook カレンダー項目を ICS ファイルとして保存
この記事では、Outlook PST ファイルからカレンダー項目にアクセスし、ICS 形式でディスクに保存する方法を説明します。次を使用する必要があります PersonalStorage および MapiCalendar クラスを使用してカレンダー データを取得します。以下の手順でカレンダー項目を保存してください。
- 次を使用して PST ファイルをロードする PersonalStorage クラス。
- カレンダーフォルダーへ移動する。
- カレンダーフォルダーからメッセージコレクションを取得する。
- メッセージ コレクションをループ処理します。
- 使用する PersonalStorage.extract_message() 連絡先情報を取得するメソッド MapiCalendar クラス。
- 使用する MapiCalendar.save() 各カレンダー項目をICS形式でディスクに保存するメソッド。
以下のプログラムはディスクから PST ファイルをロードし、すべてのカレンダー項目を ICS 形式で保存します。これらの ICS ファイルは、標準の ICS カレンダーファイルをサポートする任意のプログラムで使用できます。Microsoft Outlook で開くと、以下のスクリーンショットのように表示されます。
![]() |
|---|
以下のコードスニペットは、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 ファイルの繰り返し発生の変更または削除
Aspose.Email for .NET API を使用して、既存の繰り返しパターンに例外を追加したり、PST ファイル内の特定の発生を削除したりできます。以下のコードサンプルは、そのような変更を行う方法を示しています。
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)
