0

我正在尝试找到一种简化的方法,以使大约 100 个 Google 群组中的成员从 Google 表格工作簿中已有的信息中获得最新信息。一张表有一个包含 1700 名学生的列表,其中包含学生电子邮件和小组电子邮件的列。另一张表仅包含群组名称和群组电子邮件的列表。根据我发现的代码,谷歌应用脚​​本似乎是可行的。不幸的是,我以前没有使用 Apps 脚本的经验,也没有使用 javascript 的经验。我一直在尝试拼凑一个可能看起来像这样的过程。

  1. 删除组的所有当前成员。
  2. 上传正确的成员。
  3. 冲洗并重复。

不是最优雅的,但它似乎可行。

这就是我到目前为止所做的,我碰壁了。

我发现这 2 个功能的组合将从 1 个组中删除所有成员:

//Function to Remove 1 group member from 1 group
  function removeGroupMember(groupEmail, userEmail) {
  Logger.log(userEmail)
  userEmail = userEmail.trim();
  AdminDirectory.Members.remove(groupEmail, userEmail);
}
//Remove all Members from 1 group
function removeAllMembers() {
  var groupEmail = 'groupemail@mydomain.org';
  var members = AdminDirectory.Members.list(groupEmail).members;

  for (var m in members) {
    var member = members[m];
    var email = member.email;
    removeGroupMember(groupEmail, email)
  }
}

我想我可以嵌套另一个循环来将它与从工作表中获取组电子邮件的代码配对。

//get all of the groups from the spreadsheet
function getGroupsFromSpreadsheet(){
  const ss = SpreadsheetApp.getActive();
  const sheet = ss.getSheetByName('google_groups');
  const data = sheet.getRange(2,2,100).getValues();
  return data;
  logger.log(data)
}

这是我经过几次试验和错误后得出的结论。它从脚本编辑器运行,执行日志显示它是成功的,但是在 Google Admin 中实际上没有发生任何事情——它们都还在组中。

function removeAllMembers() {
  var groups =  [getGroupsFromSpreadsheet()];
  var groupEmail =  [getGroupsFromSpreadsheet()];
  var members = AdminDirectory.Members.list(groupEmail).members;
  
  for (var element in groups) {
  
    for (var m in members) {
      var member = members[m];
      var email = member.email;
      removeGroupMember(groupEmail, email);
    }
  }
}
4

2 回答 2

0

未经测试,因为我没有 G Suite 帐户。从未使用过AdminDirectoryAPI,所以我不知道下面是否是最好的方法。


function* getGroupEmailsFromSpreadsheet() {
  // Should allow caller to access each "group" with a 
  // simple for-of loop. Generator used here, but could
  // have used anything that would flatten the nested array
  // that Range.getValues() returns.
  
  const ss = SpreadsheetApp.GetActiveSpreadsheet();
  const sheet = ss.getSheetByName('google_groups');
  const data = sheet.getRange(2, 2, 100).getValues();
  
  for (let rowIndex = 0; rowIndex < data.length; ++rowIndex) {
    for (let columnIndex = 0; columnIndex < data[0].length; ++columnIndex) {
      // Maybe do some validation here e.g. making sure email is a string, contains an "@" character
      yield data[rowIndex][columnIndex];
    }
  }
}


function* getAllMembersInGroup(groupKeyOrEmail) {
  // Should allow caller to access each "member" with a simple
  // for-of loop. Generator used here, so that call site
  // doesn't need to deal with handling pagination.
  
  const options = {
    maxResults: 200
  };
  
  // https://developers.google.com/admin-sdk/directory/v1/reference/members/list#request
  let groupMembers = AdminDirectory.Members.list(groupKeyOrEmail, options);
  do {
    for (const member of groupMembers.members) {
      yield member;
    }
    options.pageToken = groupMembers.nextPageToken;
    groupMembers = AdminDirectory.Members.list(groupKeyOrEmail, options)
  } while (groupMembers.nextPageToken);
}


function removeAllMembersFromAllGroups() {
  // Should remove all "members" in the "groups" on the sheet.
  for (const groupEmail of getGroupEmailsFromSpreadsheet()) {
    for (const member of getAllMembersInGroup(groupEmail)) {
      Logger.log(`Going to try and remove member ${member.email} from group ${groupEmail}.`);
      AdminDirectory.Members.remove(groupEmail, member.email);
    }
  }
}

在运行removeAllMembersFromAllGroups函数之前:

  1. 如果您从工作someGroup表中随机选择一个组someMember(然后检查该成员是否实际上已从组中删除。这应该证明代码是否真的可以从组中删除成员(或者即使在这个简单的场景中也存在问题)。AdminDirectory.Members.remove(someGroup, someMember)someGroupsomeMember
  2. 仔细检查脚本是否已授予所有必要的权限。
  3. do-while然后在循环中设置一个断点,getAllMembersInGroup因为do-while循环是未经测试的。然后调试removeAllMembersFromAllGroups函数。您需要检查是否getAllMembersInGroup生成了有限的“成员”序列(并且不会永远循环)。
于 2020-09-07T13:20:44.313 回答
0

您拥有的代码中的问题实际上是导致我的代码中的groupsgroupEmail变量。因此,我建议你试试这个:

function getGroups() {
   var ss = SpreadsheetApp.openById('SPREADSHEET_ID').getSheetByName('SHEET_NAME');
   var groups = ss.getRange(2, 2, 2, 100).getValues();
   return groups;
}

function removeGroupMember(groupEmail, userEmail) {
   userEmail = userEmail.trim();
   AdminDirectory.Members.remove(groupEmail, userEmail);
}

function removeAllMembers() {
   var groups = getGroups();
   for (var i = 0; i < groups.length; i++) {
      var members = AdminDirectory.Members.list(theGroups[i][0]).members;
      for (var j = 0; j < members.length; j++) 
         removeGroupMember(theGroups[i][0], members[j].email);
   }
}

参考

于 2020-09-09T15:59:30.517 回答