Reputation: 1
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 text
l 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
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
Reputation: 201358
I believe your goal is as follows.
In this case, I guessed that the following flow might be required to be run.
When this flow is reflected in a sample script, it becomes as follows.
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.
}
labelName
are obtained.pageToken
.Upvotes: 0