עבודה עם לוחות שנה של Gmail

הוספה, עריכה ומחיקת לוח שנה

Aspose.Email מאפשרת ליישומים לנהל את לוחות השנה של Gmail באמצעות IGmailClient המאפשרת הוספה, מחיקה ועדכון של לוחות שנה של Gmail. מחלקת הלקוח מחזירה רשימת אובייקטים מסוג ExtendedCalendar הכוללים מידע על פריטי לוח השנה של Gmail. IGmailClient המחלקה מציגה את הפונקציות הבאות עבור לוחות השנה:

קבל רשימת כל לוחות השנה של לקוח

  • deleteCalendar ניתן להשתמש בה כדי למחוק לוח שנה
  • fetchCalendar ניתן להשתמש בה כדי לקבל לוח שנה ספציפי של לקוח
  • updateCalendar פונקציה זו משמשת להחזרת לוח שנה משוכתב של לקוח.

כדי לגשת ללוחות השנה, GoogleTestUser מאתחל באמצעות פרטי חשבון Gmail. GoogleOAuthHelper משמש לקבלת אסימון גישה עבור המשתמש המשמש לאחר מכן לאתחול IGmailClient.

הוספה, קבלה ועדכון

להוספת לוח שנה, אתחול Calendar אובייקט מסוג והכנס אותו באמצעות createCalendar פונקציה. createCalendar מחזירה את המזהה של לוח השנה החדש שנוסף. מזהה זה יכול לשמש לקבלת לוח השנה מהשרת. קטע הקוד הבא מציג כיצד להוסיף, לקבל ולעדכן לוח שנה.

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. סדר השדה תאריך
  2. הצג את כל אזורי הזמן
  3. הסתר הזמנות
  4. פורמט זמן 24 שעות
  5. מצב לוח שנה ברירת מחדל
  6. אורך אירוע ברירת מחדל
  7. שפה
  8. remindOnRespondedEventsOnly
  9. לוח שנה חלופי
  10. מיקום משתמש
  11. הסתר סופי שבוע
  12. showDeclinedEvents
  13. התחלת שבוע
  14. מזג אוויר
  15. customCalendarMode
  16. timezoneLabel
  17. אזור זמן
  18. useKeyboardShortcuts
  19. מדינה

באופן דומה ניתן לקבל מידע צבע עבור לקוחות באמצעות 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" ופגישה עם מזהה ייחודי "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 ניתן להעביר על ידי מתן לוח השנה המקורי, לוח היעד והמזהה הייחודי של הפגישה בלוח המקורי. קטע הקוד הבא מציג כיצד להעביר ולמחוק פגישה.

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