Home > Enterprise >  How to get or address a single event of a event series of a Google calendar by GAS
How to get or address a single event of a event series of a Google calendar by GAS

Time:11-04

In a Google calendar events have a unique id, but does the event of an event series have one, too? I need to handle each event of an event series differently. The regular way to address an event is:

let now      = new Date();
let until    = new Date(2023, 10, 25);
let calendar = CalendarApp.getCalendarById("[email protected]");
let events   = calendar.getEvents(now, until); // returns an array of all events within this period of time
// Make a list of all event id:
for (i=0; i<events.length; i  ) {
  console.log("event"   i   " has id "   events[i].getId());
}

Although events of an event series have all the same event id I can address them reliably by the variable's index, eg. events[3] as long as the variable events is alive.

Verbose example:

Assuming there is a series of five events of one event series within one day:

2022-11-03 11:18 (= event[0])
2022-11-03 12:22 (= event[1])
2022-11-03 13:15 (= event[2])
2022-11-03 13:30 (= event[3])
2022-11-03 14:00 (= event[4])

Having these indexes I can create Google forms to collect food orderings to the single events and I can add a guest to [2] and change the room of [2] and [3]:

2022-11-03 11:18 (= event[0])
2022-11-03 12:22 (= event[1])
2022-11-03 13:15 (= event[2]) - added Mr. Smith, set location: Ball Room
2022-11-03 13:30 (= event[3]) - set location: Ball Room
2022-11-03 14:00 (= event[4])

Then someone uses the calendar GUI to rightfully shift the time of some events which are varying by details and thus distinguishable now:

2022-11-03 11:18 (= event[0])
2022-11-03 12:22 (= event[1])
2022-11-03 11:30 (= event[2]) - added Mr. Smith, set location: Ball Room
2022-11-03 15:30 (= event[3]) - set location: Ball Room
2022-11-03 14:00 (= event[4])

Running a new getEvents will return

2022-11-03 11:18 (= event[0], formerly event[0])
2022-11-03 11:30 (= event[1], formerly event[2]) - with added Mr. Smith and location Ball Room
2022-11-03 12:22 (= event[2], formerly event[1])
2022-11-03 14:00 (= event[3], formerly event[4])
2022-11-03 15:30 (= event[4], formerly event[3]) - with location Ball Room

How can I now address a specific event of that series, for example to sync them with their corresponding forms? Do the events of an event series have some kind of a fixed "sub event id"?

CodePudding user response:

You can find in the official doc this statement

Note that the icalUID and the id are not identical and only one of them should be supplied at event creation time. One difference in their semantics is that in recurring events, all occurrences of one event have different ids while they all share the same icalUIDs.

(link)

So yes, an occurrence of a recurring event has its own id (its id property).

The problem with Apps Script is that the method getId() actually retrieves the iCalUID property of an event, not its unique id. So using the CalendarApp service, all occurrences will have the same id (doc).

Try using the advanced Calendar service and you'll be able to list events, get their unique id, get their iCalUID and so on.

  • Related