Add emailadress to Spreadsheet, auto add the email

2019-05-30 10:39发布

问题:

I have a Google Forms were the user writes her/his emailadress. When they do so, i would like it to auto add the emailadress/user to a Google Group.

I tried to do this with Apps Script, but i do not get any luck with it.

function addUserToGroup() 
{
  var emailadd = SpreadsheetApp.getActiveSheet().getRange("B2:B99");
  var group = GroupsManager.getGroup("grouptest@mydomain.com").addMember(emailAdd);

}

I think i might be missing something in my script, but i don't know what it is.

I am domain admin Provisioning API is enabled.

回答1:

Try it like this, I separated the process in 2 steps, one for getting the emails (test) and the other to add the users to the group.

function test(){
  var emailadd = SpreadsheetApp.getActiveSheet().getRange("B2:B99").getValues();
  var usersToAdd = [];
  for(var n=0 ; n<emailadd.length ; ++n){
    if(emailadd[n][0]!=''){
      usersToAdd.push(emailadd[n][0]);
    }
  }
  Logger.log(usersToAdd)
  var group = GroupsManager.getGroup('testgroup@domain.com');
  addMembers(group,usersToAdd);
}

function addMembers(group,usersToAdd){
  for(r=0;r<usersToAdd.length;++r){
    try{
      var memberId = UserManager.getUser(usersToAdd[r].substring(0,usersToAdd[r].indexOf('@'))).getUserLoginId();
      Logger.log('added '+memberId);
      group.addMember(memberId)
    }catch(error){Logger.log(error)}
  }
}