In GAS trigger event code, how to find out which G

2019-08-04 06:27发布

问题:

Using the advanced Calendar API Service (https://developers.google.com/apps-script/advanced/calendar) in Google Apps Script, I have found that you can cause an event handler function to be called when the calendar is updated (see screenshot), and that works fine: each time an event is added or changed it appears that my function is called. However I can't figure out which Calendar Event (i.e. appointment) has been modified. The handler function is called with 1 arg (e) which is of type "event", but it doesn't seem to contain the ID or any reference to the calendar event that was updated. Here is my handler code:

function triggeredOnUpdate(e){
  Logger.log('Update event: %s', e);

  var calendarId = 'primary';
  var eventId = e.<????what goes here???>;
  var event = Calendar.Events.get(calendarId, eventId);

  Logger.log('Running update on Calendar Event: %s', event.summary);
  colourEvent(calendarId, event);
}

And the log output is:

Update event: {authMode=FULL, calendarId=mycalendar@mydomain.com, triggerUid=1325034127}

That TriggerUID is the ID of the trigger, so its the same every time this handler is called.

Do you know how I can find out which Calendar Event has been updated?

(Note: the word event is overloaded when talking about triggers in a calendar: there's a calendar event, like and an appointment, and the update event, when that appointment is changed)

回答1:

Follow this workflow to get the event ID: https://developers.google.com/apps-script/guides/triggers/events#eventupdated

In summary: use Calendar.Events.list with the calendar address you got in the function parameter to perform a full sync, which includes nextSyncToken which can be saved in script properties.

The next time the event is triggered, you can perform an incremental sync by supplying nextSyncToken to Calendar.Events.list. This will give you the events since the last sync.

See also: https://developers.google.com/calendar/v3/reference/events/list https://developers.google.com/calendar/v3/reference/events#resource

https://developers.google.com/apps-script/advanced/calendar