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

function onOpen() 
{
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('Contacts')
      .addItem('add Contact', 'addContact')
      .addToUi();
}

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()

function addContact()
{
    var sheet = SpreadsheetApp.getActiveSheet();
    var cell = sheet.getActiveCell();
    var active_row = cell.getRow();
    var range = sheet.getDataRange();
  
    var first_name = range.getCell(active_row ,1).getValue();
    var last_name = range.getCell(active_row ,2).getValue();
    var email = range.getCell(active_row,3).getValue();
  
    
    var contact = ContactsApp.createContact(first_name, last_name, email);
    
    
    showOutputBox('first name '+first_name+' \nlast name '+last_name+' \nemail '+email, "added contact");
  
}

function showOutputBox(str, title)
{
  var html = HtmlService.createHtmlOutput('<pre>'+str+'</pre>')
    .setWidth(400)
    .setHeight(300);
  
    SpreadsheetApp.getUi() 
    .showModalDialog(html, title);
}

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 contacts.google.com 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.

function showContactGroups() 
{
  var groups = ContactsApp.getContactGroups();
  var str ='Groups\n';
  for(var g = 0; g < groups.length; g++) 
  {
    str +='\n'+groups[g].getName()
    
  }
  
  showOutputBox(str,'Your Contact Groups');
}

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

function onOpen() 
{
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('Contacts')
      .addItem('show Groups', 'showContactGroups')
      .addItem('add Contact', 'addContact')
      .addToUi();
}

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:

function addContact()
{
    var sheet = SpreadsheetApp.getActiveSheet();
    var cell = sheet.getActiveCell();
    var active_row = cell.getRow();
    var range = sheet.getDataRange();
  
    var first_name = range.getCell(active_row ,1).getValue();
    var last_name = range.getCell(active_row ,2).getValue();
    var email = range.getCell(active_row,3).getValue();
  
    
    var contact = ContactsApp.createContact(first_name, last_name, email);
    
    var mainGroup = ContactsApp.getContactGroup("System Group: My Contacts");
    
    mainGroup.addContact(contact);
 
  
    showOutputBox('first name '+first_name+' \nlast name '+last_name+' \nemail '+email, "added contact");
  
}

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

Sample Sheet

Copy the sample sheet here

Leave a Reply 0 comments

Leave a Reply: