Modification points:
“Method: people.jointions.list”, The property of resourceName
is in a range ions
. 因此,在这种情况下,您的职能如下: 我猜测,你第一个问题的原因是这样。
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。 因此,当你手脚时,就会出现错误。 我认为,这是你的第二个问题。
In order to delete a contact, People.People.Connections.deleteContact(resourceName)
is required to be modified to People.People.deleteContact(resourceName);
. I think that this is your 3rd issue.
为了利用<代码>resourceName查询联系,我认为“Method: people.getBatchGet”可能得以使用。 在这种情况下,不必使用假肢。
Modified script 1:
当你出示文字时,改为:
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);
}
- When this script is run, when
resourceName
is existing, the contact of resourceName
is deleted. Please be careful about this.
Modified script 2:
另一种做法是,如果使用<代码>资源Name查询联系的方法发生变化,则情况如下。
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);
}
References:
Added:
答复如下:
你写道,“资源价值要求是人/人/人/人/XXXXXXXXXXXXXXXXXXXXXXXX......”见:我在“第二次尝试改变人的价值”上的讲话,从谷歌联系人处获得了价值(联系人:google.com/人/cXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX)。
我的理解是,您对<代码>sheet.getRange(resourceNameRow, ResourceNameColumn).getValue()的人/c##
。 不幸的是,这不能直接用于大众宣传。 我认为,这是你目前问题的原因。 在这种情况下,必须将<代码>人/c##改为people/c##
。 我的第二稿反映了这一点。
Sample script:
// ref: https://medium.com/google-cloud/convert-contact-url-to-resource-name-for-people-api-using-google-apps-script-96457e6f9597
function convertPersonToPeople_(person) {
if (!(/^person/c[d]*$/).test(person)) {
throw new Error("Invalid value.");
}
// ref: https://stackoverflow.com/a/21668344
function dec2hex(str) { // .toString(16) only works up to 2^53
var dec = str.toString().split( ), sum = [], hex = [], i, s
while (dec.length) {
s = 1 * dec.shift()
for (i = 0; s || i < sum.length; i++) {
s += (sum[i] || 0) * 10
sum[i] = s % 16
s = (s - sum[i]) / 16
}
}
while (sum.length) {
hex.push(sum.pop().toString(16))
}
return hex.join( )
}
// Convert decimal to hexadecimal.
const hexadecimalValue = dec2hex(person.replace("person/c", ""));
// Search contact.
let res = null;
let pageToken = "";
do {
const obj = People.People.Connections.list("people/me", { personFields: "emailAddresses", pageSize: 1000, pageToken });
if (obj.connections.length > 0) {
const t = obj.connections.find(c => c.emailAddresses.some(e => e.metadata.source.id == hexadecimalValue));
if (t) {
res = t;
break;
}
}
pageToken = obj.nextPageToken;
} while (pageToken);
if (!res) return;
// Return resource name.
const { resourceName } = res;
return resourceName;
}
function deletePerson() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var resourceNameColumn = 1; // Column A
var resourceNameRow = 2; // Start row 2
var personValue = sheet.getRange(resourceNameRow, resourceNameColumn).getValue();
// Convert from person/c### to people/c###
var resourceName = convertPersonToPeople_(personValue);
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);
}
请操作<代码>deletePerson。 当使用该文字时,将<代码>人/c##改为people/c##
,并通过<编码>人/c##删除该联系。