google-sheetsgoogle-apps-scriptgoogle-calendar-api

How to set the Google Calendar Event transparency for Appscript for Calendar API


I know that the calendar api and appscript for calendar are different API sets. As the question below.

GoogleAppsScript for Calendar vs Google CalendarAPI

I am trying to automate a synching of google sheets to calendar events. I am able to create calendar event but the calendar event being created keeps on having the status set to Busy. I read that you need to set the transparency to transparent to make it not set to busy.

But there seems to be no api for it for the google appscript for calendar. Any ideas?

let eventDescription = `blah blah`;
let newEvent = eventCal.createAllDayEvent(eventTitle, aDateObj, {
    description: eventDescription,
    // Tried to add transparency but this does not do anything
    transparency: "transparent"
});
newEvent.setColor(CalendarApp.EventColor.YELLOW);
    let changes = {
    transparency: "transparent"
};

// Saw the setting of patch but this api fails on appscripts.
//Calendar.Events.patch(changes, calendarId, newEvent.getId());

Solution

  • The error GoogleJsonResponseException: API call to calendar.events.patch failed with error: Not Found shows up whenever the sample script you provided is run using Google Apps Script.

    Upon testing, I see that the eventId being generated looks like this:

    SAMPLE

    However, what's being accepted in Events: patch is the eventId as something like q912u95u62rndr3a631fja2edo in the sample image.

    To resolve this, you must remove @google.com generated by the script. You can achieve that by changing:

      Calendar.Events.patch(changes, calendarId, newEvent.getId());
    

    To:

      Calendar.Events.patch(changes, calendarId, newEvent.getId().replace("@google.com", ""));
    

    This is the example script supplemented with the missing variables:

    const myFunction = () => {
      let eventCal = CalendarApp.getDefaultCalendar();
      let calendarId = eventCal.getId();
      let eventTitle = "Test Event";
      let aDateObj = new Date("September 29, 2024");
      let eventDescription = `blah blah`;
      let newEvent = eventCal.createAllDayEvent(eventTitle, aDateObj, { description: eventDescription }).setColor(CalendarApp.EventColor.YELLOW);
      let changes = {
        transparency: "transparent"
      };
      Calendar.Events.patch(changes, calendarId, newEvent.getId().replace("@google.com", ""));
    }
    

    OUTPUT

    OUTPUT

    OUTPUT