Brandon Pillay
Brandon Pillay

Reputation: 1166

Calendar's "Event Update" Trigger isn't working

I'm trying to trigger a function calendarScan() when changes are made to my Google Calendar events using the code below.

ScriptApp.newTrigger('calendarScan')
  .forUserCalendar(Session.getActiveUser().getEmail())
  .onEventUpdated()
  .create();

I can't figure out why the trigger isn't working. The script is standalone. The calendarScan() functions runs fine manually.

Your help is sincerely appreciated.

Upvotes: 0

Views: 2038

Answers (1)

tehhowch
tehhowch

Reputation: 9872

To create a Calendar Event trigger for all calendars a user owns, the Calendar API can be used (once enabled):

function getOwnedCalendars_() {
  const options = {
    minAccessLevel: "owner", // freeBusyReader, reader, writer
    fields: "nextPageToken, items(id,summary,description)"
  };

  const cals = [];
  do {
    var search = Calendar.CalendarList.list(options);
    options.pageToken = search.nextPageToken;
    if (search.items && search.items.length)
      Array.prototype.push.apply(cals, search.items);
  } while (options.pageToken);
  return cals;
}
function giveMeTheTrigger(e) {
  console.log(e);
}
function makeTriggers() {
  getOwnedCalendars_().forEach(function (cal) {
    var t = ScriptApp.newTrigger("giveMeTheTrigger")
         .forUserCalendar(cal.id)
         .onEventUpdated()
         .create();
    console.log({message: "Created event trigger for calendar '" + cal.summary + "'",
                 desc: cal.description, id: cal.id, triggerId: t.getUniqueId()});
  });
}

Given that you're expected to be using the Calendar API to effectively use this Event Updated trigger, this shouldn't be too much extra overhead.

Upvotes: 3

Related Questions