จัดการ Google Calendar ไปโดยใช้ Gmail Client

เพิ่ม, แก้ไขและลบ Gmail Calendar

Aspose.Email ให้แอปพลิเคชันจัดการปฏิทิน Gmail โดยใช้ IGmailClient ซึ่งให้คุณลักษณะเช่น การเพิ่ม, การลบ และการอัปเดต Gmail calendar คลาสไคลเอนต์นี้ส่งคืนรายการของอ็อบเจ็กต์ประเภท ExtendedCalendar ที่มีข้อมูลเกี่ยวกับรายการปฏิทิน Gmail. IGmailClient คลาสเปิดเผยฟังก์ชันต่อไปนี้สำหรับปฏิทิน:

  • CreateCalendar สามารถใช้เพื่อแทรกปฏิทินใหม่
  • ListCalendars สามารถใช้เพื่อรับรายการปฏิทินทั้งหมดของลูกค้า
  • DeleteCalendar สามารถใช้เพื่อทำการลบปฏิทิน
  • FetchCalendar สามารถใช้เพื่อดึงปฏิทินเฉพาะของไคลเอนต์
  • UpdateCalendar ฟังก์ชันนี้ใช้เพื่อใส่กลับปฏิทินที่แก้ไขของลูกค้า

เพื่อเข้าถึงปฏิทิน GoogleTestUser จะถูกเริ่มต้นด้วยข้อมูลรับรองบัญชี Gmail GoogleOAuthHelper จะใช้เพื่อรับโทเค็นการเข้าถึงสำหรับผู้ใช้ ซึ่งต่อมาจะใช้เพื่อเริ่มต้น IGmailClient.

แทรก, ดึงและอัปเดต Gmail Calendar

สำหรับการแทรกปฏิทิน ให้เริ่มต้นอ็อบเจ็กต์ประเภท Calendar และแทรกโดยใช้ CreateCalendar() ฟังก์ชัน. CreateCalendar() ส่งคืน id ของปฏิทินที่เพิ่งแทรกใหม่ ID นี้สามารถใช้เพื่อดึงปฏิทินจากเซิร์ฟเวอร์ได้ โค้ดตัวอย่างต่อไปนี้แสดงวิธีการแทรก ดึง และอัปเดตปฏิทิน

ลบ Google Calendar เฉพาะเจาะจง

เพื่อทำการลบปฏิทินเฉพาะ เราต้องดึงรายการปฏิทินทั้งหมดของไคลเอนต์แล้วลบตามที่ต้องการ ListCalendars() ส่งคืนรายการของ ExtendedCalendar ซึ่งมีปฏิทิน Gmail อยู่ โค้ดตัวอย่างต่อไปนี้แสดงวิธีการลบปฏิทินเฉพาะ

การควบคุมการเข้าถึงปฏิทิน

Aspose.Email ให้การควบคุมเต็มที่ต่อการเข้าถึงรายการปฏิทิน. ListAccessRules() ฟังก์ชันนี้เปิดเผยโดย IGmailClient ซึ่งส่งคืนรายการของ AccessControlRule. สามารถดึงข้อมูลกฎแต่ละรายการ, แก้ไขและบันทึกกลับสำหรับปฏิทินของไคลเอนต์ IGmailClient ประกอบด้วยฟังก์ชันต่อไปนี้สำหรับจัดการกฎการควบคุมการเข้าถึง.

  • ListAccessRules ฟังก์ชันนี้ให้รายการของ AccessControlRule
  • CreateAccessRule ฟังก์ชันนี้สร้างกฎการเข้าถึงใหม่สำหรับปฏิทิน
  • UpdateAccessRule ฟังก์ชันนี้ใช้สำหรับอัปเดตกฎการเข้าถึง
  • FetchAccessRule สามารถใช้เพื่อดึงกฎการเข้าถึงเฉพาะสำหรับปฏิทินของไคลเอนต์
  • DeleteAccessRule ฟังก์ชันนี้ใช้สำหรับลบกฎการเข้าถึง

โค้ดสแนปต่อไปนี้แสดงวิธีใช้ฟังก์ชันสำหรับจัดการกฎการเข้าถึง:

การตั้งค่าไคลเอนต์ปฏิทินและข้อมูลสี

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(). วัตถุข้อมูลสีนี้ส่งคืนรายการสีพื้นหน้า สีพื้นหลัง และวันที่และเวลาที่อัปเดต

เข้าถึงการตั้งค่าไคลเอนต์

โค้ดสแนปต่อไปนี้แสดงวิธีใช้ฟังก์ชันเพื่อเข้าถึงการตั้งค่าของไคลเอนต์:

เข้าถึงข้อมูลสี

โค้ดสแนปต่อไปนี้แสดงวิธีใช้ฟังก์ชันเพื่อเข้าถึงการตั้งค่าสีของไคลเอนต์.

จัดการนัดหมายใน Google Calendar

Aspose.Email มีคุณสมบัติสำหรับทำงานกับนัดหมายในปฏิทินของ Google งานต่อไปนี้สามารถทำได้กับนัดหมายในปฏิทิน Google:

  1. เพิ่มนัดหมาย.
  2. ดึงรายการนัดหมาย.
  3. ดึงนัดหมายเฉพาะ.
  4. อัปเดตนัดหมาย.
  5. ย้ายนัดหมายจากปฏิทินหนึ่งไปยังอีกปฏิทินหนึ่ง.
  6. ลบนัดหมาย.

IGmailClient ให้ฟังก์ชันเช่น CreateAppointment, FetchAppointment, UpdateAppointment, ListAppointments, MoveAppointment และ DeleteAppointment.

เพิ่มการนัดหมายใน Google Calendar

ตัวอย่างโค้ดต่อไปนี้แสดงคุณลักษณะของการเพิ่มนัดหมายในปฏิทิน เพื่อบรรลุเป้าหมายนี้ ให้ทำตามขั้นตอนต่อไปนี้:

  1. สร้างและแทรกปฏิทิน.
  2. ดึงรายการนัดหมายจากปฏิทินใหม่.
  3. สร้างนัดหมาย.
  4. เพิ่มการนัดหมาย.

ดึงและอัปเดตนัดหมายใน Google Calendar

ที่นี่จะแสดงการดึงและอัปเดตปฏิทินตามที่ต่อไปนี้:

  1. ดึงนัดหมายบางส่วน.
  2. แก้ไขนัดหมาย.
  3. อัปเดตนัดหมายในปฏิทิน.

สมมติว่าปฏิทินที่มี id "calendarId" และนัดหมายที่มีรหัสเฉพาะ "AppointmentUniqueId" ได้รับการสกัดแล้ว ตัวอย่างโค้ดต่อไปนี้จะแสดงวิธีดึงและอัปเดตนัดหมาย.

ย้ายและลบนัดหมายใน Google Calendar

สามารถย้ายนัดหมายได้โดยระบุปฏิทินต้นทาง ปฏิทินปลายทาง และรหัสเฉพาะของนัดหมายในปฏิทินต้นทาง ตัวอย่างโค้ดต่อไปนี้จะแสดงวิธีย้ายและลบนัดหมาย.

FreeBusy Query สำหรับ Google Calendar

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