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
我已在代码编辑器中激活 Peopleapi 作为服务。
当我运行该函数时,我收到此消息:“具有资源名称“xxx”的人不存在于“
我写了这些函数,我希望具有相应resourceName的人将从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);
}`
在“方法:people.connections.list”的情况下,
resourceName
的属性位于数组connections
中。因此,在这种情况下,您的函数searchContactPerson
需要如下所示。我猜你的第一个问题的原因就是这个。
function searchContactPerson(resourceName) {
var contacts = People.People.Connections.list('people/me', { personFields: 'names' });
for (var i = 0; i < contacts.connections.length; i++) {
if (contacts.connections[i].resourceName == resourceName) {
return contacts.connections[i];
}
}
return null;
}
在您的显示脚本中,未声明
deleteContactPersoon
的 deleteContactPersoon(contactPerson.resourceName)
。因此,当您的脚本运行时,该行会发生错误。我认为这是你的第二个问题。
要删除联系人,需要将
People.People.Connections.deleteContact(resourceName)
修改为People.People.deleteContact(resourceName);
。我认为这是你的第三个问题。
为了使用
resourceName
搜索联系人,我认为可以使用“方法:people.getBatchGet”。在这种情况下,不需要使用循环。
当你的显示脚本修改后,它会变成如下。
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();
// 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;
deleteContactPerson(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 contacts = People.People.Connections.list('people/me', { personFields: 'names' });
for (var i = 0; i < contacts.connections.length; i++) {
if (contacts.connections[i].resourceName == resourceName) {
return contacts.connections[i];
}
}
return null;
}
// Function to delete the contactperson by means of the resourceName
function deleteContactPerson(resourceName) {
People.People.deleteContact(resourceName);
}
resourceName
存在时,resourceName
的联系人将被删除。请注意这一点。作为另一种方法,当使用
resourceName
搜索联系人的方法发生变化时,就会变成如下。
function deletePerson() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var resourceNameColumn = 1; // Column A
var resourceNameRow = 2; // Start row 2
var resourceName = sheet.getRange(resourceNameRow, resourceNameColumn).getValue();
var contactPerson = searchContactPerson(resourceName);
if (contactPerson) {
deleteContactPerson(resourceName);
Browser.msgBox('Succes', 'Person with unstructuredName "' + contactPerson + '" is deleted.', Browser.Buttons.OK);
} else {
Browser.msgBox('Not Okay', 'Person with the resourceName "' + resourceName + '" does not exists als contactperson.', Browser.Buttons.OK);
}
}
function searchContactPerson(resourceName) {
var res = People.People.getBatchGet({ personFields: "names", resourceNames: [resourceName] }).responses[0];
return res.httpStatusCode == 200 ? res.person?.names[0]?.unstructuredName : false;
}
function deleteContactPerson(resourceName) {
People.People.deleteContact(resourceName);
}