Tuesday, January 11, 2011

Merging Two Google Calendars

I regularly use Mail for Exchange to sync my phone with my Google Calendar account. It's awfully useful but there's one catch : you can't set it up to sync with two google calendars. It'll always sync only your primary calendar.

Now, I have a personal calendar with entries for my classes etc. Apart from that, I have also subscribed to another public calendar for keeping track of the activities organized by various clubs of IISER Kolkata. But the default-calendar-only policy of Mail for Exchange meant I was unable to get the second calendar to sync with my phone.

I googled for finding some solution for the issue, but it didn't yield any useful information. I had almost given up when I came across a not so well-known service from google called Google Apps Script. As the name suggests, It lets you run scripts written in JavaScript on google's server and the script can access many of google's popular services, including calendar.

That solved my problem completely. All I needed was a script which will add all the events of the second calendar to the primary calendar, with some background checks to avoid adding duplicate entries. That was pretty easy: as per google's standards, the documentation was excellent, with ample examples too. So I had a working version of the script ready within very little time.

The code:

You can add triggers for the script as well. I added a daily trigger, so it will run once a day and sync the secondary calendar. Problem solved!