1

I want to take a spreadsheet of e-mails names and either A) add them as new contacts or B) update existing contacts to a specific Group/Label in my contacts list. I got the "add" part down, and I'm able to retrieve things like the Person ID and such. But for updating people already existing in my contacts, I'm getting stuck.

I have a simple function (based on Google's documentation) to update a contact by Person ID and Label/Group ID. Whenever I execute it, it throws this error:

Exception: Invalid number of arguments provided. Expected 2-3 only

I've tried to follow the Google People API documentation, but it's a bit difficult for me since the examples are in JavaScript and I'm trying to use this in an Apps Script (and I'm no pro at either).

My main code is below for reference, where personID is a string that follows the people/IDNUMBER format, and groupLabel is a string in the contactGroups/GROUPIDNUMBER format:

function addContactToGroup(personID, groupLabel) {
    Logger.log("Updating: " + personID + " --> " + groupLabel);
    return People.People.updateContact({
      "resourceName": personID,
      "personFields": "memberships",
      "updatePersonFields": "memberships",
      "resource": {
        "memberships": [
          {
            "contactGroupMembership": {
              "contactGroupResourceName": groupLabel
            }
          }
        ]
      }
    });
}

EDITED CODE:

Answering my own question (sort of) because I did a bit of digging and found this thread.

But now I'm having issues with "etag." Is there a specific way I can get the etag to pass into the function? My updated (semi-working) code is:

function modifyContact(person, label) {
    Logger.log("Updating: " + person + " --> " + label);
    return People.People.updateContact({
        "resourceName": person,
        "etag": etag,
        "memberships": [
            {
            "contactGroupMembership": {
            "contactGroupResourceName": label
            }
          }
        ]
    },
        person,
        {updatePersonFields: "memberships"});
}
  • First, I apologize that my answer was not useful for your situation. From your question, I proposed a modified script as an answer. Please confirm it. If I misunderstood your question and that was not useful, I apologize again. – Tanaike Aug 01 '23 at 05:14

1 Answers1

1

I believe your goal is as follows.

  • You want to work your function modifyContact using the valid value of etag.

In this case, how about retrieving etag using Method: people.get? When this is reflected in your script, it becomes as follows.

Modified script:

function modifyContact(person, label) {
  const { etag } = People.People.get(person, { personFields: "memberships" });
  return People.People.updateContact({
    "resourceName": person,
    "etag": etag,
    "memberships": [
      {
        "contactGroupMembership": {
          "contactGroupResourceName": label
        }
      }
    ]
  },
    person,
    { updatePersonFields: "memberships" });
}
  • When this script is run with the valid values of person, label, the label of person is changed to label.

Note:

  • In this modification, it supposes that your values of person, label are valid values, and also, you have permission for updating it. Please be careful about this.

Reference:

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Thank you! I was able to find the solution for etag using the people.get method. I structured my code differently from yours but I was able to use the get method to make it work. I tested it and my function updated the contact successfully! Now my only other difficulty is that it **overwrote** the existing label on the contact. I can't find any way to simply **add* the new label, vs. overwriting any existing label(s) with the new one. Would you suggest that I first retrieve the existing labels with the people.get method, and then pass the existing + new labels to people.updateContact? – Corinne Jacobo Aug 01 '23 at 05:31
  • @Corinne Jacobo Thank you for replying. From `Thank you! I was able to find the solution for etag using the people.get method. I structured my code differently from yours but I was able to use the get method to make it work. I tested it and my function updated the contact successfully!`, I understood that your question was resolved. – Tanaike Aug 01 '23 at 05:34
  • @Corinne Jacobo About your new question of `Now my only other difficulty is that it overwrote the existing label on the contact. I can't find any way to simply *add the new label, vs. overwriting any existing label(s) with the new one. Would you suggest that I first retrieve the existing labels with the people.get method, and then pass the existing + new labels to people.updateContact?`, I would like to support you. – Tanaike Aug 01 '23 at 05:34
  • @Corinne Jacobo But the issue of replying is new issue, and that is different from your question. So can you post it as new question? Because when your initial question is changed by comment, other users who see your question are confused. By posting it as new question, users including me can think of it. At that time, [please close this question](https://stackoverflow.com/help/someone-answers). If you can cooperate to resolve your new issue, I'm glad. Can you cooperate to resolve your new question? – Tanaike Aug 01 '23 at 05:34
  • I understand, thank you. I'll ask a new question regarding this specific issue. – Corinne Jacobo Aug 01 '23 at 05:36
  • @Corinne Jacobo Thank you for your response. When you post a new question, I would like to check it. – Tanaike Aug 01 '23 at 05:38