GOAL:
What i want to achieve is that i can pick in a list of resourceNames a specific resourceName and when i run the function in the (special) menu of my GoogleSheet, the selected resourceName is used to delete the person in the Google Contacts
person in googlesheet
I have activated the Peopleapi as a service - in the code editor.
When i run the function i get this message: "person with the resourceName "xxx" does not exits in "
I wrote these functions, i expected that the person with the corresponding resourceName woule be deleted from the GoogleContacts.
`function deletePerson() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var resourceNameColumn = 1; // Column A
var resourceNameRow = 2; // Start row 2
// get the resourceName form the cell in the sheet
var resourceName = sheet.getRange(resourceNameRow, resourceNameColumn).getValue();
// var resourceName = "person/c4736325340303186435"
//"person/c4736325340303186435";
// Search for the person with the given resourceName in the People API
var contactPerson = searchContactPerson(resourceName);
if (contactPerson) {
// if the person is found, get the unstructuredName en delete the person
var unstructuredName = contactPerson.names[0].unstructuredName;
deleteContactPersoon(contactPerson.resourceName);
// Give a message to the user that the person is deleted
Browser.msgBox('Succes', 'Person with unstructuredName "' + unstructuredName + '" is deleted.', Browser.Buttons.OK);
} else {
// Giver a message to the user if the person is not found
Browser.msgBox('Not Okay', 'Person with the resourceName "' + resourceName + '" does not exists als contactperson.', Browser.Buttons.OK);
}
}
// Function to find the contactperson by means of the resourceName
function searchContactPerson(resourceName) {
// var contacten = People.People.Connections.list('people/me').connections;
var contacts = People.People.Connections.list('people/me', {
//personFields: 'names,emailAddresses,addresses,resourceName,birthdays,phoneNumbers,urls,organizations,biographies'
personFields: 'names'});
for (var i = 0; i < contacts.length; i++) {
if (contacts[i].resourceName == resourceName) {
return contacts[i];
}
}
return null; // Return null if person is not found
}
// Function to delete the contactperson by means of the resourceName
function deleteContactPerson(resourceName) {
People.People.Connections.deleteContact(resourceName);
}`

Modification points:
In the case of "Method: people.connections.list", the property of
resourceNameis in an arrayconnections. So, in this case, your functionsearchContactPersonis required to be as follows. I guessed that the reason for your 1st issue is due to this.In your showing script,
deleteContactPersoonofdeleteContactPersoon(contactPerson.resourceName)is not declared. So, when your script is run, an error occurs at this line. I think that this is your 2nd issue.In order to delete a contact,
People.People.Connections.deleteContact(resourceName)is required to be modified toPeople.People.deleteContact(resourceName);. I think that this is your 3rd issue.In order to search the contact using
resourceName, I thought that "Method: people.getBatchGet" might be able to be used. In this case, it is not required to use a loop.Modified script 1:
When your showing script is modified it becomes as follows.
resourceNameis existing, the contact ofresourceNameis deleted. Please be careful about this.Modified script 2:
As another approach, when the method for searching a contact using
resourceNameis changed, it becomes as follows.References:
Added:
From the following reply,
I understood that your value of
sheet.getRange(resourceNameRow, resourceNameColumn).getValue()is likeperson/c###. Unfortunately, this cannot be directly used with People API. I think that this is the reason for your current issue. In this case, it is required to convertperson/c###topeople/c###. When this is reflected in my 2nd script, it becomes as follows.Sample script:
Please run the function
deletePerson. When this script is run,person/c###is converted topeople/c###and the contact is deleted usingpeople/c###.