使用者可以自由更新或刪除 Google 日曆活動。如果使用者在建立會議後更新活動,您的外掛程式可能需要更新會議資料來回應異動內容。如果您的第三方會議系統需要追蹤事件資料,若無法在事件變更時更新會議,可能會導致會議無法使用,進而對使用者體驗造成負面影響。
將會議資料與 Google 日曆活動變更保持同步的程序稱為「同步」。您可以建立 Apps Script 可安裝觸發條件,在特定日曆中的事件發生變更時觸發,藉此同步處理事件變更。很抱歉,觸發條件不會回報哪些事件已變更,而且您無法將觸發條件限制為僅限於您建立的會議活動。相反地,您必須要求一份清單,列出自上次同步處理以來對日曆發生的所有變更,請篩選事件清單,並據此進行更新。
一般同步處理程序如下:
- 使用者第一次建立會議時,系統會初始化同步處理程序。
- 每當使用者建立、更新或刪除日曆活動時,觸發事件就會在外掛程式專案中執行觸發函式。
- 觸發事件函式會檢查自上次同步處理以來的事件變更集合,並判斷是否需要更新相關的第三方會議。
- 提出第三方 API 要求,對會議進行所有必要更新。
- 系統會儲存新的同步憑證,如此一來,下次觸發事件執行時,只需要查看日曆的最新變更。
初始化同步處理作業
在第三方系統上成功建立會議後,如果沒有可安裝的觸發事件,外掛程式應建立可回應此日曆事件變更的觸發事件。
建立觸發條件後,初始化作業應透過建立初始同步權杖來完成。這可透過直接執行觸發事件函式來完成。
建立日曆觸發條件
如要同步處理,您的外掛程式需要偵測附加會議的 Google 日曆活動是否有變更。這可透過建立 EventUpdated
可安裝的觸發條件來達成。外掛程式只需要為每個日曆建立一個觸發條件,而且可以透過程式碼建立這些觸發條件。
建立觸發條件的好時機,就是使用者第一次建立會議時,因為使用者之後會開始使用外掛程式。建立會議並確認沒有錯誤後,外掛程式應檢查此使用者是否有觸發事件,如果沒有,則建立觸發事件。
實作同步處理觸發條件函式
當 Apps Script 偵測到觸發觸發條件的條件時,就會執行觸發條件函式。當使用者在指定日曆中建立、修改或刪除任何事件時,就會觸發 EventUpdated
日曆觸發條件。
您必須實作外掛程式使用的觸發事件函式。這個觸發事件函式應執行以下操作:
- 使用
syncToken
發出 Google 日曆進階服務Calendar.Events.list()
呼叫,擷取自上次同步處理後已變更的活動清單。使用同步權杖可減少外掛程式必須檢查的事件數量。當觸發事件函式在沒有有效的同步處理權杖的情況下執行時,會改為執行完整同步處理。完整同步作業只會在規定的時間範圍內嘗試擷取所有事件,以產生新的有效同步權杖。
- 系統會檢查每個修改過的事件,判斷是否有相關聯的第三方會議。
- 如果活動有會議,我們會檢查變更內容。 視變更內容而定,您可能需要修改相關會議。舉例來說,如果刪除活動,外掛程式應該也會刪除會議。
- 如需對會議進行任何必要變更,請向第三方系統發出 API 呼叫。
- 完成所有必要變更後,請儲存
Calendar.Events.list()
方法傳回的nextSyncToken
。這個同步符記會出現在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. } }