如何在Google App脚本中为用户获取所有组?

时间:2018-08-16 17:48:37

标签: google-apps-script google-admin-sdk google-groups-api

有没有办法为用户获取所有组?我的无法正常工作的代码如下;

  var pageToken = null;
  var filter= {userKey: "abc@gmail.com"}; //this is a member's email address
  do {
    var result = AdminDirectory.Groups.list(filter);
    filter.pageToken = result.pageToken;

    if(result.groups) {
      groups = groups.concat(result.groups.map(function(group) {
        return {
          name: group.name
        };
      }));
    }
  }
  while(result.pageToken);

  for(i = 0; i < groups.length; i++) {
    console.log(groups[i].name);
  }

它返回空的组数组。

1 个答案:

答案 0 :(得分:0)

代码比这更简单,无需使用令牌。 返回的值是具有多个属性的对象数组(请参阅JSON)

像这样

function myFunction() {
  var user = AdminDirectory.Groups.list({userKey:"userEmail@xxx.yyy"});
  var groupMembership = [];
  var userGroupProperties = user.groups;
  Logger.log(JSON.stringify(userGroupProperties));
  for(var n=0 ; n<userGroupProperties.length ; n++){
    groupMembership.push(userGroupProperties[n].name)
  }
  Logger.log('groupMembership = '+groupMembership);
}