将谷歌小组成员与工作表进行比较



我的目标是使用谷歌工作表来添加和删除特定组的成员。添加不是问题,但我在比较两个数组时遇到了问题,这样我就可以删除属于组但不属于工作表的电子邮件地址。请告诉我是否还有其他途径。

我曾尝试编写代码,该代码将循环通过组中的用户,然后另一个循环从工作表中获取电子邮件地址。这种比较是有效的,但只有当双方都有对手时才有效。如果我交换逻辑以查找丢失的地址,那么当数组比较值时,如果它们不匹配,就会出现误报。

以下是我目前所拥有的:

function listGroupMembers() {
var GROUP_EMAIL = "group@group.com";
var group = GroupsApp.getGroupByEmail(GROUP_EMAIL);
var users = group.getUsers();
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("main list");
var emailsFromSheet = sheet.getRange(1,7,sheet.getLastRow()-1,1).getValues();
var group = GroupsApp.getGroupByEmail(GROUP_EMAIL);
console.log(users.length);
console.log(emailsFromSheet.length);
for (i=0;i<users.length;i++) {

var user = users[i];
for (j=0;j<emailsFromSheet.length;j++) {
var emailStuff = emailsFromSheet[j];
if (user.getEmail() == emailStuff) {
console.log(user.getEmail() + " " + emailStuff);
}
}
}
}

组的成员

function listGroupMembers() {
var GROUP_EMAIL = "group@group.com";
var group = GroupsApp.getGroupByEmail(GROUP_EMAIL);
var us = group.getUsers();
const usem = us.map(u => u.getEmail()).flat();
var sh = SpreadsheetApp.getActive().getSheetByName("main list");
var em = sh.getRange(1,7,sh.getLastRow()).getValues().flat();
em.forEach(e => {
let idx = usem.indexOf(e)
if(~idx) {
Logger.log(`${e} is a member of ${GROUP_EMAIL}` )
} else {
Logger.log(`${e} is not a member of ${GROUP_EMAIL}` )
}
})
}

也许这会奏效:

function listGroupMembers() {
var GROUP_EMAIL = "group@group.com";
var group = GroupsApp.getGroupByEmail(GROUP_EMAIL);
var us = group.getUsers();
const usem = us.map(u => u.getEmail()).flat();
var sh = SpreadsheetApp.getActive().getSheetByName("main list");
var em = sh.getRange(1,7,sh.getLastRow()).getValues().flat();
usem.forEach(e => {
let idx = em.indexOf(e)
if(~idx) {
Logger.log(`${e} is a member of usem list` )
} else {
Logger.log(`${e} is not a member of usem list` )
}
})
}

相关内容

  • 没有找到相关文章

最新更新