here my specific case:
- I have some range protected in google sheets
- I need to replace some specific Editor if is editor of those range (var Editor2Replace and Editor2Add are emails)
- Logically I tried to, for each sheet:
- Cycle (FOR) of all the protected range (counter p)
- For each protected range catch current editors and have it in array
- Of the Editors read the email ==> this is what generate the mistake
- Cycle (FOR) all the editors looking if someone of those is == Editor2Replace (that is an email)
Here the code, but something is logically wrong, I doubt in what is an array and what not..
var Protections = sheet.getProtections(SpreadsheetApp.ProtectionType.RANGE);
for (var p = 0; p < Protections.length; p++) {
var Protection_Desc = Protections[p].getDescription();
var Protection_Editors = [];
var Protection_Editors = [Protections[p].getEditors()];
for (var r = 0; r < Protection_Editors.length; r++){
var Protection_Email[r] = [Protection_Editors[r].getEmail()];
if (Protection_Idontknow == Editor2Replace){
Protections[p].addEditor = Editor2Add;
Protections[p].removeEditor = Editor2Replace;
var Protection_Range = Protections[p].getRange();
var Protection_Row = Protection_Range.getRow();
var Owner1 = sheet.getRange(Protection_Row,5).getValue();
var Owner2 = sheet.getRange(Protection_Row,6).getValue();
if (Owner1 == Editor2Replace){
sheet.getRange(Protection_Row,5).setValue(Editor2Add);
}
if (Owner2 == Editor2Replace){
sheet.getRange(Protection_Row,6).setValue(Editor2Add);
}
}
}
Many thanks for hepling
There were a lot of issues in your script and I will enumerate them one by one. Also, I was able to replace a user in the protected sheet by modifying your script.
Issues:
Protection_Idontknow
addEditor
andremoveEditor
Below code should fix those issues (added some comments):
Code:
Note:
Reference: