Search code examples
google-apps-scriptgoogle-apps

Is there a way to modify a live Calendar event through Google Apps Script?


Goal: Add attendees through Google Calendar Add on to live event being created on the spot

Context: Live Event being created (left)+ Calendar Add on (Right)

Live Event being created (left)+ Calendar Add on (Right)

Code snip:

current_user = e.calendar.calendarId;
event_id = e.calendar.id;
var calendar = CalendarApp.getCalendarById(current_user);
var event = calendar.getEventById(event_id);

However, when I open an event, and current_user and event_id have values, event seems to be null, because this event is not technically 'created' yet. If it were created yet, then a simple event.addGuest() would work

Is it even possible to customize a live event while it is being created? Every example I find only updates events already created.

Edit: Forgot to post scopes I am using. Snippet of appsscript.json is below

{
  ....,
  "oauthScopes": [
    "https://www.googleapis.com/auth/calendar.addons.current.event.read",
    "https://www.googleapis.com/auth/calendar.addons.current.event.write",
    "https://www.googleapis.com/auth/calendar.addons.execute",
    "https://www.googleapis.com/auth/script.external_request",
    "https://www.google.com/calendar/feeds"
  ],
  "addOns": {
    "common": {
      ...
      }
    },
    "calendar": {
      "currentEventAccess": "READ_WRITE",
      ...
    }
  }
}

Solution

  • Change your code to:

      if (!e.calendar.capabilities.canAddAttendees) {
        return;
      }
    
      CardService.newCalendarEventActionResponseBuilder()
        .addAttendees(["example1@example.com", "example2@example.com"])
        .build();
    

    See documentation at:
    https://developers.google.com/workspace/add-ons/calendar/calendar-actions?hl=en#adding_attendees_with_a_callback_function