Praca z kalendarzami Gmail

Dodawanie, Edycja i Usuwanie Kalendarza

Aspose.Email umożliwia aplikacjom zarządzanie kalendarzami Gmail przy użyciu IGmailClient która zapewnia funkcje takie jak dodawanie, usuwanie i aktualizacja kalendarzy Gmail. Ta klasa klienta zwraca listę obiektów typu ExtendedCalendar, które zawierają informacje o elementach kalendarza Gmail. IGmailClient klasa udostępnia następujące funkcje dla kalendarzy:

Pobierz listę wszystkich kalendarzy klienta

  • deleteCalendar Może być użyta do usunięcia kalendarza
  • fetchCalendar Może być użyta do pobrania konkretnego kalendarza klienta
  • updateCalendar Ta funkcja służy do ponownego wstawienia zmodyfikowanego kalendarza klienta

Aby uzyskać dostęp do kalendarzy, GoogleTestUser jest inicjalizowany przy użyciu danych logowania konta Gmail. GoogleOAuthHelper służy do uzyskania tokenu dostępu dla użytkownika, który jest następnie używany do inicjalizacji IGmailClient.

Wstaw, Pobierz i Zaktualizuj

Aby wstawić kalendarz, zainicjuj Calendar obiekt typu i wstawić go przy użyciu createCalendar funkcja. createCalendar zwraca identyfikator nowo wstawionego kalendarza. Ten identyfikator może być użyty do pobrania kalendarza z serwera. Poniższy fragment kodu pokazuje, jak wstawiać, pobierać i aktualizować kalendarz.

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

Usuń Konkretny Kalendarz

Aby usunąć konkretny kalendarz, musimy pobrać listę wszystkich kalendarzy klienta, a następnie usunąć wybrany. listCalendars zwraca listę ExtendedCalendar który zawiera kalendarze Gmail. Poniższy fragment kodu pokazuje, jak usunąć konkretny kalendarz.

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

Praca z Kontrolą Dostępu do Kalendarza

Aspose.Email zapewnia pełną kontrolę nad dostępem do elementów kalendarza. listAccessRules funkcja jest udostępniona przez IGmailClient który zwraca listę AccessControlRule. Informacje o poszczególnych regułach można pobrać, zmodyfikować i zapisać ponownie dla kalendarza klienta. IGmailClient zawiera następujące funkcje do zarządzania regułami kontroli dostępu.

Poniższy fragment kodu pokazuje, jak używać funkcji do zarządzania regułami dostępu:

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

Praca z Ustawieniami Klienta i Informacjami o Kolorze

Aspose.Email obsługuje dostęp do ustawień Klienta za pomocą IGmailClient.getSettings. Zwraca listę ustawień jak podano poniżej:

  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. pogoda
  15. customCalendarMode
  16. timezoneLabel
  17. strefa czasowa
  18. useKeyboardShortcuts
  19. kraj

Podobnie informacje o kolorach dla klientów można pobrać przy użyciu IGmailClient.getColors. Ten obiekt informacji o kolorze zwraca listę kolorów pierwszego planu, kolorów tła oraz datę i czas aktualizacji.

Dostęp do Ustawień Klienta

Poniższy fragment kodu pokazuje, jak używać funkcji do uzyskiwania ustawień klienta:

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

Dostęp do Informacji o Kolorze

Poniższy fragment kodu pokazuje, jak używać funkcji do uzyskiwania ustawień kolorów klienta.

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

Praca ze Spotkaniami

Aspose.Email oferuje funkcje do pracy z Spotkania w kalendarzach Google. Poniżej znajduje się lista zadań, które można wykonać na spotkaniach w kalendarzu Google:

  1. Dodaj spotkania - createAppointment, importAppointment
  2. Pobierz listę spotkań - listAppointments
  3. Pobierz konkretne spotkanie - fetchAppointment, listAppointmentInstances
  4. Zaktualizuj spotkanie - updateAppointment
  5. Przenieś spotkanie z jednego kalendarza do drugiego - moveAppointment
  6. Usuń spotkanie - deleteAppointment

Dodawanie spotkania

Poniższy przykład kodu demonstruje funkcję dodawania spotkania do kalendarza. W tym przykładzie wykonano następujące kroki:

  1. Utwórz i wstaw kalendarz.
  2. Pobierz listę spotkań z nowego kalendarza.
  3. Utwórz spotkanie.
  4. Wstaw spotkanie.
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);
    }
    
}

Pobieranie i aktualizacja spotkania

Pobieranie i aktualizacja kalendarza jest tutaj przedstawiona w następujący sposób:

  1. Pobierz konkretne spotkanie.
  2. Modyfikuj spotkanie.
  3. Zaktualizuj spotkanie w kalendarzu.

Zakłada się, że kalendarz o identyfikatorze "calendarId" i unikalny identyfikator spotkania "AppointmentUniqueId" zostały już pobrane. Poniższy fragment kodu pokazuje, jak pobrać i zaktualizować spotkanie.

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

Przenoszenie i usuwanie spotkania

Appointment można przenieść, podając kalendarz źródłowy, kalendarz docelowy i unikalny identyfikator spotkania w kalendarzu źródłowym. Poniższy fragment kodu pokazuje, jak przenieść i usunąć spotkanie.

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