Search code examples
google-apps-scriptgoogle-sheetsgoogle-groups

Is there a way to extract contact information from a google group?


I have a script which pulls all the email addresses from a google group.

I need to extract the Full name associated with each email address, this information is stored in the contacts app.

It looks like I can only extract email addresses and role information from groups, is there a way to then link this information to the contacts app to extract their Full Names?

It's worth noting that I don't have admin access so can't use Admin SDK

This is the script I'm using to get the email addresses:

function listGroupMembers() {
  var GROUP_EMAIL = "email@emailcom";
  var group = GroupsApp.getGroupByEmail(GROUP_EMAIL);
  var users = group.getUsers();
    var str = "Group " + GROUP_EMAIL + " has " + users.length +
  " members: ";
  for (var i = 0; i < users.length; i++) {
    var user = users[i];
    str = str + user.getEmail() + ", ";
  }
  Logger.log(str);
  var range = SpreadsheetApp.getActiveSheet().getActiveRange().setValue(str)
  }

I'm thinking of then connecting with a script like this, but can't seem to get it to work on a list of email addresses in a range on my sheet even if I split it out

function getUserName(){
var contacts = ContactsApp.getContactsByEmailAddress(query);
for (var i in contacts) {
  Logger.log(contacts[i].getFullName());
}}

Any help would be appreciated, I'm far from an expert on this!


Solution

  • You can connect Google Groups and ContactApp directly, without storing the email addresses in the spreadsheet

    Sample

    function listGroupMembers() {
      var GROUP_EMAIL = "email@emailcom";
      var group = GroupsApp.getGroupByEmail(GROUP_EMAIL);
      var users = group.getUsers();
      var str = "Group " + GROUP_EMAIL + " has " + users.length +
      " members: ";
      for (var i = 0; i < users.length; i++) {
        var user = users[i];
        var email = user.getEmail();
        var contact =  ContactsApp.getContact(email);
        if(contact!=null){
          Logger.log(contact.getFullName());
          str=str+contact.getFullName()+" ";
          }
        }
      }