How to get the Label or Group of a Contact

58 Views Asked by At

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);
  }
}
1

There are 1 best solutions below

0
TheWizEd On

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 function getContact().

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.

function test() {
  try {
    let group = getGroup("My Label");
    console.log(group);
    group = group.resourceName
    console.log(group)
    createContact(group);
  }
  catch(err) {
    console.log("Error in test: "+err)
  }
}

function getGroup(name) {
  try {
    let groups = People.ContactGroups.list();
    let group = groups.contactGroups.find( value => {
        return ( value.name === name );
      }
    );
    return group;
  }
  catch(err) {
    console.log("Error in getGroups: "+err);
  }
}

function getContact(contact) {
  try {
    const connections = People.People.Connections.list('people/me', {
        pageSize: 10,
        personFields: 'names,emailAddresses,memberships'
        // use other query parameter here if needed.
      }
    );
    contact = connections.connections.find( person => {
        return ( person.names[0].displayName === contact );
      }
    );
    if( contact ) {
      let group = contact.memberships.find( membership => {
          return ( membership.contactGroupMembership.contactGroupResourceName !== "contactGroups/myContacts" );
        }
      );
      return group;
    }
  }
  catch(err) {
    console.log("Error in getContact: "+err);
  }
}

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;
      }
      person.memberships.forEach( membership => {
          console.log(JSON.stringify(membership.contactGroupMembership));
        }
      );
      console.log(person);
      //console.log(person.names[0].displayName);
    });
  }
  catch(err) {
    console.log("Error in getContacts: "+err);
  }
}

function createContact(group) {
  try {
    let request = {};
    request.names = [ { givenName: "My", familyName: "Test" } ];
    request.emailAddresses = [{ value: "[email protected]", type: "home", displayName: "My Test" } ];
    let contactGroupResourceName = { contactGroupResourceName: group };
    let contactGroupMembership = { contactGroupMembership: contactGroupResourceName };
    let memberships = [ contactGroupMembership ];
    request.memberships = memberships;
    People.People.createContact(request);
  }
  catch(err) {
    console.log("Error in getContacts: "+err);
  }
}