Home > Mobile >  compare google group members to sheet
compare google group members to sheet

Time:08-24

My goal is to use a google sheet to add and delete members of a particular group. Adding was not an issue but I'm running into issues comparing the two arrays so that I can delete email addresses that belong to the group but not the sheet. Please let me know if there is another avenue to pursue.

I've attempted to write code that will loop through the users in the group, then another loop to get the email addresses from a sheet. The comparison works but only when there is a match on both sides. If I swap the logic to look for addresses that are missing, false positives appear as the array compares the values and they do not match.

Here is what I have so far:

function listGroupMembers() {
  var GROUP_EMAIL = "[email protected]";
  var group = GroupsApp.getGroupByEmail(GROUP_EMAIL);
  var users = group.getUsers();

  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("main list");
  var emailsFromSheet = sheet.getRange(1,7,sheet.getLastRow()-1,1).getValues();
  var group = GroupsApp.getGroupByEmail(GROUP_EMAIL);

  console.log(users.length);
  console.log(emailsFromSheet.length);
  for (i=0;i<users.length;i  ) {
    
    var user = users[i];

    for (j=0;j<emailsFromSheet.length;j  ) {
      var emailStuff = emailsFromSheet[j];
      if (user.getEmail() == emailStuff) {
          console.log(user.getEmail()   " "   emailStuff);
        }
      }
    }
  }

CodePudding user response:

Members of a Group

function listGroupMembers() {
  var GROUP_EMAIL = "[email protected]";
  var group = GroupsApp.getGroupByEmail(GROUP_EMAIL);
  var us = group.getUsers();
  const usem = us.map(u => u.getEmail()).flat();
  var sh = SpreadsheetApp.getActive().getSheetByName("main list");
  var em = sh.getRange(1,7,sh.getLastRow()).getValues().flat();
  em.forEach(e => {
    let idx = usem.indexOf(e)
    if(~idx) {
      Logger.log(`${e} is a member of ${GROUP_EMAIL}` )
    } else {
      Logger.log(`${e} is not a member of ${GROUP_EMAIL}` )
    }
  })
}

Maybe this will work:

function listGroupMembers() {
  var GROUP_EMAIL = "[email protected]";
  var group = GroupsApp.getGroupByEmail(GROUP_EMAIL);
  var us = group.getUsers();
  const usem = us.map(u => u.getEmail()).flat();
  var sh = SpreadsheetApp.getActive().getSheetByName("main list");
  var em = sh.getRange(1,7,sh.getLastRow()).getValues().flat();
  usem.forEach(e => {
    let idx = em.indexOf(e)
    if(~idx) {
      Logger.log(`${e} is a member of usem list` )
    } else {
      Logger.log(`${e} is not a member of usem list` )
    }
  })
}
  • Related