กำลังซิงค์การเปลี่ยนแปลงการประชุมในปฏิทิน

ผู้ใช้จะอัปเดตหรือลบกิจกรรมใน Google ปฏิทินได้อย่างอิสระ หากผู้ใช้ อัปเดตกิจกรรมหลังจากที่สร้างการประชุมแล้ว ส่วนเสริมของคุณอาจต้อง ตอบสนองต่อการเปลี่ยนแปลงดังกล่าวโดยอัปเดตข้อมูลการประชุม หาก ระบบการประชุมของบุคคลที่สามจะขึ้นอยู่กับการติดตามข้อมูลเหตุการณ์ การไม่อัปเดตการประชุมตามการเปลี่ยนแปลงกิจกรรมสามารถแสดงผลการประชุมได้ ไม่สามารถใช้งานได้ และทำให้ผู้ใช้ได้รับประสบการณ์ที่ไม่ดี

กระบวนการอัปเดตข้อมูลการประชุมอยู่เสมอโดยมีการเปลี่ยนแปลง กิจกรรมใน Google ปฏิทินเรียกว่าการซิงค์ คุณสามารถซิงค์การเปลี่ยนแปลงกิจกรรมได้โดย การสร้าง Apps Script ทริกเกอร์ที่ติดตั้งได้ที่เริ่มทำงาน เมื่อใดก็ตามที่กิจกรรมในปฏิทินหนึ่งๆ มีการเปลี่ยนแปลง ขออภัย ทริกเกอร์จะไม่ ได้ว่ามีการเปลี่ยนแปลงเหตุการณ์ใดบ้าง และคุณไม่สามารถจำกัดให้แสดงเฉพาะเหตุการณ์ที่ การประชุมที่คุณสร้างขึ้นได้อีกด้วย แต่คุณจะต้องขอรายการ การเปลี่ยนแปลงทั้งหมดที่ทำกับปฏิทินนับตั้งแต่การซิงค์ครั้งล่าสุด ให้กรอง รายการเหตุการณ์ แล้วอัปเดตให้สอดคล้องกัน

กระบวนการซิงค์ทั่วไปมีดังต่อไปนี้

  1. ครั้งแรกที่ผู้ใช้สร้างการประชุม ขั้นตอนการซิงค์จะเริ่มต้น
  2. เมื่อใดก็ตามที่ผู้ใช้สร้าง อัปเดต หรือลบกิจกรรมในปฏิทินของตน ทริกเกอร์จะเรียกใช้ฟังก์ชันทริกเกอร์ในโปรเจ็กต์ส่วนเสริม
  3. ฟังก์ชันทริกเกอร์จะตรวจสอบชุดการเปลี่ยนแปลงเหตุการณ์นับจากครั้งล่าสุด ซิงค์และระบุว่าต้องมีการอัปเดตบุคคลที่สามที่เชื่อมโยงหรือไม่ การประชุม
  4. การอัปเดตที่จําเป็นในการประชุมดังกล่าวด้วยการสร้างบริการของบุคคลที่สาม คำขอ API
  5. ระบบจะจัดเก็บโทเค็นการซิงค์ใหม่ไว้เพื่อให้การเรียกใช้ทริกเกอร์ถัดไปทำได้เพียง ตรวจสอบการเปลี่ยนแปลงล่าสุดในปฏิทิน

เริ่มต้นการซิงค์

เมื่อสร้างการประชุมในระบบของบุคคลที่สามเรียบร้อยแล้ว โค้ดควรสร้างทริกเกอร์ที่ติดตั้งได้ ที่ตอบสนองต่อ การเปลี่ยนแปลงของกิจกรรม ในปฏิทินนี้ ถ้าไม่มีทริกเกอร์อยู่แล้ว

หลังจากสร้างทริกเกอร์ การเริ่มต้นควรเสร็จสิ้นด้วยการสร้างค่าเริ่มต้น โทเค็นการซิงค์ ซึ่งทำได้ด้วยการเรียกใช้ฟังก์ชันทริกเกอร์โดยตรง

สร้างทริกเกอร์ปฏิทิน

ในการซิงค์ ส่วนเสริมจะต้องตรวจพบเมื่อกิจกรรมในปฏิทินมีการแนบ การประชุมมีการเปลี่ยนแปลง ซึ่งทำได้โดยสร้าง EventUpdated ทริกเกอร์ที่ติดตั้งได้ ส่วนเสริมของคุณ แต่ละปฏิทินต้องใช้ทริกเกอร์เพียง 1 รายการเท่านั้น รวมทั้งสร้างโดยใช้โปรแกรมได้

เวลาที่เหมาะสมในการสร้างทริกเกอร์คือเมื่อผู้ใช้สร้างการประชุมครั้งแรก เพราะผู้ใช้เริ่มใช้ส่วนเสริม หลัง การสร้างการประชุม และยืนยันว่าไม่มีข้อผิดพลาด ส่วนเสริมของคุณควรตรวจสอบว่า มีทริกเกอร์สำหรับผู้ใช้รายนี้และหากไม่ได้สร้าง

ใช้ฟังก์ชันทริกเกอร์การซิงค์

ฟังก์ชันทริกเกอร์จะทำงานเมื่อ Apps Script ตรวจพบเงื่อนไขที่ทำให้เกิด ทริกเกอร์ให้เริ่มทำงาน ทริกเกอร์ปฏิทิน EventUpdated รายการ เริ่มทำงานเมื่อผู้ใช้สร้าง แก้ไข หรือลบกิจกรรมใดๆ ใน ปฏิทิน

คุณต้องใช้ฟังก์ชันทริกเกอร์ที่ส่วนเสริมใช้ ฟังก์ชันทริกเกอร์นี้ ควรทำดังนี้

  1. โทร Calendar.Events.list() สำหรับบริการขั้นสูงของปฏิทินโดยใช้ syncTokenเพื่อเรียกข้อมูลรายการเหตุการณ์ที่มีการเปลี่ยนแปลงนับจากครั้งล่าสุด การซิงค์ การใช้โทเค็นการซิงค์จะช่วยลดจำนวนเหตุการณ์ที่ส่วนเสริมลงได้ ต้องตรวจสอบ

    เมื่อฟังก์ชันทริกเกอร์ทำงานโดยไม่มีโทเค็นการซิงค์ที่ถูกต้อง ฟังก์ชันจะย้อนกลับ เปลี่ยนเป็นการซิงค์เต็มรูปแบบ การซิงค์แบบเต็มจะเรียกเหตุการณ์ทั้งหมด ภายในกรอบเวลาที่กำหนดเพื่อสร้างการซิงค์ใหม่ที่ถูกต้อง โทเค็น

  2. เหตุการณ์ที่แก้ไขแต่ละรายการจะได้รับการตรวจสอบเพื่อพิจารณาว่ามีการเชื่อมโยง การประชุมของบุคคลที่สามเท่านั้น
  3. หากกิจกรรมมีการประชุม ระบบจะตรวจสอบเพื่อหาสิ่งที่เปลี่ยนแปลง การแก้ไขการประชุมที่เกี่ยวข้องอาจขึ้นอยู่กับการเปลี่ยนแปลงที่เกิดขึ้น เป็นสิ่งที่จำเป็น ตัวอย่างเช่น หากกิจกรรมถูกลบ ส่วนเสริมควร ก็อาจจะลบการประชุมนั้นด้วย
  4. การเปลี่ยนแปลงที่จำเป็นในการประชุมจะกระทำโดยการเรียก API ไปที่ ระบบของบุคคลที่สาม
  5. หลังจากทำการเปลี่ยนแปลงที่จำเป็นทั้งหมดแล้ว ให้จัดเก็บ nextSyncToken ที่แสดงผลโดย เมธอด Calendar.Events.list() พบโทเค็นการซิงค์นี้เมื่อ หน้าผลการค้นหาที่แสดงโดยการเรียก Calendar.Events.list()

กำลังอัปเดตกิจกรรมใน Google ปฏิทิน

ในบางกรณี คุณอาจต้องการอัปเดตกิจกรรมใน Google ปฏิทินเมื่อดำเนินการ การซิงค์ หากคุณเลือกที่จะทำเช่นนี้ ให้ทำการอัปเดตกิจกรรมด้วย บริการขั้นสูงของ Google ปฏิทิน อีกครั้ง อย่าลืมใช้ การอัปเดตแบบมีเงื่อนไข ที่มีส่วนหัว If-Match วิธีนี้จะป้องกันไม่ให้มีการเปลี่ยนแปลงโดยไม่ตั้งใจ เขียนทับการเปลี่ยนแปลงที่เกิดขึ้นพร้อมกันโดยผู้ใช้ในไคลเอ็นต์อื่น

ตัวอย่าง

ตัวอย่างต่อไปนี้แสดงวิธีการตั้งค่าการซิงค์สำหรับกิจกรรมในปฏิทิน และการประชุมที่เกี่ยวข้อง

/**
 *  Initializes syncing of conference data by creating a sync trigger and
 *  sync token if either does not exist yet.
 *
 *  @param {String} calendarId The ID of the Google Calendar.
 */
function initializeSyncing(calendarId) {
  // Create a syncing trigger if it doesn't exist yet.
  createSyncTrigger(calendarId);

  // Perform an event sync to create the initial sync token.
  syncEvents({'calendarId': calendarId});
}

/**
 *  Creates a sync trigger if it does not exist yet.
 *
 *  @param {String} calendarId The ID of the Google Calendar.
 */
function createSyncTrigger(calendarId) {
  // Check to see if the trigger already exists; if does, return.
  var allTriggers = ScriptApp.getProjectTriggers();
  for (var i = 0; i < allTriggers.length; i++) {
    var trigger = allTriggers[i];
    if (trigger.getTriggerSourceId() == calendarId) {
      return;
    }
  }

  // Trigger does not exist, so create it. The trigger calls the
  // 'syncEvents()' trigger function when it fires.
  var trigger = ScriptApp.newTrigger('syncEvents')
      .forUserCalendar(calendarId)
      .onEventUpdated()
      .create();
}

/**
 *  Sync events for the given calendar; this is the syncing trigger
 *  function. If a sync token already exists, this retrieves all events
 *  that have been modified since the last sync, then checks each to see
 *  if an associated conference needs to be updated and makes any required
 *  changes. If the sync token does not exist or is invalid, this
 *  retrieves future events modified in the last 24 hours instead. In
 *  either case, a new sync token is created and stored.
 *
 *  @param {Object} e If called by a event updated trigger, this object
 *      contains the Google Calendar ID, authorization mode, and
 *      calling trigger ID. Only the calendar ID is actually used here,
 *      however.
 */
function syncEvents(e) {
  var calendarId = e.calendarId;
  var properties = PropertiesService.getUserProperties();
  var syncToken = properties.getProperty('syncToken');

  var options;
  if (syncToken) {
    // There's an existing sync token, so configure the following event
    // retrieval request to only get events that have been modified
    // since the last sync.
    options = {
      syncToken: syncToken
    };
  } else {
    // No sync token, so configure to do a 'full' sync instead. In this
    // example only recently updated events are retrieved in a full sync.
    // A larger time window can be examined during a full sync, but this
    // slows down the script execution. Consider the trade-offs while
    // designing your add-on.
    var now = new Date();
    var yesterday = new Date();
    yesterday.setDate(now.getDate() - 1);
    options = {
      timeMin: now.toISOString(),          // Events that start after now...
      updatedMin: yesterday.toISOString(), // ...and were modified recently
      maxResults: 50,   // Max. number of results per page of responses
      orderBy: 'updated'
    }
  }

  // Examine the list of updated events since last sync (or all events
  // modified after yesterday if the sync token is missing or invalid), and
  // update any associated conferences as required.
  var events;
  var pageToken;
  do {
    try {
      options.pageToken = pageToken;
      events = Calendar.Events.list(calendarId, options);
    } catch (err) {
      // Check to see if the sync token was invalidated by the server;
      // if so, perform a full sync instead.
      if (err.message ===
            "Sync token is no longer valid, a full sync is required.") {
        properties.deleteProperty('syncToken');
        syncEvents(e);
        return;
      } else {
        throw new Error(err.message);
      }
    }

    // Read through the list of returned events looking for conferences
    // to update.
    if (events.items && events.items.length > 0) {
      for (var i = 0; i < events.items.length; i++) {
         var calEvent = events.items[i];
         // Check to see if there is a record of this event has a
         // conference that needs updating.
         if (eventHasConference(calEvent)) {
           updateConference(calEvent, calEvent.conferenceData.conferenceId);
         }
      }
    }

    pageToken = events.nextPageToken;
  } while (pageToken);

  // Record the new sync token.
  if (events.nextSyncToken) {
    properties.setProperty('syncToken', events.nextSyncToken);
  }
}

/**
 *  Returns true if the specified event has an associated conference
 *  of the type managed by this add-on; retuns false otherwise.
 *
 *  @param {Object} calEvent The Google Calendar event object, as defined by
 *      the Calendar API.
 *  @return {boolean}
 */
function eventHasConference(calEvent) {
  var name = calEvent.conferenceData.conferenceSolution.name || null;

  // This version checks if the conference data solution name matches the
  // one of the solution names used by the add-on. Alternatively you could
  // check the solution's entry point URIs or other solution-specific
  // information.
  if (name) {
    if (name === "My Web Conference" ||
        name === "My Recorded Web Conference") {
      return true;
    }
  }
  return false;
}

/**
 *  Update a conference based on new Google Calendar event information.
 *  The exact implementation of this function is highly dependant on the
 *  details of the third-party conferencing system, so only a rough outline
 *  is shown here.
 *
 *  @param {Object} calEvent The Google Calendar event object, as defined by
 *      the Calendar API.
 *  @param {String} conferenceId The ID used to identify the conference on
 *      the third-party conferencing system.
 */
function updateConference(calEvent, conferenceId) {
  // Check edge case: the event was cancelled
  if (calEvent.status === 'cancelled' || eventHasConference(calEvent)) {
    // Use the third-party API to delete the conference too.


  } else {
    // Extract any necessary information from the event object, then
    // make the appropriate third-party API requests to update the
    // conference with that information.

  }
}