admin管理员组

文章数量:1310455

I have created a script that reads contact's info from a sheet and create a new contact in Google contacts. It works fine and end by saving in a second sheet the external contact ID, Resource name and etag. The idea is to reused this lookup information to find contacts that might not have an email or a phone number at first. (They will always at least have a first name and a physical address.)

ID Personne Appsheet    Ressource name  etag
CF32F4F6    people/c184383266205945415  %EigBAgMEBQYHCAkKCwwNDg8QERITFBUWFxkfISIjJCUmJy40NTc9Pj9AGgQBAgUHIgxMNlNjOFJMcG55TT0=

When I try to update a contact's info the updateContact function fails and I am to rookie at this to find the obvious.

It seems like this line fails: const contact = People.People.getContact(resourceName);

function updateContact(personData) {
  const spreadsheetId = "1NNkW8joHAWXgatSXyAu0O4fjPgkgYeZVjx4s9KZkAZw"; // Replace with your spreadsheet ID
  const ss = SpreadsheetApp.openById(spreadsheetId);

  const lookupSheet = ss.getSheetByName("Mapping Google Contacts");
  let resourceName = findContactByAppsheetId(personData.appsheetId, lookupSheet);
  Logger.log("resourceName : " + resourceName)


  Logger.log("resourceName (before prefix): " + resourceName);

  if (resourceName) {
    try {
      Logger.log("getcontact");
      const contact = People.People.getContact(resourceName);
      Logger.log("getcontact after " + contact);

      if (contact) {
        Logger.log("Contact found");
        const updatedContact = { // Create a *new* contact object
          names: [{
            givenName: personData.firstName || (contact.names && contact.names.length > 0 ? contact.names[0].givenName : ""),
            familyName: personData.lastName || (contact.names && contact.names.length > 0 ? contact.names[0].familyName : "")
          }],
          phoneNumbers: [], // Initialize phoneNumbers as an empty array
          emailAddresses: [], // Initialize emailAddresses as an empty array
          anizations: [], // Initialize anizations as an empty array
          addresses: [], // Initialize addresses as an empty array
          birthdays: contact.birthdays ? [...contact.birthdays] : [],
          user_defined: contact.user_defined ? {...contact.user_defined} : {}
        };
        Logger.log("updatedContact created");

Any help on how I can fix this will be appreciated.

I have created a script that reads contact's info from a sheet and create a new contact in Google contacts. It works fine and end by saving in a second sheet the external contact ID, Resource name and etag. The idea is to reused this lookup information to find contacts that might not have an email or a phone number at first. (They will always at least have a first name and a physical address.)

ID Personne Appsheet    Ressource name  etag
CF32F4F6    people/c184383266205945415  %EigBAgMEBQYHCAkKCwwNDg8QERITFBUWFxkfISIjJCUmJy40NTc9Pj9AGgQBAgUHIgxMNlNjOFJMcG55TT0=

When I try to update a contact's info the updateContact function fails and I am to rookie at this to find the obvious.

It seems like this line fails: const contact = People.People.getContact(resourceName);

function updateContact(personData) {
  const spreadsheetId = "1NNkW8joHAWXgatSXyAu0O4fjPgkgYeZVjx4s9KZkAZw"; // Replace with your spreadsheet ID
  const ss = SpreadsheetApp.openById(spreadsheetId);

  const lookupSheet = ss.getSheetByName("Mapping Google Contacts");
  let resourceName = findContactByAppsheetId(personData.appsheetId, lookupSheet);
  Logger.log("resourceName : " + resourceName)


  Logger.log("resourceName (before prefix): " + resourceName);

  if (resourceName) {
    try {
      Logger.log("getcontact");
      const contact = People.People.getContact(resourceName);
      Logger.log("getcontact after " + contact);

      if (contact) {
        Logger.log("Contact found");
        const updatedContact = { // Create a *new* contact object
          names: [{
            givenName: personData.firstName || (contact.names && contact.names.length > 0 ? contact.names[0].givenName : ""),
            familyName: personData.lastName || (contact.names && contact.names.length > 0 ? contact.names[0].familyName : "")
          }],
          phoneNumbers: [], // Initialize phoneNumbers as an empty array
          emailAddresses: [], // Initialize emailAddresses as an empty array
          anizations: [], // Initialize anizations as an empty array
          addresses: [], // Initialize addresses as an empty array
          birthdays: contact.birthdays ? [...contact.birthdays] : [],
          user_defined: contact.user_defined ? {...contact.user_defined} : {}
        };
        Logger.log("updatedContact created");

Any help on how I can fix this will be appreciated.

Share Improve this question asked Feb 3 at 14:08 Martin LeblancMartin Leblanc 112 bronze badges 1
  • Welcome to Stack Overflow! Is this your full code? The provided script appears incomplete; please share your full code. – leylou Commented Feb 3 at 14:52
Add a comment  | 

2 Answers 2

Reset to default 1

The issue is that you're using People.People.getContact(), which does not exist. Instead, you should use People.People.get() to retrieve a contact.

So try this:

const contact = People.People.get(resourceName, {
  personFields: 'names,emailAddresses,phoneNumbers,addresses,anizations,birthdays,user_defined'
});

Instead of:

const contact = People.People.getContact(resourceName);

I was also missing this line in the updatedContact declaration in order for the People.People.updateContact() to work.

etag: contact.etag

      phoneNumbers: [], // Initialize phoneNumbers as an empty array
      emailAddresses: [], // Initialize emailAddresses as an empty array
      anizations: [], // Initialize anizations as an empty array
      addresses: [], // Initialize addresses as an empty array
      birthdays: contact.birthdays ? [...contact.birthdays] : [],
      etag: contact.etag

本文标签: How to update a Google contactStack Overflow