问题
I'm working on a script which consist in get members from a group, the script identifies if the mail whether is a group email or a member email, but, in my job, we have groups in groups !
So here's the script
function getGroupUsers() {
var group = sheet.getActiveRange().getValue();
var members = AdminDirectory.Members.list(group).members;
for (var i = 1; i < members.length; i++) {
var member = members[i];
var caseUser = sheet.getRange(i+1, 1);
var caseGroupe = sheet.getRange(i+1, 2);
var lastRange = sheet.getRange(sheet.getLastRow(), 3);
// var caseGroupe2 = sheet.getRange(i+1, 4);
switch (member.type) {
case 'USER':
Logger.log('%s is a user', member.email);
caseUser.setValue(member.email);
break;
case 'GROUP':
Logger.log('%s is a group', member.email);
//var group = GroupsApp.getGroupByEmail(member.email);
caseGroupe.setValue(member.email);
break;
default:
Logger.log('nothing');
}
That's workin, as groups going in the second column, but i need them to be auto extended , like what I'm doing with the loop in the begining, but I'm not sure how can it works, I tried loops in loops but when I get members of the group there's only one written in the case instead of all.
If you need more information, or you need me to explain a point you're not sure to understand just tell me
回答1:
I modified your code a little bit. The thing that you mentioned about doing a loop inside a loop was correct.
I did something similar but instead of having a loop inside a loop, i created a new function, and then call it recursively. This was to handle the case where a group would have another group, and that other group would have another group as member.
function getGroup(){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var cell = sheet.getActiveCell();
var group = sheet.getActiveCell().getValue();
var subgroup = 1;
getMembers(group, subgroup, cell, sheet);
cell.activate();
}
function getMembers(group, subgroup, cell, sheet)
{
var col = cell.getColumn() +subgroup;
var row = cell.getRow() +1;
try{
var members = AdminDirectory.Members.list(group).members;
if(!members)
{
return 0;
}
}
catch (e)
{
return;
}
var offset = 0;
for(var member in members){
var newcell = sheet.getRange(row++,col);
newcell.setValue(members[member].email);
if(members[member].type == 'GROUP'){
newcell.activate();
offset = getMembers(members[member].email,subgroup , sheet.getActiveCell() ,sheet);
row = row + offset;
}
}
return member.length + offset;
}
I hope this helps in what you intended to do.
来源:https://stackoverflow.com/questions/32883992/extract-group-emails-from-group-emails-groups-in-groups-apps-script-and-then-e