0

I would like to create a script that I remove suspended users who have been suspended for 6 months. I have this code, but this code only removes the users.


function deleteUsers() {
  var ss = SpreadsheetApp.openById('xxxxxxxxuuuuuuuuuyyyyygygygygygygygygy');//id of exported spreadsheet
  var sheet = ss.getSheetByName('neverloggedin');//tab name of data
  var data = sheet.getDataRange().getValues();//all the data in an array
  
  var len = data.length;//number of rows in the array
  
  for(var i=1; i<len; i++){//has a header row, start counting at 1
    var user = data[i][2];//3rd column has the user email addresses
    Logger.log(user);
    
    
    //use try catch in case a user is already removed
    try{
    AdminDirectory.Users.remove(user);
    }
    catch(err){}
    
  }
}

Yeiko
  • 1
  • 1
    Please check this post: [link](https://stackoverflow.com/questions/58322286/getting-all-viewers-of-spreadsheet-using-app-script) . You can not retrieve view information and therefore finding which users hasn't viewed a particular file is not possible. – Marios Aug 07 '20 at 11:32
  • 2
    You say you want to "remove suspended users" and "this code only remove the users" - I do not understand where the problem is. – ziganotschka Aug 07 '20 at 11:58
  • @ziganotschka - I think the question is about "removing only users that were suspended in the last 6 months", but I might be reading too much into the question. If that is indeed the question, `User` resource has `suspended` field, but I am not sure there is a history of suspensions that is assigned to a user – Oleg Valter is with Ukraine Aug 07 '20 at 12:13
  • I suppose that can be achieved by setting the date of suspension on the user "manually" (in a sense of setting a custom property on a User when suspending - assuming the suspension is also done via the API), but that's way out of the scope of an answer here – Oleg Valter is with Ukraine Aug 07 '20 at 12:21
  • 1
    I see, I had the impresion that the user has a spreadsheet where he manually inserted all the users he wants to remove. But maybe he wants this spreadsheet to be populated automatically wiht the correct users? In the latter case, he could populate this spreadsheet with users and timestamps whenever a suer is suspended - and wen he runs the `deleteUsers()` function - the function could check the timestamps to calculate either 6 months have passed already. – ziganotschka Aug 07 '20 at 12:24
  • 1
    @ziganotschka - well, actually, you may be right. I don't know, this is not apparent from the code... Actually, I would consider the question about doing it via an API interesting :) I guess we need details from the OP. Re: suggestion - yes, I think that is a viable option (consider making an answer out of it) – Oleg Valter is with Ukraine Aug 07 '20 at 12:29

0 Answers0