Skip to content

Instantly share code, notes, and snippets.

@jccarlin56
Created October 12, 2013 14:42
Show Gist options
  • Select an option

  • Save jccarlin56/6950705 to your computer and use it in GitHub Desktop.

Select an option

Save jccarlin56/6950705 to your computer and use it in GitHub Desktop.
Contact code that will read your spreadsheet, look up contacts by email address and add or update your My Contacts. From https://sites.google.com/site/scriptsexamples/learn-by-example/other-examples/contacts-sharing Example: http://spreadsheets.google.com/ccc?key=0AjThpih7byJUdDZlNGY3TEJPYU9VcWd0TWhaaElfNVE&hl=en
//This onOpen function adds a menu item
function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var searchMenuEntries = [ {name: "Update Contacts", functionName: "updateContacts"}];
ss.addMenu("Contacts", searchMenuEntries);
}
function updateContacts() {
//Here we get the spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
//Iterate through your spreadsheet
for (var i = 0; i < sheet.getLastRow()-1; i++) {
//call up your contacts service
//get the contact email from the ss
var contactEmail = sheet.getRange(i+2, 1, 1, 1).getValue();
var myContact = ContactsApp.getContact(contactEmail);
//check to see if the contact exits, if not add the contact     
if (myContact === null){
ContactsApp.createContact(sheet.getRange(i+2, 5, 1, 1).getValue(),sheet.getRange(i+2, 4, 1, 1).getValue(),sheet.getRange(i+2, 1, 1, 1).getValue());
}
//Update the phone numbers
if (sheet.getRange(i+2, 2, 1, 1).getValue() !== ""){
//you have to call the contact each time you make a change or there will be an etag error
myContact = ContactsApp.getContact(contactEmail);
myContact.setWorkPhone(sheet.getRange(i+2, 2, 1, 1).getValue());
}
if (sheet.getRange(i+2, 3, 1, 1).getValue() !== ""){
myContact = ContactsApp.getContact(contactEmail);
myContact.setMobilePhone(sheet.getRange(i+2, 3, 1, 1).getValue());
}
//You can add more fields to update    
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment