将用户添加到多个组

时间:2019-07-31 20:29:24

标签: google-apps-script

我正在尝试使用Google表格在Gsuite中自动进行入职。我要自动执行组成员身份。

我想知道以下语法是否可以将用户添加到多个组:

function addGroupMember() {
var groupArray = ["x@example.com", "z@example.com"]
var userEmail = "email";
var groupEmail = groupArray;
var member = {
     email: userEmail,
     role: 'MEMBER'
   };
   member = AdminDirectory.Members.insert(member, groupEmail);
   Logger.log('User %s added as a member of group %s.', userEmail, groupEmail);
 }

1 个答案:

答案 0 :(得分:0)

您的代码几乎正确。您需要遍历groupArray一次才能进行一组操作,而且,groupEmail = groupArray;是多余的,因为您可以直接使用groupArray或将其名称更改为groupEmail

function addGroupMember() {

  var groupArray = ["group1@example.com", "group2@example.com"];
  var userEmail = "user@email.com";
  var member = {
    email: userEmail,
    role: 'MEMBER'
  };

  for (i in groupArray){

    AdminDirectory.Members.insert(member, groupArray[i]);
    Logger.log('User %s added as a member of group %s.', userEmail, groupArray[i]);
  }
}