This question is about Google spreadsheet. I have a spreadsheet that shares with multiple people. I'm using a script to update the sheet contents as the well. My question is when I running the script, is there a way to protect the sheet against others to edit it. And when the script done, editing permission can be granted back to users again?
1 Answers
I'm only replying because I see you haven't had any others yet, and I've tinkered with this very issue without success. Though in my case the objective was somewhat different in that I needed my code to be executed by users when they ran menu-driven scripts. Since you only need to modify the protection yourself, then you might find this works for you.
In any case, I can forsee a situation where users may be mid-edit when you run your code, which could prove frustrating for them! What you might want to do is to get to grips with the User Object and associated methods in order to manage them. Right now I can't see how you would do that without kicking them out (same result), but maybe someone else will chime in with some thoughts. Take a look over the Spreadsheet Class for the following:
- getEditors / removeEditor / addEditor
- getViewers / addViewers
https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet
Below is a function I made which worked for me (owner), but not for users - hope it helps:
//Target = string name of sheet to change permissions for
//ProtectOn = boolean: TRUE = turn on protection
//function returns boolean representing previous permissions setting (for loops/verification)
function SheetProtectionSet(Target, ProtectOn) {
var g_wkbActive = SpreadsheetApp.getActiveSpreadsheet();
var wksTarget = g_wkbActive.getSheetByName(Target);
var permissions = wksTarget.getSheetProtection();
var cur_protection = permissions.isProtected();
permissions.setProtected(ProtectOn);
wksTarget.setSheetProtection(permissions);
return cur_protection;
}