Gmail カレンダーの操作

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

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

クライアントのすべてのカレンダーの一覧を取得

  • deleteCalendar カレンダーを削除するために使用できます
  • fetchCalendar クライアントの特定のカレンダーを取得するために使用できます
  • updateCalendar この関数は、クライアントの修正されたカレンダーを再挿入するために使用されます

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

挿入、取得、更新

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

try (IGmailClient client = GmailClient.getInstance(accessToken, email)) {
    // Insert, get and update calendar
    Calendar calendar = new Calendar("Summary", "Description", "Location", "America/Los_Angeles");

    // Insert calendar and Retrieve same calendar using id
    String id = client.createCalendar(calendar);
    Calendar cal = client.fetchCalendar(id);

    // Change information in the fetched calendar and Update calendar
    cal.setDescription("New Description");
    cal.setLocation("New Location");
    client.updateCalendar(cal);
}

特定のカレンダーを削除

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

try (IGmailClient client = GmailClient.getInstance(accessToken, email)) {
    // Access and delete calendar with summary starting from "Calendar summary"
    String summary = "Calendar summary";

    // Get calendars list
    ExtendedCalendar[] lst = client.listCalendars();

    for (ExtendedCalendar extCal : lst) {
        // Delete selected calendars
        if (extCal.getSummary().startsWith(summary))
            client.deleteCalendar(extCal.getId());
    }
}

カレンダーアクセスコントロールの操作

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

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

以下のコードスニペットは、アクセスルールの管理に使用される関数の使い方を示しています:

try (IGmailClient client = GmailClient.getInstance(accessToken, email)) {
    // Retrieve list of calendars for the current client
    ExtendedCalendar[] calendarList = client.listCalendars();

    // Get first calendar id and retrieve list of AccessControlRule for the first calendar
    String calendarId = calendarList[0].getId();
    AccessControlRule[] roles1 = client.listAccessRules(calendarId);

    // Create a local access control rule and Set rule properties
    AccessControlRule rule = new AccessControlRule();
    rule.setRole(AccessRole.reader);
    rule.setScope(new AclScope(AclScopeType.user, email2));

    // Insert new rule for the calendar. It returns the newly created rule
    AccessControlRule createdRule = client.createAccessRule(calendarId, rule);

    // Get list of rules
    AccessControlRule[] roles2 = client.listAccessRules(calendarId);

    // Current list length should be 1 more than the earlier one
    if (roles1.length + 1 == roles2.length) {
        System.out.println("List lengths are ok");
    } else {
        System.out.println("List lengths are not ok");
        return;
    }

    // Change rule and Update the rule for the selected calendar
    createdRule.setRole(AccessRole.writer);
    AccessControlRule updatedRule = client.updateAccessRule(calendarId, createdRule);

    // Retrieve individaul rule against a calendar
    AccessControlRule fetchedRule = client.fetchAccessRule(calendarId, createdRule.getId());

    // Delete particular rule against a given calendar and Retrieve the all rules list for the same calendar
    client.deleteAccessRule(calendarId, createdRule.getId());
    AccessControlRule[] roles3 = client.listAccessRules(calendarId);

    // Check that current rules list length should be equal to the original list length before adding and deleting the rule
    if (roles1.length == roles3.length) {
        System.out.println("List lengths are same");
    } else {
        System.out.println("List lengths are not equal");
        return;
    }
}

クライアント設定とカラー情報の操作

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このカラー情報オブジェクトは、前景色、背景色、および更新日時のリストを返します。

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

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

try (IGmailClient client = GmailClient.getInstance(accessToken, email)) {
    // Retrieve client settings
    Dictionary<String, String> settings = client.getSettings();
    if (settings.size() < 1) {
        System.out.println("No settings are available.");
        return;
    }

    // Traverse the settings list
    for (KeyValuePair<String, String> pair : settings) {
        // Get the setting value and test if settings are ok
        String value = client.getSetting(pair.getKey());
        if (pair.getValue().equals(value)) {
            System.out.println("Key = " + pair.getKey() + ", Value = " + pair.getValue());
        } else {
            System.out.println("Settings could not be retrieved");
        }
    }
}

カラー情報へのアクセス

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

try (IGmailClient client = GmailClient.getInstance(accessToken, email)) {
    ColorsInfo colors = client.getColors();
    Dictionary<String, Colors> palettes = colors.getCalendar();

    // Traverse the settings list
    for (KeyValuePair<String, Colors> pair : palettes) {
        System.out.println("Key = " + pair.getKey() + ", Color = " + pair.getValue());
    }
    System.out.println("Update Date = " + colors.getUpdated());
}

予定の操作

Aspose.Email は以下の機能を提供します: 予定 Google カレンダー内で。以下は、Google カレンダーの予定に対して実行できるタスクの一覧です:

  1. 予定を追加 - createAppointment, importAppointment
  2. 予定の一覧を取得 - listAppointments
  3. 特定の予定を取得 - fetchAppointment, listAppointmentInstances
  4. 予定を更新 - updateAppointment
  5. 別のカレンダーへ予定を移動 - moveAppointment
  6. 予定を削除 - deleteAppointment

予定の追加

以下のコードサンプルは、カレンダーに予定を追加する機能を示しています。このサンプルでは以下の手順が実行されます。

  1. カレンダーを作成して挿入する。
  2. 新しいカレンダーから予定のリストを取得する。
  3. 予定を作成する。
  4. 予定を挿入する。
try (IGmailClient client = GmailClient.getInstance(accessToken, email)) {
    // Create local calendar
    Calendar calendar1 = new Calendar("Summary", null, null, "Europe/Kiev");

    // Insert calendar and get id of inserted calendar and Get back calendar using an id
    String id = client.createCalendar(calendar1);
    Calendar cal1 = client.fetchCalendar(id);
    String calendarId1 = cal1.getId();

    try {
        // Retrieve list of appointments from the first calendar
        Appointment[] appointments = client.listAppointments(calendarId1);
        if (appointments.length > 0) {
            System.out.println("Wrong number of appointments");
            return;
        }

        // Get current time and Calculate time after an hour from now
        java.util.Calendar c = java.util.Calendar.getInstance();
        Date startDate = c.getTime();
        c.add(java.util.Calendar.HOUR_OF_DAY, 1);
        Date endDate = c.getTime();

        // Initialize a mail address collection and set attendees mail address
        MailAddressCollection attendees = new MailAddressCollection();
        attendees.add("User1.EMail@domain.com");
        attendees.add("User3.EMail@domain.com");

        // Create an appointment with above attendees
        Appointment app1 = new Appointment("Location", startDate, endDate, MailAddress.to_MailAddress(email2), attendees);

        // Set appointment summary, description, start/end time zone
        app1.setSummary("New Summary");
        app1.setDescription("New Description");
        app1.setStartTimeZone("Europe/Kiev");
        app1.setEndTimeZone("Europe/Kiev");

        // Insert appointment in the first calendar inserted above and get back inserted appointment
        Appointment app2 = client.createAppointment(calendarId1, app1);

        // Retrieve appointment using unique id
        Appointment app3 = client.fetchAppointment(calendarId1, app2.getUniqueId());
    } catch (Exception ex) {
        System.err.println(ex);
    }
    
}

予定の取得と更新

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

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

"calendarId" という ID のカレンダーと "AppointmentUniqueId" というユニーク ID の予定がすでに取得されていることを前提とします。以下のコード スニペットは、予定の取得と更新方法を示します。

try (IGmailClient client = GmailClient.getInstance(accessToken, email)) {
    String calendarId = client.listCalendars()[0].getId();
    String AppointmentUniqueId = client.listAppointments(calendarId)[0].getUniqueId();

    // Retrieve Appointment
    Appointment app3 = client.fetchAppointment(calendarId, AppointmentUniqueId);
    // Change the appointment information
    app3.setSummary("New Summary");
    app3.setDescription("New Description");
    app3.setLocation("New Location");
    app3.setFlags(AppointmentFlags.AllDayEvent);
    java.util.Calendar c = java.util.Calendar.getInstance();
    c.add(java.util.Calendar.HOUR_OF_DAY, 2);
    app3.setStartDate(c.getTime());
    c.add(java.util.Calendar.HOUR_OF_DAY, 1);
    app3.setEndDate(c.getTime());
    app3.setStartTimeZone("Europe/Kiev");
    app3.setEndTimeZone("Europe/Kiev");
    // Update the appointment and get back updated appointment
    Appointment app4 = client.updateAppointment(calendarId, app3);
}

予定の移動と削除

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

try (IGmailClient client = GmailClient.getInstance(accessToken, email)) {
    String SourceCalendarId = client.listCalendars()[0].getId();
    String DestinationCalendarId = client.listCalendars()[1].getId();
    String TargetAppUniqueId = client.listAppointments(SourceCalendarId)[0].getUniqueId();

    // Retrieve the list of appointments in the destination calendar before moving the appointment
    Appointment[] appointments = client.listAppointments(DestinationCalendarId);
    System.out.println("Before moving count = " + appointments.length);
    Appointment Movedapp = client.moveAppointment(SourceCalendarId, DestinationCalendarId, TargetAppUniqueId);

    // Retrieve the list of appointments in the destination calendar after moving the appointment
    appointments = client.listAppointments(DestinationCalendarId);
    System.out.println("After moving count = " + appointments.length);

    // Delete particular appointment from a calendar using unique id
    client.deleteAppointment(DestinationCalendarId, Movedapp.getUniqueId());

    // Retrieve the list of appointments. It should be one less than the earlier appointments in the destination calendar
    appointments = client.listAppointments(DestinationCalendarId);
    System.out.println("After deleting count = " + appointments.length);
}