From the comment:
@Saddles Removing the
?
solved it!I'm not familiar with that operator, and just copied it from the documentation - do you happen to know why that might have been causing issues? (I feel like this should be an answer, since it seems likely to me that others might run into a similar issue with adapting the provided example, could you please write it up into one and then I can mark it as best?)
Change:
const protection = s.getProtections(SpreadsheetApp.ProtectionType.SHEET)[0];
if (protection?.canEdit()) {
protection.remove();
}
To:
const protection = s.getProtections(SpreadsheetApp.ProtectionType.SHEET)[0];
if (protection.canEdit()) {
protection.remove();
}
As per remove():
// Remove sheet protection from the active sheet, if the user has permission to
// edit it.
const sheet = SpreadsheetApp.getActiveSheet();
const protection = sheet.getProtections(SpreadsheetApp.ProtectionType.SHEET)[0];
if (protection?.canEdit()) {
protection.remove();
}
The example works. However, it's likely that the Optional chaining syntax isn't being detected when the script runs, causing the TypeError: protection.remove is not a function.
I recommend making sure that Enable Chrome V8 runtime
is enabled in the project by going to its Project Settings, and if the issue doesn't clear by itself, even after the removal of ?
in the code, submit a bug report to let Google know about the unusual behavior that's going on.