[英]Script to delete a user account from G Suite Admin SDK
I am writing a script that fetches a data from a spread sheet and based on every entry entered in the spread sheet, it fetches the data which is basically email addresses and deletes a user's account from the domain. 我正在编写一个脚本,该脚本从电子表格中获取数据,并根据在电子表格中输入的每个条目,获取基本上是电子邮件地址的数据,并从域中删除用户的帐户。
//** Delete the users on submitting the "Submit" button
function onFormSubmit(e) {
//Logger.log(e);
//Run this function passing on event "e" when the form is submitted.
//Object e has form parameters sourced from the sheet attached to the form
deleteUsers(e);
}
//Logs all the info from the spreadsheet and deletes the user
function deleteUsers() {
var sheet = SpreadsheetApp.getActiveSheet();
var data = sheet.getDataRange().getValues();
for (var i = 0; i < data.length; i++) {
Logger.log('managerEmail: ' + data[i][0]);
Logger.log('email: ' + data[i][1]);
Logger.log('trasferToEmail: ' + data[i][3]);
var managerEmail = data[i][0];
var email = data[i][1];
var trasferToEmail = data[i][3];
var request = {
'url': 'https://www.googleapis.com/admin/directory/v1/users/' + email,
'method' : 'DELETE'
};
}
But I am still unsuccessful in deleting an account. 但是我仍然无法删除帐户。 I actually tried to implement it based on this doc https://developers.google.com/admin-sdk/directory/v1/reference/users/delete but didn't know how to use it.
我实际上试图根据此文档https://developers.google.com/admin-sdk/directory/v1/reference/users/delete实施它,但不知道如何使用它。 Any ideas?
有任何想法吗? Sorry if this is a stupid question!
抱歉,这是一个愚蠢的问题! I am a novice in Google scripts.
我是Google脚本的新手。
It would be better to use AdminDirectory.Users.remove(email);
最好使用
AdminDirectory.Users.remove(email);
rather than making a request to the API like you are doing. 而不是像您一样向API发出请求。
Keep it in a variable if you want to log the response var request = AdminDirectory.Users.remove(data[i][1]);
如果要记录响应,请将其保留在变量中
var request = AdminDirectory.Users.remove(data[i][1]);
To activate the AdminDirectory; 激活AdminDirectory;
Go to Resources -> Advanced Google services 转到资源->高级Google服务
Enable the Admin Directory and then click on the link to the Google API console. 启用管理员目录,然后单击指向Google API控制台的链接。
You're sending e
to deleteUsers()
, but that function doesn't receive any parameters. 您正在将
e
发送到deleteUsers()
,但是该函数未接收任何参数。 No need to access the spreadsheet data when it's already provided by the onFormSubmit()
–look at the event object documentation for reference. 当
onFormSubmit()
已提供电子表格数据时,无需访问该电子表格数据–请查看事件对象文档以供参考。
function deleteUser(e) {
var data = e.namedValues;
var managerEmail = data["Manager Email"][0]; //You'll need to adjust the field names to match with your form data
var email = data["Email"][0];
var transferToEmail = data["Transfer to Email"][0];
var response = AdminDirectory.Users.remove(email);
}
To make sure that your trigger is correctly set up, first have the form responses get saved to your spreadsheet. 为了确保正确设置触发器,请首先将表单响应保存到电子表格中。 Then Edit > Current project's triggers and copy these settings:
然后编辑>当前项目的触发器并复制以下设置:
To make AdminDirectory
work, you need to enable advanced services . 要使
AdminDirectory
正常工作,您需要启用高级服务 。 (In the script editor, go to Resources > Advanced Google services and switch "on" Admin Directory API. Then click the link at the bottom of the modal to enable the Admin SDK in the API Console.) (在脚本编辑器中,转到“资源”>“高级Google服务”并“打开” Admin Directory API。然后单击模式底部的链接以在API控制台中启用Admin SDK。)
If I was mistaken about what data the form is collecting and you really do need to pull the data from spreadsheet (assuming the form isn't connected to the sheet), then you need to create a trigger for when there is a submission to that form. 如果我误认为表单正在收集什么数据,并且确实需要从电子表格中提取数据(假设表单未连接到工作表),那么您需要为何时提交该表单创建触发器 。形成。 Run this function to install the trigger.
运行此功能以安装触发器。
function installFormTrigger() {
var form = FormApp.openById("FORM_ID");
ScriptApp.newTrigger("deleteUsers")
.forForm(form)
.onFormSubmit()
.create();
}
Then your original deleteUsers()
function will work almost as you had it, but with the addition of AdminDirectory
. 然后,原始的
deleteUsers()
函数几乎可以像您一样运行,但是需要添加AdminDirectory
。
function deleteUsers() {
var sheet = SpreadsheetApp.getActive().getSheetByName("SheetName"); //HIGHLY recommend using this instead
var data = sheet.getDataRange().getValues();
for (var i = 0; i < data.length; i++) {
var managerEmail = data[i][0];
var email = data[i][1];
var trasferToEmail = data[i][3];
var response = AdminDirectory.Users.remove(email);
}
}
Note that in your for loop, you might come across an invalid email or AdminDirectory
could through an error, so I'd suggest implementing try...catch and logging . 请注意,在您的for循环中,您可能会遇到无效的电子邮件,或者
AdminDirectory
可能会通过错误进行处理,因此我建议实现try ... catch和logging 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.