I got this error when I tried to run this code for People API. When I ran this code, the numbers in the sheet "contact" are added into Google Contacts though this error still showed up. When I checked in Google Contacts, the numbers already uploaded :D but this error is so annoyed. Anyone please help me to fix to remove this error, thanks

function addcontact() {
var sheetName = "contact";
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spreadsheet.getSheetByName(sheetName);

var rangeContact = sheet.getRange('A2:C501');
var rangeValues = rangeContact.getValues();

var hasData = false;

for (var i = 0; i \< rangeValues.length; i++) {
var isBlank = true;
for (var j = 0; j \< rangeValues\[i\].length; j++) {
if (rangeValues\[i\]\[j\] !== "") {
isBlank = false;
break;
}
}
if (!isBlank) {
hasData = true;
break;
}
}

if (!hasData) {
Browser.msgBox("No number to add in contacts. Check again!");
return;
}

var row = sheet.getLastRow();
var addedContactsCount = 0;
var duplicateContactsCount = 0;

for (var i = 2; i \<= sheet.getLastRow(); i++) {
 var data = {
  "names": \[{
  "givenName": sheet.getRange(i, 1).getValue().toString().replace("'", ""),
  "familyName": sheet.getRange(i, 2).getValue().toString().replace("'", ""),
}\],
  "phoneNumbers": \[{
  "value": sheet.getRange(i, 3).getValue().toString().replace("'", ""),
  "type": "mobile"
}\]
};
var str = data.names\[0\].givenName + " " + data.names\[0\].familyName;
var contacts = GetContacts(str);
var success = false;
try {
if (contacts == undefined) {
contacts = GetContacts(str);
}
if (contacts.length \> 0) {
duplicateContactsCount += contacts.length;
success = true;
}
}
catch (e) {
}
finally {
  if (!success) {
    var existingContacts = GetContacts(data.names[0].givenName + ' ' + data.names[0].familyName);
    if (existingContacts && existingContacts.length > 0) {
      for (var j = 0; j < existingContacts.length; j++) {
        var existingContact = existingContacts[j];
        People.People.deleteContact(existingContact.person.resourceName);
      }
    }
    People.People.createContact(data);
    addedContactsCount++;
  }
}

}

var message = 'Added ' + addedContactsCount + ' numbers into contact';
if (duplicateContactsCount \> 0) {
message += ' and there are ' + duplicateContactsCount + ' duplicated contacts.';
}
SpreadsheetApp.getActiveSpreadsheet().toast(message, 'COMPLETED', 20);
}

function GetContacts(str) {
return People.People.searchContacts({ "query": str, "readMask": "names,phoneNumbers" }).results;
}
0

There are 0 best solutions below