Search code examples
google-apps-scriptgoogle-calendar-api

Calendar event missing methods?


I am trying to write a script to manipulate Calendar data. I seem to be retrieving events, but the objects do not implement the expected methods.

Here's my MVE:

const response = Calendar.Events.list(myCalendar, 
   {   timeMin: (new Date()).toISOString(),
       timeMax: (new Date(new Date().getTime() + (24 * 60 * 60 * 1000))).toISOString(),
       showDeleted: false,
       singleEvents: true,
       maxResults: 100,
       orderBy: 'startTime',
       timeZone: 'UTC' }
);
for (event of response.items) {
    Logger.log(event);
    Logger.log(event.summary + " / " + event.getTag("PRInv");
}

I expect Calendar.Events.list.items to be an array of event objects. While the first Logger.log dumps something which looks like it might be an event object:

{
htmlLink=https://www.google.com/calendar/event?eid=NjR...ldmEuY29t&ctz=UTC, 
description=hello world, 
[email protected], 
summary=Colin - test calendar entry, 
kind=calendar#event, 
updated=2024-04-18T21:08:13.098Z, 
end={dateTime=2024-04-19T21:00:00Z, timeZone=America/Los_Angeles}, 
reminders={useDefault=true}, 
id=fgfkgfgkfjgkdjgjd, 
etag="3426948986196000", 
sequence=0.0, 
eventType=default, 
status=confirmed, 
start={timeZone=America/Los_Angeles, dateTime=2024-04-19T20:00:00Z}, 
organizer={self=true, [email protected]}, 
created=2024-04-18T21:08:13.000Z, 
creator={[email protected], self=true}
}

However the next line fails with

event.getTag is not a function

Using

Calendar.getEventById(event.id).getTag("PRInv");

I get Calendar.getEventById is not a function even though the docs (again) say that it is a function.

What am I missing here?


Solution

  • Modification points:

    • I guessed that from your showing script, you might think of Calendar API (Calendar) and Calendar service (CalendarApp) as the same. Unfortunately, those are different.

    • If you want to use getTag("PRInv") of Calendar service (CalendarApp) with Calendar API, it is required to search a property of extendedProperties. If Logger.log(event.summary + " / " + event.getTag("PRInv"); is modified, it becomes as follows.

      Logger.log(event.summary + " / " + event.extendedProperties.shared["PRInv"]);
      
    • By the way, Calendar.getEventById(event.id).getTag("PRInv"); cannot be used. In that case, it is required to be as follows.

      CalendarApp.getCalendarById(myCalendar).getEventById(event.iCalUID).getTag("PRInv")
      

    When these points are reflected in your showing script, it becomes as follows.

    Modified script:

    const response = Calendar.Events.list(myCalendar,
      {
        timeMin: (new Date()).toISOString(),
        timeMax: (new Date(new Date().getTime() + (24 * 60 * 60 * 1000))).toISOString(),
        showDeleted: false,
        singleEvents: true,
        maxResults: 100,
        orderBy: 'startTime',
        timeZone: 'UTC'
      }
    );
    for (event of response.items) {
      Logger.log(event);
      Logger.log(event.summary + " / " + event.extendedProperties.shared["PRInv"]); // and, Logger.log(event.summary + " / " + event.getExtendedProperties().getShared()["PRInv2"]);
    }
    
    • By this modification, you can see the value of PRInv from the event object.

    References: