0
votes

here is my purpose: I want to create a script that, when you select different user's group, it shows you every member of these groups. Also ( later), it will permit to show only users that are in all the selected groups.

Here is my problem: I have a variable that store one by one users that are in groups i selected. But if a user is in two group he will be stored twice. (The issue will be met again in second part of my script sooo....)

Here is my code:

function membreAssocGroupe()
{
  var email = " ";
  var  groupes = ["[email protected]","[email protected]","[email protected]"]; // this is groups i selected 

  //checking in "var groupes" all values that are registered.
   for (var g in groupes) {
     //opening the group by the e-mail and getting all the users in "var users" of the group.
     var group = GroupsApp.getGroupByEmail(groupes[g]);
     var users = group.getUsers();

     //for all the users get in "var users", we will copy their e-mail in "var e-mail"
     for (var u in users){
       //if the user e-mail is already saved in "var email", go to the next user.
       if(email.indexOf(u)>0)
       {
         continue;
       }
       //if the user e-mail is not saved, copy it in "var email".
       else
       {
       email = email +  users [u] .getEmail () + email.indexOf(u) + "  ";
        }
     }

    //to see what i get 
     var s = s + "\n" + "Group " + groupes[g] + " has " + users.length +
     " members:\n " + email + "\n \n ";
   }

     Logger.log(s);
}

As you now understand, the problem is this part(all entry get -1 in this operation):

   if(email.indexOf(u)>0)
   {
     continue;
   }

I tried many things:

if(email.toString().match(u)==u)
if(email.match(u)==u.toString())
if(email.toString().match(u.toString())==u.toString())
if(email.toString().match(u)==u.toString())

etc... (This way worked for cells in spreadsheet, but no way to make it works here)

But also findText and other functions i don't even remember.

As you can see (because of my way to explain the problem, and my shitty code :p), i'm a newbie, that tries hard, and finally succeed by learning little by little. If you can tell me the function i should use, and why its is better than others, i would appreciate it a lot.

2

2 Answers

0
votes

If I well understand you want to list all emails in all groups but don't want duplicate.

Normally this must work :

function membreAssocGroupe(){
  var emails = [];
  var  groupes = ["GROUP1","GROUP2"];
  for(var i in groupes){
    var group = groupes[i];
    var users = GroupsApp.getGroupByEmail(group).getUsers();
    for(var j in users){
      var email = users[j].getEmail();
      if(emails.indexOf(email) < 0){
        emails.push(email);
      }
    }
  }
  Logger.log(emails.join(","));
}

To simplify management I use a tab for emails but with a join you can get easily a string.

0
votes

It does work thanks.

I think i begin to understand indexOf(). Is that function tries to find a value in an array, and return -1 if no find, index number if found?

I'm French ( not used to english terms), and unexperimented, so i sometimes misunderstand things..^^.

anyway, my problem is solved. Thanks mate!

Edit: Btw, as you already understood, you were right, i wanted to copy all users' email adresses, with no duplicate. but also the only adresses that are in all groups in the same time ( but with your solution, it's now a matter of time to do it).