I have multiple google calendars which I would like to display combined in a single list (like on the google calendar ui) using google calendar api on javascript.
The official google calendar api doc is pretty good. The main snipped is the following:
function listUpcomingEvents() {
gapi.client.calendar.events.list({
'calendarId': 'primary',
'timeMin': (new Date()).toISOString(),
'showDeleted': false,
'singleEvents': true,
'maxResults': 10,
'orderBy': 'startTime'
}).then(function(response) {
var events = response.result.items;
appendPre('Upcoming events:');
if (events.length > 0) {
for (i = 0; i < events.length; i++) {
var event = events[i];
var when = event.start.dateTime;
if (!when) {
when = event.start.date;
}
appendPre(event.summary + ' (' + when + ')')
}
} else {
appendPre('No upcoming events found.');
}
});
}
due to I have multiple calendars I obviously have to replace the 'calendarId': 'primary' request with my multiple calendar ids. To get the various ids its simple as well. e.g.:
function listCalendars() {
gapi.client.calendar.calendarList.list({
}).then(function(response) {
var calendars = response.result.items;
console.log(calendars);
});
}
Due to the mentioned 'calendarId': 'primary' accepts just a single value (and not a list) I have to call the endpoint gapi.client.calendar.events.list various times in order to get all events.
BUT when I want to have my events in order (using start date due to its a calendar) I have to sort them.
Because its async I'm not sure how to collect all events and then sort them. Any ideas how to do this? (or request all events from multiple calendars at the same time/call?)