I am sharing a set of Google Sheet documents with other users. I am the owner of all the spreadsheets and they are housed on my Google Drive. Every user has their own dedicated sheet. Users need to be able to run scripts on their sheet that collect and manipulate data from the sheet, which means the sheet needs to be unprotected while the scripts run. However, at the end end of the script, I want to protect the sheet again so that the user running the script is unable to edit the protected cells once the script is done running.
I am able to easily remove the protections from the sheet at the beginning of the script, but I am unable to protect the sheet again via the script without the user who is running the script being listed as an editor. I have tried using the "removeEditors" function to no avail (see below for basic example for one of the protected ranges).
var userEmail = Session.getActiveUser().getEmail();
var protection = newPickSheet.getRange('A1:M3').protect();
protection.removeEditor(userEmail);
For your reference, here is the code I'm using at the beginning of the script to remove all protections:
var protections = newPickSheet.getProtections(SpreadsheetApp.ProtectionType.RANGE);
for(var i = 0; i < protections.length; i++){
protections[i].remove();
}
To summarize, I need my script to:
- Remove all protections
- Execute the main part of the script
- Re-add all protections so that only me, the sheet owner, but NOT the user running the script, has edit ability.
I was able to accomplish this very easily using VBA when deploying Excel Macros, but it seems this is not nearly as simple using App Scripts.
If anyone can help me figure out a solution, it would be greatly appreciated.
remove all protections? - Tanaike