Monday, June 20, 2011

Google Apps Scripts: Shared Contact Groups

UPDATE: Jul 7, 2012
While revising the script I did more debugging as there were some code chunks I wanted to improve, thinking that maybe Apps script performance got better. It turns out, creating a new or updating existing contact is very inefficient. Here's a screenshot from my debugging session:

Notice a bunch of cc.setXxx() methods. From my understanding every setXxx() call (e.g. setFamilyName) actually does a real API call to Google Contacts backend, so you can imagine how many API calls the script does only to update or create one contact. I suspect Google (reasonably) sets an API call rate threshold which is probably what limits the synchronization process and "We're sorry, a server error occurred. Please try again".

A much more efficient approach would be to create or update a contact data in one API call, e.g. ContactsApp.createContact(...all fields go here...).

Another efficient approach would be to update all fields without making actual Contacts API calls and only afterwords do one API call e.g. ContactsApp.updateContact(contact_object).

Unfortunately, there are no efficient approaches currently available. At least, I could not dig it up from Apps script documentation. This leaves the script to be quite inefficient as there is no way to do "batch updates", especially with a medium to large contact groups. Well, it is possible to use directly Google Contacts API protocol (GData) from Apps script, but App Engine would probably be a better platform to do the job.

ORIGINAL POST (Jun 20, 2011)

This is a simple Apps script to share a group of contacts among some users within your Google Apps domain or just any user with a Google account, if you don't want to buy an app from Google Apps Marketpalce.

Here's how to use it.

1. Create a new spreadsheet, go to Tools > Script Gallery, search for "shared contact groups" and click on "install" button. If you can't find it just download this text file and paste its content into the script editor window:

2. After you install the script you should be able to see an additional menu called "Contacts". Use it to start sharing an existing group of your contacts:

Select a group you want to share and click on "continue"

3. The script will create a new sheet named after the contacts group and populate it with contacts of that group:

4. That's it. Now, share the spreadsheet with someone else and tell them to go Contacts > Sync with my contacts:

5. Keep it synchronized! Go to
Tools > Script editor and then Triggers > Current script's triggers:

Now, add a time-driven trigger keep the contacts in sync with other users you've shared the spreadsheet with:

Let me know if you have any troubles.