Carré d'As
Carré d'As

Reputation: 1

API People from GOOGLE in APPS script (search by label)

How can i do to have in apps script the list of email of my contacts filtered with a specific label?

il obtained with the people API the list of the adresses but i cannot filter by label. There is not any function to extract the labeyour textl of the objet contact. const contacts = People.People.Connections.list('people/me', { personFields: 'names,emailAddresses' }); Thank you for your answers Cordialy

Upvotes: 0

Views: 696

Answers (2)

Cooper
Cooper

Reputation: 64040

This is how I display mine:

function displayCurrentContacts() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName('Contacts');
  sh.clearContents();
  const vs = [['Name', 'Emails']];
  const resp = People.People.Connections.list('people/me', { personFields: "emailAddresses,names,organizations" });
  //Logger.log(resp);
  const data = JSON.parse(resp);
  let m = 0;
  let n = 0;
  data.connections.forEach((ob1, i) => {
    if (ob1.emailAddresses && ob1.emailAddresses.length > 0) {
      let emails = [... new Set(ob1.emailAddresses.map(ob2 => ob2.value))];//used set to insure I get unique list
      //let emails = ob1.emailAddresses.map(ob2 => ob2.value);
      let name;
      m += emails.length;
      if (ob1.names && ob1.organizations) {
        name = ob1.names[0].displayName + '\n' + ob1.organizations[0].name;
        ++n;
      } else if (ob1.names) {
        name = ob1.names[0].displayName;
        ++n;
      } else if (ob1.organizations) {
        name = ob1.organizations[0].name;
        ++n;
      }
      vs.push([name, emails.sort().join('\n')])
    }
  });
  vs.push([n, m])
  sh.getRange(1, 1, vs.length, vs[0].length).setValues(vs)
  sh.getRange(2, 1, sh.getLastRow() - 2, sh.getLastColumn()).sort({ column: 1, sortAscending: true });
}

Upvotes: 0

Tanaike
Tanaike

Reputation: 201358

I believe your goal is as follows.

  • You want to retrieve the contacts by searching with a label.
  • You want to achieve this using Google Apps Script.

In this case, I guessed that the following flow might be required to be run.

  1. Retrieve the label list using People.ContactGroups.list.
  2. Retrieve the resource name of the label.
  3. Retrieve the member resource names using the retrieved resource name with People.ContactGroups.get.
  4. Retrieve all contacts.
  5. Filter the retrieved contacts using the member resource names.

When this flow is reflected in a sample script, it becomes as follows.

Sample script:

Before you test this script, please set labelName, and enable People API at Advanced Google services.

function myFunction() {
  const labelName = "sample label name"; // Please set your label name.

  // 1. Retrieve the label list using People.ContactGroups.list.
  const { contactGroups } = People.ContactGroups.list({ groupFields: "memberCount,name", pageSize: 1000 });

  // 2. Retrieve the resource name of the label.
  const obj1 = contactGroups.find(({ name }) => name == labelName);
  if (!obj1) {
    console.error(`Label ${labelName} was not found.`);
    return;
  }
  const { resourceName, memberCount } = obj1;

  // 3. Retrieve the member resource names using the retrieved resource name with People.ContactGroups.get.
  const { memberResourceNames } = People.ContactGroups.get(resourceName, { maxMembers: memberCount });

  // 4. Retrieve all contacts.
  const contacts = People.People.Connections.list('people/me', { personFields: 'names,emailAddresses', pageSize: 1000 });

  // 5. Filter the retrieved contacts using the member resource names.
  const res = contacts.connections.filter(({ resourceName }) => memberResourceNames.includes(resourceName));
  
  console.log(JSON.stringify(res, null, 2)); // Result value is shown in the log.
}
  • When this script is run, the contacts of labelName are obtained.

Note:

  • This is a simple sample script. If in your actual situation, the number of contact groups and/or contacts is more than 1000, please retrieve all values using pageToken.

References:

Upvotes: 0

Related Questions