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 다음 함수들을 포함하여 접근 제어 규칙을 관리합니다.

다음 코드 스니펫은 접근 규칙을 관리하는 데 사용되는 함수를 보여줍니다:

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. dateFieldOrder
  2. displayAllTimezones
  3. hideInvitations
  4. format24HourTime
  5. defaultCalendarMode
  6. defaultEventLength
  7. locale
  8. remindOnRespondedEventsOnly
  9. alternateCalendar
  10. userLocation
  11. hideWeekends
  12. showDeclinedEvents
  13. weekStart
  14. weather
  15. customCalendarMode
  16. timezoneLabel
  17. timezone
  18. useKeyboardShortcuts
  19. country

클라이언트의 색상 정보도 다음을 사용하여 검색할 수 있습니다 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. 캘린더에서 약속을 업데이트합니다.

캘린더 ID "calendarId"와 약속 고유 ID "AppointmentUniqueId"가 이미 추출된 것으로 가정합니다. 다음 코드 스니펫은 약속을 검색하고 업데이트하는 방법을 보여줍니다.

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);
}