Home > Back-end >  Need an Apps Script for Google Sheets to remove all access except the owner
Need an Apps Script for Google Sheets to remove all access except the owner

Time:03-29

My company split with a sister company and we need to remove access from about 20 files that have over 100 permissions each. After some research it appears that the only solution is to write an apps script with which i have zero experience atm. I've tried moving the sheets file to a limited folder but the permissions already on the file just transfer over. Deleting the file and reuploading isn't an option as the links need to stay the same for import ranges and queries. The only thing i haven't tried is the apps scripts cause i have no idea what to do with them. I found some vaguely similar solutions but i couldn't apply them to my case. Also, i can't share the file because of company privacy and the only relevant thing would be the ammount of people it was shared with and can access the file.

TLDR: I need a script that removes all access (editing, viewing, everything) to the current sheet, except the owner. Or, a script that does the same but for all files in a specific folder, so we can dump all the files in there and reset permissions for all.

CodePudding user response:

Try something like this:

function myFunction() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();

  var editors = ss.getEditors();
  var i = editors.length
  while (i--) ss.removeEditor(editors[i]);

  var viewers = ss.getViewers();
  var i = viewers.length
  while (i--) ss.removeViewer(viewers[i]);
}

or:

function myFunction() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  ss.getEditors().forEach(e => ss.removeEditor(e));
  ss.getViewers().forEach(v => ss.removeViewer(v));
}
  • Related