1

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.

John Smith
  • 387
  • 2
  • 8
  • 24
  • Do any errors? Also remember as mentioned in the documentation: [`Domain Service`](https://developers.google.com/apps-script/reference/domain/)`- This service allows Google Apps domain administrators to create and modify groups, users, or nicknames. To use this service, you must first`[`enable the Provisioning API`](http://support.google.com/a/bin/answer.py?hl=en&answer=60757)`for your domain.` – wchiquito Nov 21 '13 at 11:34

1 Answers1

2

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)}
  }
}
Serge insas
  • 45,904
  • 7
  • 105
  • 131
  • Hi Serge, thank you fr helping me, i tried the code, first i run the function test, it works, but when i try to run the function addMembers, it fails with this error: Type error can't read the property "length" for(r=0;r – John Smith Nov 21 '13 at 12:55
  • the function addMembers is not supposed to be run alone, You have to use test (from there addMembers is called with the right parameters). If "test" worked then you're done, the groups have been updated with new members. – Serge insas Nov 21 '13 at 13:11