I need to make sure that the all editors are removed, then set the owner and myself as the editors that can change restricted ranges and sheets, but I keepgetting the following error:
Exception: You can't remove yourself as an editor.
Here is a piece of the code I am using:
const p = templateContentWbSht.getProtections(SpreadsheetApp.ProtectionType.SHEET)[0];
let p2 = newContentWbSht.protect();
p2.setDescription(p.getDescription());
p2.setWarningOnly(p.isWarningOnly());
if (!p.isWarningOnly()) {
const myEmail = Session.getEffectiveUser().getEmail();
const ownerEmail = projectWbFile.getOwner().getEmail();
console.log("My Email: " + myEmail);
console.log("Owner Email: " + ownerEmail);
const editors = projectWbFile.getEditors();
editors.forEach(e => {
return e.getEmail != myEmail;
})
editors.forEach(e => {
console.log("Emails to remove: " + e.getEmail());
})
const editorsToKeep = editors.filter(editor => {
return editor.getEmail() != myEmail && editor.getEmail() != ownerEmail;
});
editorsToKeep.forEach(e => {
console.log("Emails to keep: " + e.getEmail());
})
p2.removeEditors(editors); //Exception: You can't remove yourself as an editor.
p2.addEditors(editorsToKeep);
}
I am not sure if this would be the best approach, though, and would appreciate any light shed.