这里有几个函数可以用来完成你想要的。
首先是删除过程。正如 Sandy 评论的那样,创建一个用于标记要删除的成员的列。只要未删除的为空白,您应该可以在这里使用您喜欢的任何内容。您将需要更改变量“标记”以反映该列编号,您还需要对“电子邮件”变量执行相同操作。那是保存用户电子邮件的列。
首先复制您的工作表,并确保在删除行时它们是正确的!
/**
*Removes members from group that are marked for removal in a specific column
*/
function removeMarkedMembers() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Sheet1');
var range = sheet.getDataRange();
var data = range.getValues();
//Adjust these following variables to match your sheet
var groupEmail = 'group@email.com';//change to your group email address
var marked = 7;//number of column used to mark for removal ex. Column A = 1
var email = 4; //number of column that holds member email address.
//must adjust columns numbers to zero based array integers
marked = marked-1;
email = email-1;
var rows = [];
for (var d in data) {
var rowData = data[d];
if(rowData[marked] != "" || rowData != null) {
removeGroupMember(groupEmail, rowData[email]);
rows.push(new Number(d)+1)
}
}
for (var r in rows) {
sheet.deleteRow(rows[r])
}
}
function removeGroupMember(groupEmail, userEmail) {
userEmail = userEmail.trim();
AdminDirectory.Members.remove(groupEmail, userEmail);
}
最后还有一些其他的管理组的功能,根据需要使用它们。最好创建一个菜单,您可以从电子表格中调用这些菜单。
/**
*Adds all email addresses in column to group.
*/
function addAllToGroup() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Sheet1');
var row = sheet.getLastRow();
var column = 7;
var range = sheet.getRange(2, column, sheet.getLastRow(), 1)
var emails = range.getValues();
for (var e in emails) {
var email = emails[e]
if(validateEmail(email)) {
addGroupMember(email, 'your@groupemailaddress.com');////Must replace with group email address you want them added too.
}
}
}
function addGroupMember(userEmail, groupEmail) {
var member = {
email: userEmail,
role: 'MEMBER'
};
AdminDirectory.Members.insert(member, groupEmail);
}
function validateEmail(email) {
var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(email);
}
/**
*Removes all members from a defined group
*/
function removeAllMembers() {
var groupEmail = 'group@email.com';
var members = AdminDirectory.Members.list(groupEmail).members;
for (var m in members) {
var member = members[m];
var email = member.email;
removeGroupMember(groupEmail, email);
}
}
这应该可以帮助您获得漂亮的成品。
编辑代码以处理评论中报告的错误。
还更新了标记列必须使用“x”来删除标记。最后,要正确处理行的删除,您需要清除它,然后对整个工作表进行排序,否则您将删除错误的行。记得调整你的参数。
/**
*Removes members from group that are marked for removal in a specific column
*/
function removeMarkedMembers() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Sheet1');
var range = sheet.getRange(2, 1, sheet.getLastRow()-1, sheet.getLastColumn());
var data = range.getValues();
Logger.log(data)
//Adjust these following variables to match your sheet
var groupEmail = 'test-group@email.com';//change to your group email address
var marked = 2;//number of column used to mark for removal ex. Column A = 1
var email = 1; //number of column that holds member email address.
//must adjust columns numbers to zero based array integers.
marked = marked-1;
email = email-1;
Logger.log(marked+' : '+email)
var rows = [];
for (var d in data) {
var rowData = data[d];
if(validateEmail(rowData[email]) && rowData[marked] == "x") {
Logger.log('marked')
removeGroupMember(groupEmail, rowData[email]);
rows.push(new Number(d)+2)
}
}
for (var r in rows) {
var row = rows[r];
sheet.getRange(row, 1, 1, sheet.getLastColumn()).clear();
}
range.sort(1);
}
function removeGroupMember(groupEmail, userEmail) {
Logger.log(userEmail)
userEmail = userEmail.trim();
AdminDirectory.Members.remove(groupEmail, userEmail);
}
/**
*Adds all email addresses in column to group.
*/
function addAllToGroup() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Sheet1');
var row = sheet.getLastRow();
var column = 1;
var range = sheet.getRange(2, column, sheet.getLastRow(), 1)
var emails = range.getValues();
for (var e in emails) {
var email = emails[e]
if(validateEmail(email)) {
addGroupMember(email, 'test-group@email.com');////Must replace with group email address you want them added too.
}
}
}
function addGroupMember(userEmail, groupEmail) {
var member = {
email: userEmail,
role: 'MEMBER'
};
var members = AdminDirectory.Members.list(groupEmail).members
for (var m in members) {
if(members[m].email == userEmail) {
return 'Member already exist';
}
}
AdminDirectory.Members.insert(member, groupEmail);
}
function validateEmail(email) {
var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(email);
}
/**
*Removes all members from a defined group
*/
function removeAllMembers() {
var groupEmail = 'test-group@email.com';
var members = AdminDirectory.Members.list(groupEmail).members;
for (var m in members) {
var member = members[m];
var email = member.email;
removeGroupMember(groupEmail, email);
}
}