简体   繁体   中英

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. Any ideas? Sorry if this is a stupid question! I am a novice in Google scripts.

It would be better to use AdminDirectory.Users.remove(email); rather than making a request to the API like you are doing.

Keep it in a variable if you want to log the response var request = AdminDirectory.Users.remove(data[i][1]);

To activate the AdminDirectory;

  1. Go to Resources -> Advanced Google services 在此处输入图片说明

  2. Enable the Admin Directory and then click on the link to the Google API console. 在此处输入图片说明

  3. Click Enable APIs and Services 在此处输入图片说明
  4. Search for Admin SDK 在此处输入图片说明
  5. Click on Admin SDK and then click 'Enable'

You're sending e to deleteUsers() , but that function doesn't receive any parameters. No need to access the spreadsheet data when it's already provided by the onFormSubmit() –look at the event object documentation for reference.

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 . (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.) 在此处输入图片说明

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 .

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM