即使用户存在于使用 google 脚本的 Google 组中,如何继续 运行 脚本?
How to continue running the script even if a user exists in Google Group using google scripts?
我需要 Google 组的帮助。该代码当前检查成员是否已存在,但在找到成员时停止。我如何修改代码以使其不停止地处理下一行?
TIA!
function updateGroup() {
const s = SpreadsheetApp.openById("ID HERE");
const sheet_name = s.getSheetByName("REPORT HERE");
const sheet_data = sheet_name.getRange(2,1,sheet_name.getLastRow(),sheet_name.getLastColumn());
const sheet_dataVal = sheet_data.getValues();
for (var i = 0; i < sheet_dataVal.length-1; i++) {
var member_Email = sheet_dataVal[i][9]; // REWS Projects Email Address
var groupEmail = "GROUP EMAIL ADDRESS HERE";
var member_Role = "MEMBER";
var member_Type = "USER";
var group = GroupsApp.getGroupByEmail(groupEmail);
var comment = sheet_dataVal[i][13];
if (comment === "Member Added to Group" || comment === "Member already exists") {continue;}
var checkMembers = AdminDirectory.Members.list(groupEmail).members
for (var m in checkMembers) {
if (checkMembers[m].email == member_Email);
return sheet_name.getRange(i+2,14).setValue("Member already exists");
}
addNewMembersToGroup(member_Email,member_Role,groupEmail,i,sheet_name);
}
}
function addNewMembersToGroup(member_Email,member_Role,groupEmail,i,sheet_name) {
/* Member does not exists in group, add */
var addNewMember = {
kind: "admin#directory#member",
email: member_Email,
role: member_Role
};
AdminDirectory.Members.insert(addNewMember,groupEmail);
sheet_name.getRange(i+2,14).setValue("Member Added to Group");
}
当您使用 return
时,函数的执行将停止 - 因此您会遇到问题。
为了解决这个问题,您应该删除 return
并使 if
声明如下:
if (checkMembers[m].email == member_Email);
sheet_name.getRange(i+2,14).setValue("Member already exists");
参考
我需要 Google 组的帮助。该代码当前检查成员是否已存在,但在找到成员时停止。我如何修改代码以使其不停止地处理下一行?
TIA!
function updateGroup() {
const s = SpreadsheetApp.openById("ID HERE");
const sheet_name = s.getSheetByName("REPORT HERE");
const sheet_data = sheet_name.getRange(2,1,sheet_name.getLastRow(),sheet_name.getLastColumn());
const sheet_dataVal = sheet_data.getValues();
for (var i = 0; i < sheet_dataVal.length-1; i++) {
var member_Email = sheet_dataVal[i][9]; // REWS Projects Email Address
var groupEmail = "GROUP EMAIL ADDRESS HERE";
var member_Role = "MEMBER";
var member_Type = "USER";
var group = GroupsApp.getGroupByEmail(groupEmail);
var comment = sheet_dataVal[i][13];
if (comment === "Member Added to Group" || comment === "Member already exists") {continue;}
var checkMembers = AdminDirectory.Members.list(groupEmail).members
for (var m in checkMembers) {
if (checkMembers[m].email == member_Email);
return sheet_name.getRange(i+2,14).setValue("Member already exists");
}
addNewMembersToGroup(member_Email,member_Role,groupEmail,i,sheet_name);
}
}
function addNewMembersToGroup(member_Email,member_Role,groupEmail,i,sheet_name) {
/* Member does not exists in group, add */
var addNewMember = {
kind: "admin#directory#member",
email: member_Email,
role: member_Role
};
AdminDirectory.Members.insert(addNewMember,groupEmail);
sheet_name.getRange(i+2,14).setValue("Member Added to Group");
}
当您使用 return
时,函数的执行将停止 - 因此您会遇到问题。
为了解决这个问题,您应该删除 return
并使 if
声明如下:
if (checkMembers[m].email == member_Email);
sheet_name.getRange(i+2,14).setValue("Member already exists");