Search code examples
javascriptgoogle-apps-scriptgoogle-sheetsgoogle-calendar-apicustom-function

How can I authorize custom function to use Google Calendar?


I'm trying to create a custom function in Google Sheets that generates an array with de brazilian holidays, to automate it in another formulas that calculates data intervals considering holidays. But this code is generating that error message:

The script is not allowed to perform this action. Required Permissions: (https://www.googleapis.com/auth/calendar || https://www.googleapis.com/auth/calendar.readonly || https://www.google.com/calendar/feeds) (line 4).

I have already allowed the Calendar API in the Google API Console and created the credentials, but I don't know if I need to use them in my code or if there's some Google problem.

Here's the code I tried:

function FERIADOS() {
  var cal = "pt.brazilian#[email protected]";
  var calendario = CalendarApp.getCalendarById(cal);

  var dtini = new Date('January 1, 2019 00:00:01 -0300');
  var dtfim = new Date('December 31, 2020 23:59:59 -0300');

  var eventos = calendario.getEvents(dtini, dtfim);

  for (var i = 1; i < eventos.length ; ++i) {
     var event = [];
     event[i] = eventos[i].getStartTime(); 
  }

  return event;
}

Solution

  • Calendar Service is not one of the Services available to custom functions. Read Guidelines for custom functions and Using Apps Script Services