Search code examples
javascriptgoogle-apps-scriptcalendargoogle-apis-explorer

Change Google Calendar Event Free/Busy with Calendar API


I have a GAS web app for reserving rooms. When the app creates the event, it currently defaults to "Busy" for the event. I am trying to set the default to "Free".

I found a GAS forum entry that recommends using the Advanced Google Calendar API to edit the transparency field (Source: https://code.google.com/p/google-apps-script-issues/issues/detail?id=2341).

The script they suggested was

var changes = {
transparency: "transparent"
};
Calendar.Events.patch(changes, cal_id, event_id);

I have the Advanced API enabled, but for some reason I am getting an uncaught error prompt in the Chrome console when the function executes. Any thoughts on where this error is coming from?


Solution

  • After looking around, the following seems to work. I forgot that you need to remove the "@google.com" from the event ID returned by CalendarApp before making a request to CalendarApi. The calendarId can be set to 'primary' since the user is only editing an event on their own calendar

    var eventId= event_id.slice(0,event_id.length-11);
    var calendarId = 'primary';
    Logger.log(eventId)
    var changes = {
        transparency: "transparent"
      };
    Calendar.Events.patch(changes,calendarId,eventId);