Pages

Friday, August 9, 2013

Read and Display Google Contacts on Spreadsheet

You can use the following script to Read Contacts from your Google Account and Display it to your Spreadsheet:

/////////////////////////////////

function onOpen()
{
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var menuEntries = [];
  menuEntries.push( {name: "Read Contacts", functionName: "readContacts"} );
  spreadsheet.addMenu("Contacts", menuEntries);
};


function readContacts() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Sheet1");
  
  sheet.clear();
  var contacts = ContactsApp.getContacts();
  var ContactArray  = new Array();
  var ContactArrays = [];
  
  ContactArray = [];
  ContactArray.push("FullName");
  ContactArray.push("Email");
  ContactArray.push("Mobile");
  ContactArray.push("HomeAddress");
  ContactArray.push("HomeFax");
  ContactArray.push("HomePhone");
  ContactArray.push("WorkAddress");
  ContactArray.push("WorkFax");
  ContactArray.push("WorkPhone");
  ContactArray.push("Notes");
  ContactArray.push("Pager");
  ContactArray.push("Company");
  ContactArray.push("Job Title");
  
  ContactArrays.push(ContactArray);
  
  for (var i=0;i<contacts.length;i++)
  {
    ContactArray = [];
    ContactArray.push(contacts[i].getFullName());
    ContactArray.push(contacts[i].getPrimaryEmail());
    ContactArray.push(contacts[i].getMobilePhone());
    ContactArray.push(contacts[i].getHomeAddress());
    ContactArray.push(contacts[i].getHomeFax());
    ContactArray.push(contacts[i].getHomePhone());
    ContactArray.push(contacts[i].getWorkAddress());
    ContactArray.push(contacts[i].getWorkFax());
    ContactArray.push(contacts[i].getWorkPhone());
    ContactArray.push(contacts[i].getNotes());
    ContactArray.push(contacts[i].getPager());
    try{ContactArray.push(contacts[i].getCompanies()[0].getCompanyName());}
    catch(e){ContactArray.push("Info Not Available")}
    try{ContactArray.push(contacts[i].getCompanies()[0].getJobTitle());}
    catch(e){ContactArray.push("Info Not Available")}
    
    ContactArrays.push(ContactArray);
  }
  
  sheet.getRange(1,1,ContactArrays.length,ContactArrays[0].length).setValues(ContactArrays);
};

/////////////////////////////////



And If you are not much familiar with scripts then check out the following link:
http://igoogledrive.blogspot.com/2012/08/how-to-write-script-in-google.html 

I hope the above solution will help you, and if you need more help then please do comment below on this blog itself, I will try to help you out.


I also take up private and confidential projects:
If this blog post was helpful to you, and if you think you want to help me too and make my this blog survive then please donate here: http://igoogledrive.blogspot.com/2012/09/donate.html 

Thanks,

1 comment: