I'm trying to create a bunch of email contacts using Google App Script. I have a spreadsheet with a list of email address with name, address, etc. I want to loop through this list and create Contacts. However I also want them to be Grouped according to a club name.
I believe Groups are created by Labeling the email address in my contacts. How do I do that with app script. So far I have the following. Any advise appreciated.
function getContacts() {
try {
const connections = People.People.Connections.list('people/me', {
pageSize: 10,
personFields: 'names,emailAddresses,memberships'
// use other query parameter here if needed.
});
connections.connections.forEach((person) => {
// if contacts/connections is available, print the name of person.
if (person.names && person.names.length === 0) {
console.log('No display name found for connection.');
return;
}
//console.log(person);
console.log(person.names[0].displayName);
});
}
catch(err) {
console.log("Error in getContacts: "+err);
}
}
function createContacts() {
try {
let request = {};
request.names = [ { givenName: "Ed", familyName: "Test" } ];
request.emailAddresses = [{ value: "[email protected]", type: "home", displayName: "Ed Test" } ];
let contact = People.People.createContact(request);
console.log(contact);
}
catch(err) {
console.log("Error in getContacts: "+err);
}
}
It took a lot of trial and error but I figured it out. To create a new Contact and assign a group to it. I first had to create a Contact within Contacts and assign a Label to it for the Group.
Then I search through my Contacts and get the one that has the Label (that is not
contactGroupId:"myContacts"). In my case I only have one contact with a label so my method is rather limited.Finally I create a new Contact and assign the
membership.Here are all the functions that I used to get the info I needed.
Also thanks to Tanaike, who pointed me in the direction of ContactGroups, I've simplified the search for the for the group
contactGroupResourceName. I've added another functiongetContact().I would just like to point a minor limitation of my functions. My Contact list and Groups are small so no paging of list results is necessay.