I am new to Google App Maker and am trying to build m own custom version of the Calendar sample was that given. In the example, the code grabs the default calendar by getDefaultCalendar. I have another Calendar that I want to grab (either by name or id). After using getCalendarByName and debugging it, it finds the calendar, however when i use it with getEvents it does not populate in the sample app.
The code below is from the Calendar Sample.
/**
* Gets Calendar events.
* @param {Query} query - data query with parameters.
* @return {Array<Event>} events from Calendar.
*/
function getEvents_(query) {
var startDate = query.parameters.StartDate;
startDate.setHours(0, 0, 0, 0);
var endDate = query.parameters.EndDate;
endDate.setDate(endDate.getDate() + 1);
endDate.setHours(0, 0, 0, 0);
if (startDate.getTime() > endDate.getTime()) {
return [];
}
var results = [];
var events =
CalendarApp.getDefaultCalendar().getEvents(startDate,endDate);
events.forEach(function(item) {
var event = app.models.Events.newRecord();
event.StartDate = item.getStartTime();
event.EndDate = item.getEndTime();
event.Title = item.getTitle();
event.Color = item.getColor();
results.push(event);
});
return results;
}
The function is getCalendarsByName
with an s
and thus it returns an array of calendars. If you know you'll only get one correct result you can just change the code to:
CalendarApp.getCalendarsByName('email@gmail.com')[0].getEvents(startDate,endDate);
getCalendarById
will return a calendar on it's own, so you could just use that instead.