How to add new contacts using Google Apps Script (example code)

Suppose you have a Google Sheet full of contacts. You want to add some of those contacts to your Google/Gmail Contacts. This article has all the sample apps script code to do it.

By the way having all your contacts neatly organized in your Google Contacts list has several advantages. You can create groups, send emails to that groups. Export and import contacts to the group and so on.

You can make a copy of the sample Google Sheet along with the sample code here:
Sample Google Sheet

Go to the menu item Tools → Script Editor

Let us first add a new menu item to add contacts from this sheet.

Add a menu

The code adds a menu item Contacts → add Contact

Now we need to add a handler to handle when you click on that menu item.

Add a contact from the Google sheet

The following code first picks up the contact from the currently selected cell.
As you can see from the sample Google Sheet, the first column is the First name, second column the last name and third column is the email.
We pick the values and call ContactsApp.createContact()

Select File → Save. Then Select the onOpen function from the dropdown and press run.
This is to enable the authorizations to run the script.

Close and re-open the Google Sheet. wait till the new Contacts menu appears.
Select a cell. Then choose menu item: Contacts → add Contact

( If you want to try the script without adding the contact first, just remove the createContact line from the script and then try again )

Where is the new contact?

If you open you still will not see the new contact yet. There was no error too. So where did the new contact disappear?

The reason is that the new contact is not a member of any of your contact groups. There are predefined groups called “system groups” in every account.

Let us first add a function to list all the groups.

Now let us add a menu item to show the contact groups:

Do File → save and re-open your Google Sheet.

Now your Contacts menu should have tow items. Select Contacts → show Groups.

It will show all the contact groups in your account

Let us add your new contacts to “My Contacts” group.

Here is the updated code:

Now try adding another contact and it should appear in the main listing when you go to

Sample Sheet

Copy the sample sheet here

Leave a Reply 0 comments

Leave a Reply: