In our team we have the following scenario: we're using a google spreadsheet with some protected areas (mainly formulas that must not be overridden - only the "admin", in our case it's me, can edit them). The other input fields are unprotected. Every team member can enter their data. At some point in time (end of the sprint), our project managers need to report the numbers from the spreadsheet. To ensure that the numbers do not get changed afterward, they want to look at the entire sheet. For the next sprint, we create a new spreadsheet. I added an app script (code below) and assigned this to a button that they can click to lock the sheets (or at least the important ranges where people enter their data).
If I click the button everything works fine. But if they do that, they get an exception:
Exception: You are trying to edit a protected cell or object. Please contact the spreadsheet owner to remove protection if you need to edit.
I tried various options, setting range protections, sheet protection, removing me as an editor, adding them as an editor to protection, etc. But in all cases, as soon as a function on the protection is called (e.g. addEditor()
), this exception appears.
Any idea how to make it possible, so that basically everybody can click the button and so lock the entire sheet?
Below my function. This example uses the sheet.protect()
, but I also tried the ranged protections, just to make it easier to understand what I'm talking about.
function lockSheet() {
var me = Session.getEffectiveUser();
var as = SpreadsheetApp.getActiveSpreadsheet();
as.getSheets().map(function(sheet) {
var protection = sheet.protect().setDescription('The sprint has been closed. No further editing possible.');
// Ensure the current user is an editor before removing others. Otherwise, if the user's edit
// permission comes from a group, the script throws an exception upon removing the group.
protection.addEditor(me);
protection.removeEditors(protection.getEditors());
if (protection.canDomainEdit()) {
protection.setDomainEdit(false);
}
});
var ui = SpreadsheetApp.getUi();
var response = ui.alert('Sheet is now locked. The only editor now is: ' + me);
}