Monday, December 9, 2013

Google App Script: How to pull all Google contacts in Google Spreadsheet?

Today I will share a simple Google App Script that we can use to pull all Google contacts in a Google Spreadsheet. You can use store this spreadsheet to your Google Drive as a backup of all your personal or professional contacts.

To do so let us create a Google Spreadsheet with the following four columns – Contact Name, Email Address, Phone Number and Address.

Next we need to go to Tools -> Script Editor. The Script editor window will open. We can delete all the code by default written on the script editor window. Let us add the following code in the Script editor.

function readAllContacts()
{
    var row =2;
    var mySpredSheetFile = SpreadsheetApp.getActiveSpreadsheet();    
  
    var allContacts=ContactsApp.getContacts();     
  
    for (var i = 0; i < allContacts.length; i++) 
    {
      
      mySpredSheetFile.getRange('A'+row).setValue(allContacts[i].getFullName());
      
      var emailAddress=allContacts[i].getEmails();
      for (var j in emailAddress) 
      {
        mySpredSheetFile.getRange('B'+row).setValue(emailAddress[j].getAddress());
        
      }
      
      var emailPhone=allContacts[i].getPhones();
      for (var j in emailPhone) 
      {
        mySpredSheetFile.getRange('C'+row).setValue(emailPhone[j].getPhoneNumber());
        
      }
      
      
      var emailAdd=allContacts[i].getAddresses();
      for (var j in emailAdd) 
      {
        mySpredSheetFile.getRange('D'+row).setValue(emailAdd[j].getAddress());
        
      }
      
         
      row=row+1; 
      
    }
  
}

From the Script editor window we need to click on Run. The script will run successfully and the spreadsheet will be populated with all the data from our Google Contacts.

For more Google App Script visit the Google App Script section.

Popular Posts

Real Time Web Analytics