2022-02-08

Tech Programing

程式人小天地

Add contact if contact does not exist under google contacts with “ContactsApp.getContact”

3 min read


I found the following code as a response to the same question at Check if contact exists under google contacts with "ContactsApp.getContact" but am having some trouble. I am trying to create a Google Script that checks to see if a respondent to a Google Survey already has a contact card in ‘System Group: My Contacts’ and if not, create a contact card and then add their phone number.

I’ve updated the following code to reference my fields, but am receiving an error for the line if(emailjson[email.toLowerCase()].id) – which is meant to check to see if that contact existed:

TypeError: Cannot read property "id" from undefined. (line 36, file "Code")

I thought that there was a problem with the IF statement, but I’m not completely familiar with Javascript (or JSON). As an Excel and Access person I’m accustomed to IF statements having a IF something is null, then do this, which so I tried adding === undefined, changing the code to if(emailjson[email.toLowerCase()].id === undefined) but that didn’t solve the problem.

If I remove new contacts from my spreadsheet and go back to the original code (without === undefined), then I make it past that line, but receive an error on the next line, if(!emailjson[email.toLowerCase()]['phones'][phone.replace(/[_)(s.-]/g,'')]) {.

TypeError: Cannot find function replace in object 2024313437. (line 37, file "Code")

If I add some of those characters to the phone numbers, instead of already having them stripped, then the error goes back to line 36.

Thanks in advance for your thoughts.

function emailsasJSON() {
  
 var emailjson = {}
  var myContacts = ContactsApp.getContactGroup('System Group: My Contacts').getContacts();
  for (var i = 0; i < myContacts.length; i++) {
    var emails = myContacts[i].getEmails();
    var phonesobj = myContacts[i].getPhones();
    var phones = {}
    for (var j = 0; j < phonesobj.length; j++) {
       phones[phonesobj[j].getPhoneNumber().replace(/[_)(s.-]/g,'')] = 1;
    }
    for (var j = 0; j < emails.length; j++) {
      emailjson[emails[j].getAddress().toLowerCase()] = {id: myContacts[i].getId(), phones:  phones};
      
    }
  }
  Logger.log(JSON.stringify(emailjson))
  return emailjson;
}

function addClient() {
 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sheetNew = ss.getActiveSheet(); 

 var clientsgroup = ContactsApp.getContactGroup('System Group: My Contacts')

//this is where we will insert the function from above to get the emailjson obj
 var emailjson = emailsasJSON()

 var contactarray = sheetNew.getDataRange().getValues();
 for (var i = 1 ; i < contactarray.length; i++){
   var name = contactarray[i][1]
   var email = contactarray[i][4]
   var phone = contactarray[i][3]
   
   if(emailjson[email.toLowerCase()].id) { //check if email exists
     if(!emailjson[email.toLowerCase()]['phones'][phone.replace(/[_)(s.-]/g,'')]) { //if email exists but phone doesn't, add phone
         
     ContactsApp.getContactById(emailjson[email.toLowerCase()].id).addPhone(ContactsApp.Field.MOBILE_PHONE, phone)
     emailjson[email.toLowerCase()]['phones'][phone.replace(/[_)(s.-]/g,'')] = 1; //add it to the emailjson object in case there are more iterations of this contact in the sheet
     } 
   } else { //add new contact if it doesn't exist
   
    var newcontact = ContactsApp.createContact(name.split(' ')[0],name.split(' ')[1], email)
    newcontact.addPhone(ContactsApp.Field.MOBILE_PHONE, phone)
    emailjson[email.toLowerCase()]['id'] = newcontact.getId();
    emailjson[email.toLowerCase()]['phones'][phone.toString().replace(/[_)(s.-]/g,'')] = 1;
    clientsgroup.addContact(newcontact)
     }
  }
}



Source link

資料來源:Stackoverflow

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *