English 中文(简体)
利用资源Name,从谷歌剧团的一个单元中删除谷歌联系人/人
原标题:Deleting a contact/person from Google Contacts by using its resourceName - retreived from a Cell in a Google Sheet
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

“在此处的影像描述”/</a

我将Peopleapi投入使用,作为编辑。

在行使职能时,我收到以下信息:“拥有“xxx”资源的人不退出“......”。

“entergraph 我写了这些职能,即期望从谷歌中删除拥有相应资源的人。

`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:

  • “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##删除该联系。





相关问题
How to use year counter google app script?

I need an urgent help i have data as follows in a google sheet i need to add a year counter to the column d as follows using google apps script

Google Spreadsheet multiple column filter using OR

I have a Google Spreadsheet with 3 columns that are either blank or have a value. I want to get the count of the number of rows that has A and either B or C populated. If I were writing a SQL query ...

Run Google Apps Script on Google-Spreadsheet event?

If I create a Google apps script, can I hook it up to Google spreadsheet to run based on an event, or must I manually invoke it? I don t know if Google Sheets supports any events. For example: a ...

How do I insert a row in to a Google spreadsheet using c#

I ve seen; Accessing Google Spreadsheets with C# using Google Data API and http://code.google.com/apis/spreadsheets/data/2.0/developers_guide_dotnet.html#CreatingRows However i m still having ...

using Zend_Gdata_Spreadsheets for public spreadsheets?

I have this code which is working, to load a Google Spreadsheet and load some data from it. If the spreadsheet in question is public, how do i modify the code to not require a username/password? $key=...

Programatically updating a Google spreadsheet

I ve got a pre-existing Google spreadsheet. Each month I update this document. I ve got a template workseet in the spreadseet that I d like to clone and then update. I d prefer to clone the ...

热门标签