user1678677
user1678677

Reputation: 11

Google Apps Script - Issue getCalendarsByName then getEvents

I am building a Google Apps script code that will write the events from a Calendar to a Spreadsheet. The Calendar I want to use is not my default calendar, and I want to get this calendar by name. However, the method does not work ... getCalendarById does work but as this script is meant for a non initated user I need to get the calendar by name ...

Thank you for your further help !

var cal = CalendarApp.getCalendarsByName("Test 21");    
// var cal = CalendarApp.getDefaultCalendar(); 
var sheet = SpreadsheetApp.getActiveSheet();

var events = cal.getEvents(new Date("January 1, 2012"), new Date("December 31, 2030"));

for (var i=0;i<events.length;i++) {

var details=[[events[i].getTitle(), events[i].getDescription(), events[i].getStartTime(), events[i].getEndTime()]];

var row=i+1;

var range=sheet.getRange(row,1,1,4);

range.setValues(details);

}
}

When I launch my script I am getting the error : TypeError: Cannot find the method getEvents in Calendar. (line 22)

Upvotes: 1

Views: 3950

Answers (1)

Serge insas
Serge insas

Reputation: 46792

Did you notice the 's' in getCalendarsByName? This means that this method returns an array of calendar objects because there can be many calendars with the same name. If you think that there is most probably only one calendar with the name you entered then you can decide to get only the first one.

To do that, simply define Cal like this

var cal  = getCalendarByName(name string)[0]

Upvotes: 2

Related Questions