Gmail クライアントを使用して Google カレンダーを管理する

Gmail カレンダーの追加、編集、削除

Aspose.Email は、次を使用してアプリケーションが Gmail カレンダーを管理できるようにします IGmailClient このクラスは Gmail カレンダーの追加、削除、更新などの機能を提供します。クライアントクラスは、Gmail カレンダー項目の情報を含む ExtendedCalendar 型オブジェクトのリストを返します。 IGmailClient クラスはカレンダー向けに以下の機能を提供します:

  • CreateCalendar 新しいカレンダーを挿入するために使用できます。
  • ListCalendars クライアントのすべてのカレンダーリストを取得するために使用できます。
  • DeleteCalendar カレンダーを削除するために使用できます
  • FetchCalendar クライアントの特定のカレンダーを取得するために使用できます
  • UpdateCalendar この関数は、クライアントの変更済みカレンダーを再挿入するために使用されます。

カレンダーにアクセスするには、gmail アカウントの認証情報で GoogleTestUser を初期化します。GoogleOAuthHelper を使用してユーザーのアクセストークンを取得し、それを用いて IGmailClient を初期化します。

Gmail カレンダーの挿入、取得、更新

カレンダーを挿入するには、Calendar 型オブジェクトを初期化し、次を使用して挿入します。 CreateCalendar() 関数です。 CreateCalendar() 新しく挿入されたカレンダーの ID を返します。この ID はサーバーからカレンダーを取得するために使用できます。以下のコードスニペットは、カレンダーの挿入、取得、更新方法を示しています。

特定の Google カレンダーを削除

特定のカレンダーを削除するには、クライアントのすべてのカレンダーのリストを取得し、必要に応じて削除します。 ListCalendars() 次のリストを返します ExtendedCalendar Gmail カレンダーが含まれています。以下のコードスニペットは、特定のカレンダーを削除する方法を示しています。

カレンダーアクセス制御

Aspose.Email はカレンダー項目へのアクセスを完全に制御します。 ListAccessRules() 関数は以下によって公開されています IGmailClient それはリストを返します。 AccessControlRule個々のルール情報は取得、変更、そしてクライアントのカレンダーに対して保存できます。 IGmailClient アクセス制御ルールを管理するための以下の関数が含まれています。

  • ListAccessRules この関数は AccessControlRule のリストを提供します。
  • CreateAccessRule この関数はカレンダー用の新しいアクセスルールを作成します。
  • UpdateAccessRule この関数はアクセスルールを更新するために使用されます。
  • FetchAccessRule クライアントのカレンダーに対する特定のアクセスルールを取得するために使用できます
  • DeleteAccessRule この関数はアクセスルールを削除するために使用されます。

以下のコードスニペットは、アクセスルール管理のための関数の使用方法を示しています。

カレンダー クライアント設定とカラー情報

Aspose.Email は、次を使用してクライアント設定にアクセスすることをサポートします IGmailClient.GetSettings(). 以下のように設定のリストを返します。

  1. 日付フィールドの順序
  2. すべてのタイムゾーンを表示
  3. 招待を非表示
  4. 24時間制で表示
  5. デフォルトカレンダーモード
  6. デフォルトのイベント長さ
  7. ロケール
  8. 回答済みイベントのみリマインド
  9. 代替カレンダー
  10. ユーザーの位置情報
  11. 週末を非表示
  12. 予定の辞退を表示
  13. 週の開始日
  14. 天気
  15. カスタムカレンダーモード
  16. タイムゾーンラベル
  17. タイムゾーン
  18. useKeyboardShortcuts

同様に、クライアントのカラー情報は次を使用して取得できます IGmailClient.GetColors()このカラー情報オブジェクトは、前景色、背景色、および更新日時のリストを返します。

クライアント設定へのアクセス

以下のコードスニペットは、クライアント設定にアクセスするための関数の使用例を示しています。

カラー情報へのアクセス

以下のコードスニペットは、クライアントのカラー設定にアクセスする関数の使用例を示しています。

Google カレンダーの予約を管理する

Aspose.Email は Google カレンダーの予約を操作する機能を提供します。以下のタスクを Google カレンダーの予約で実行できます:

  1. 予約を追加する。
  2. 予約のリストを取得する。
  3. 特定の予約を取得する。
  4. 予定を更新する。
  5. 予約をあるカレンダーから別のカレンダーへ移動する。
  6. 予約を削除する。

IGmailClient 以下のような機能を提供します: CreateAppointment, FetchAppointment, UpdateAppointment, ListAppointments, MoveAppointment および DeleteAppointment.

Google カレンダーへの予約追加

以下のコードサンプルは、カレンダーに予約を追加する機能を示します。実行するには次の手順に従ってください。

  1. カレンダーを作成して挿入する。
  2. 新しいカレンダーから予約リストを取得する。
  3. 予定を作成する。
  4. 予約を挿入する。

Google カレンダーの予約を取得および更新する

ここでは、カレンダーの取得と更新を次のように示します。

  1. 特定の予定を取得する。
  2. 予定を変更する。
  3. カレンダー内の予定を更新する。

カレンダー ID "calendarId" と予約一意 ID "AppointmentUniqueId" が既に取得されているものとします。以下のコードスニペットは、予約を取得および更新する方法を示しています。

Google カレンダーで予約を移動および削除する

予約は、ソースカレンダー、宛先カレンダー、およびソースカレンダー内の予約の一意 ID を指定することで移動できます。以下のコードスニペットは、予約を移動および削除する方法を示しています。

Google カレンダーの FreeBusy クエリ

Aspose.Email は、条件に基づいて予約が期限切れかどうかをチェックするクエリ機構を提供します。この目的のために FreebusyQuery クラスが用意されており、特定のカレンダーに対するクエリを作成できます。

このコードサンプルは、カレンダーのクエリ機能を示しています。サンプルでは以下のタスクを実行します:

  1. カレンダーを作成して挿入する
  2. 予定を作成する
  3. 予定を挿入する
  4. FreeBusyQuery を準備する
  5. FreebusyResponse を取得する
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET

// Use the GoogleUser and GoogleOAuthHelper classes below to receive an access token
using (IGmailClient client = GmailClient.GetInstance(accessToken, user.Email))
{
    // Initialize calendar item
    Aspose.Email.Clients.Google.Calendar calendar1 = new Aspose.Email.Clients.Google.Calendar("summary - " + Guid.NewGuid().ToString(), null, null, "Europe/Kiev");

    // Insert calendar and get back id of newly inserted calendar and Fetch the same calendar using calendar id
    string id = client.CreateCalendar(calendar1);
    Aspose.Email.Clients.Google.Calendar cal1 = client.FetchCalendar(id);
    string calendarId1 = cal1.Id;
    try
    {
        // Get list of appointments in newly inserted calendar. It should be zero
        Appointment[] appointments = client.ListAppointments(calendarId1);
        if (appointments.Length != 0)
        {
            Console.WriteLine("Wrong number of appointments");
            return;
        }

        // Create a new appointment and Calculate appointment start and finish time
        DateTime startDate = DateTime.Now;
        DateTime endDate = startDate.AddHours(1);

        // Create attendees list for appointment
        MailAddressCollection attendees = new MailAddressCollection();
        attendees.Add("user1@domain.com");
        attendees.Add("user2@domain.com");

        // Create appointment
        Appointment app1 = new Appointment("Location - " + Guid.NewGuid().ToString(), startDate, endDate, "user2@domain.com", attendees);
        app1.Summary = "Summary - " + Guid.NewGuid().ToString();
        app1.Description = "Description - " + Guid.NewGuid().ToString();
        app1.StartTimeZone = "Europe/Kiev";
        app1.EndTimeZone = "Europe/Kiev";

        // Insert the newly created appointment and get back the same in case of successful insertion
        Appointment app2 = client.CreateAppointment(calendarId1, app1);

        // Create Freebusy query by setting min/max timeand time zone
        FreebusyQuery query = new FreebusyQuery();
        query.TimeMin = DateTime.Now.AddDays(-1);
        query.TimeMax = DateTime.Now.AddDays(1);
        query.TimeZone = "Europe/Kiev";

        // Set calendar item to search and Get the reponse of query containing 
        query.Items.Add(cal1.Id);
        FreebusyResponse resp = client.GetFreebusyInfo(query);
        // Delete the appointment
        client.DeleteAppointment(calendarId1, app2.UniqueId);
    }
    finally
    {
        // Delete the calendar
        client.DeleteCalendar(cal1.Id);
    }
}