google-sheetsgoogle-contacts-api

Exporting from Google Sheets to Google Contacts


I'm trying to add contacts using a spreadsheet and a script.

So far I was able to copy the script from this user: How to sync google sheets database with google contacts

  var ss = SpreadsheetApp.getActiveSpreadsheet()
  var sheet = ss.getSheetByName('Respostas ao formulário 1')
  var headerRows = 1;
  var MaxRow = sheet.getLastRow();
  var dataRange = sheet.getDataRange();
  var data = dataRange.getValues();     
  data.splice(0, headerRows);           

  function addContact() {
    for (var i = 0; i < data.length; i++) {
      var row = data[i];
      var firstName = row[1];
      var lastName = row[2];
      var email = row[3];
      var phone = row[4];
      var group = row[5];
      var job = row[6];
      var address2 = row[7];
         
      contact = ContactsApp.createContact(firstName, lastName, email);
      contact.addPhone(ContactsApp.Field.MOBILE_PHONE, phone);
      
      contact.setNotes(address2)
      contact.addCompany('', job)
      
      var group = ContactsApp.getContactGroup("System Group: My Contacts");
      group.addContact(contact);
   
    }

  }

It works great, but I can't find out how to add age or to automatically add a label to the contact. Any help would be awesome


Solution

  • You need to create a custom field. Within your code if you add this line I think you'll get what you need:

    contact.addCustomField("Age", '26');