I am trying to apply protection on all sheets and certain ranges using a google script, for every sheet in its workbook (and can be applied on new added sheets via a trigger later)
the problem is, I want to assign different ranges to different users after protecting the whole sheet from anyone, but the people allowed to edit on their ranges!
I can't seem to know why it doesn't work.. It works oppositely, allowing all users to edit all ranges but the protected ones.
I want the first editor to be able to edit his QC_Range, and the second editor to be able to edit his PLN_Range and restrict access from editing (making it view only) for all other cells and whole the sheet.
function Sheet_Ranges_Protection(){
var Veranda_Test = SpreadsheetApp.openById("Sheet ID");
//var workbookB = SpreadsheetApp.getActiveSpreadsheet();
var Veranda_Sheets = Veranda_Test.getSheets();
for(var SheetNumb = 0; SheetNumb < Veranda_Sheets.length; SheetNumb++)
{
var Shprotection = Veranda_Sheets[SheetNumb].getProtections(SpreadsheetApp.ProtectionType.SHEET);
var QC_Range = Veranda_Sheets[SheetNumb].getRange("E1:G5");
var PLN_Range = Veranda_Sheets[SheetNumb].getRange("A1:C5");
if (Shprotection == true)
SheetNumb++;
else if (Shprotection == false)
{
var Shprotection = Veranda_Sheets[SheetNumb].protect().setDescription('Sheet Protection');
var Rangesprotection = Veranda_Sheets[SheetNumb].getProtections(SpreadsheetApp.ProtectionType.RANGE);
if (Rangesprotection == false)
{
var QCprotection = QC_Range.protect().setDescription('QC Protection');
var me = Session.getEffectiveUser();
QCprotection.addEditor(me);
QCprotection.removeEditors(QCprotection.getEditors());
if (QCprotection.canDomainEdit())
{
QCprotection.setDomainEdit(false);
QCprotection.addEditors(['[email protected]']);
}
var PLNprotection = PLN_Range.protect().setDescription('PLN Protection');
var me = Session.getEffectiveUser();
PLNprotection.addEditor(me);
PLNprotection.removeEditors(PLNprotection.getEditors());
if (PLNprotection.canDomainEdit())
{
PLNprotection.setDomainEdit(false);
PLNprotection.addEditors(['[email protected]']);
}
}
else
SheetNumb++;
// Shprotection = true;
// Rangesprotection = true;
// Ensure the current user is an editor before removing others. Otherwise, if the user's edit
// permission comes from a group, the script will throw an exception upon removing the group.
}
}
}