TL;DR
You should use the advanced API for this.
Calendar.Events.list(calendarId).items[0].id; // returns the id of the first event
If you just want the event ID for a specific event and are fine using a manual approach, then eventdeb=1
is your best bet.
If you want a programmatic solution, note that you cannot use the standard API (CalendarApp
) because the documentation for getId()
on a CalendarEvent
states:
Gets the unique iCalUID of the event. Note that the iCalUID and the event id used by the Calendar v3 API and Calendar advanced service are not identical and cannot be used interchangebly. 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.
This means if you want to construct a URL for a recurring event, you'll run into issues because you can't use getId() properly, especially for recurring events. The best fix is to use the Advanced Calendar API. (Also note that if you're trying to construct a link to the calendar event, the advanced API even has an htmlLink
property with the encoded calendarId + realEventId.
Here are some more details as to why getId()
it breaks for recurring events: This is because when you call event.getId()
on an event that is repeating, you get the same id. There is no way to reference a specific instance of the calendar event using the simple Calendar API. The URL is still able to reference a specific instance of the event by appending a format such as _20190626T000000Z
. However, it's not clear what values need to be provided there. There are also cases where _
is replaced with _R
and Z
replaced with @google.com
, further complicating matters.
To prove this is a problem:
- Find a repeating calendar event.
- Parse its URL (which is guaranteed to refer to the specific instance).
- Call
CalendarApp.getEventById(parsedId1).getId()
notice the value that is returned.
- Now parse a URL for a different instance of the event.
- Call
CalendarApp.getEventById(parsedId2).getId()
notice the value that is returned. Although parsedId1
and parsedId2
are different strings (referring to the specific instances), the returned value of getId()
on both of those is the same. The event is actually a reference to the specific instances, but you can't serialize their IDs anymore.
If you really want to use the simple calendar API, you'll likely need some kind of synchronization process and to store metadata/tags on each instance of the calendar events during as alluded to in this answer. However, the Advanced API will make your life a lot easier.