I have several Google calendars: one for lectures, one for appointments, one for vacations, etc. I need to combine them automatically into a single ICS file. Surprisingly, this feature does not seem to exist off-the-shelf.
I have devised a solution based on Google Apps Script: a Javascript script runs periodically on the Google cloud, and generates the ICS file. Surprisingly, the script has to be in a Google Docs spreadsheet. The script editor is in the Tools menu. When the script works okay, it’s possible to schedule its execution periodically in the script editor, via the Triggers menu. As easy as a good old cronjob.
I use the following script:
function eventToICS(event) {
var tz = "Europe/Paris";
var dateFormat = "yyyyMMdd'T'HHmmss";
var res = "";
res += "BEGIN:VEVENT\
";
res += "SUMMARY:" + event.getTitle() + "\
";
res += "DTSTART;TZID=" + tz + ":" +
Utilities.formatDate(event.getStartTime(), tz, dateFormat) + "\
";
res += "DTEND;TZID=" + tz + ":" +
Utilities.formatDate(event.getEndTime(), tz, dateFormat) + "\
";
res += "END:VEVENT\
\
";
return res;
}
function aggregateCalendars() {
var cals = CalendarApp.getAllOwnedCalendars();
var ics = "BEGIN:VCALENDAR\
VERSION:2.0\
\
";
var startDate = new Date();
var endDate = new Date();
startDate.setDate(startDate.getDate() - 50);
endDate.setDate(endDate.getDate() + 500);
for(var i=0; i<cals.length; i++) {
ics += "COMMENT: " + cals[i].getName() + "\
\
";
var events = cals[i].getEvents(startDate, endDate);
for(var j=0; j<events.length; j++) {
ics += eventToICS(events[j]);
}
}
ics += "\
END:VCALENDAR\
";
// here do something with the ICS file...
}
Once the contents of the ICS file has been constructed, you can do whatever you want with it, for instance send it via e-mail. Personally I upload it onto a Google Sites page :
var page = SitesApp.getPageByUrl(pageURL);
page.addHostedAttachment(Utilities.newBlob(ics, "text/plain", filename));
HTML5 valide ?
© Christophe Jacquet. ✍ Contact.
Mentions légales.