Reputation: 11
I'm trying to make a script which take a group's email in input and parse all members to add them in a CSV document.
The Problem is that some of my groups contain other groups so i have to call recursively my function. And to call it recursivelly i need to test the email address to know if its a user or a group. Here the code:
var USERS = new Array();
var INDEX = 0;
function listAllUsersInGroup(email) {
var temporaryObjectListMember = GroupsManager.getGroup(email).getAllMembers();
for (z=0; z<temporaryObjectListMember.length; z++) {
try {
var group = GroupsManager.getGroup(temporaryObjectListMember[z]);
}
catch (err2){Logger.log(err2)}
if (group != null) { listAllUsersInGroup(group.getId());}
else {
try {
var user = UserManager.getUser(temporaryObjectListMember[z].substring(0,temporaryObjectListMember[z].lastIndexOf('@')));
} catch(err) { Logger.log(err) }
if (user != null) {
USERS[INDEX] = user.getEmail();
INDEX++;
}
}
}
}
I call this function with a main:
function main() {
var email = "[email protected]";
listAllUsersInGroup(email);
}
When the group contain users and an another group, it broke with an "Unexpected exception upon serializing continuation" error.
The try catch does'nt seem to work in this case.
One solution could be to test the type of the email (user, group, alias, ...) but i haven't find how to do this.
Thanks Jérémie BECOUSSE
Upvotes: 1
Views: 235
Reputation: 3337
after little modifications of your script it seems to be correctly working.
when you create a google group the list of the members will automatically be cut of all the aliases so there is no problem with those. you have to check if you are facing other groups Or email adresses that can be from 2 types: email from your domain --> you can find it out with the user manager function email from outside your domain --> I dont believe there is a way to know if they are valid before sending something to those
the next code will return in "USERS" users from your domain and in "EXTUSER" user that are not from your domain. (run testingIt())
function listAllUsersInGroup(email) {
var temporaryObjectListMember = GroupsManager.getGroup(email).getAllMembers();
for (var z in temporaryObjectListMember) {
var member = temporaryObjectListMember[z];
try {
var group = GroupsManager.getGroup(member);
}
catch (err2){
//Logger.log("is not group: "+err2);
}
if (group != null) {
Logger.log(member+" is a group");
listAllUsersInGroup(member);
}
else {
try {
var user = UserManager.getUser(member.split('@')[0]);
} catch(err) {
//Logger.log("it's not a known mail: "+err);
EXTUSERS.push(member);
}
if (user != null) {
USERS.push(member);
}
}
}
Logger.log("end of the group");
}
var EXTUSERS = [];
var USERS = [];
function testingIt(){
listAllUsersInGroup("[email protected]");
Logger.log("recognized users are: "+USERS);
Logger.log("unrecognized users are: "+EXTUSERS);
}
Upvotes: 1