I'm still learning the fundamentals of Javascript, but I know there's a way to simplify this one, I just can't figure it out. I've modified a script I found here: Protect spreadsheet then unprotect specific cells and ranges with script
I'm sharing a sheet with multiple users and want most of each sheet protected. The above linked script helped me ensure that the ranges my users need access to can be edited by all even as the sheets expand. However, the editable ranges are different on each sheet, so I've ended up recycling the unlockCertainRanges()
portion to apply it to each sheet individually. This has made the script take up to 70 seconds to run. I'm pretty sure I can use an array and a for loop to run through each sheet, but I'm having trouble figuring it out.
This is what I have so far:
function mainProtection(){ //Main function to run
var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
var disregard = ["NetSuite INV", "Sales", "Delivery Schedule", "TO", "APP Arch", "ACC Arch", "INV REF"]; //ADD SHEET NAMES HERE THAT YOU WANT TO BE DISREGARDED
for(var x=0; x<sheets.length; x++){
if(disregard.some(data => sheets[x].getName().includes(data))){
//E.g. Disregard any sheet names added on the "disregard" array
}else{
unlockOrderingRanges(sheets[x]);
unlockPendingTORanges(sheets[x]);
unlockAccessoryRanges(sheets[x]);
unlockApparelRanges(sheets[x]);
}
}
}
function unlockOrderingRanges(){ //Function to unlock ranges on Ordering spreadshseet
var sheet = SpreadsheetApp.getActive().getSheetByName("Ordering");
// Remove all range protections in the spreadsheet
var protections = sheet.getProtections(SpreadsheetApp.ProtectionType.RANGE);
for (var i = 0; i < protections.length; i++) {
var protection = protections[i];
protection.remove();
}
var protection = sheet.protect();
//restrict editors to owner
protection.getRange().getA1Notation();
var eds = protection.getEditors();
protection.removeEditors(eds);
//set unprotected ranges
var ranges = protection.getUnprotectedRanges();
var data = ["O5:P", "C2:E2"]; // ADD YOUR RANGES HERE
data.forEach(res => { //LOOPS INTO EVERY ARRAY CONTAINING SPECIFIC RANGES
ranges.push(sheet.getRange(res));
protection.setUnprotectedRanges(ranges); //REMOVES THE PROTECTION ON THE RANGE
});
}
I omitted the unlockPendingTORanges();
, unlockAccessoryRanges();
and unlockApparelRanges();
scripts for the sake of simplifying this post, as they are identical to the unlockOrderingRanges()
script, they just change the defined sheet name and ranges.
Any guidance is greatly appreciated!
ETA details of the unlockPendingTORanges();
, unlockAccessoryRanges();
and unlockApparelRanges();
function unlockPendingTORanges(){ //Function to unlock ranges on Pending TOs spreadshseet
var sheet = SpreadsheetApp.getActive().getSheetByName("Pending TOs");
// Remove all range protections in the spreadsheet
var protections = sheet.getProtections(SpreadsheetApp.ProtectionType.RANGE);
for (var i = 0; i < protections.length; i++) {
var protection = protections[i];
protection.remove();
}
var protection = sheet.protect();
//restrict editors to owner
protection.getRange().getA1Notation();
var eds = protection.getEditors();
protection.removeEditors(eds);
//set unprotected ranges
var ranges = protection.getUnprotectedRanges();
var data = ["E6:H"]; // ADD YOUR RANGES HERE
data.forEach(res => { //LOOPS INTO EVERY ARRAY CONTAINING SPECIFIC RANGES
ranges.push(sheet.getRange(res));
protection.setUnprotectedRanges(ranges); //REMOVES THE PROTECTION ON THE RANGE
});
}
function unlockAccessoryRanges(){ //Function to unlock ranges on Accessory INV spreadshseet
var sheet = SpreadsheetApp.getActive().getSheetByName("Accessory INV");
// Remove all range protections in the spreadsheet
var protections = sheet.getProtections(SpreadsheetApp.ProtectionType.RANGE);
for (var i = 0; i < protections.length; i++) {
var protection = protections[i];
protection.remove();
}
var protection = sheet.protect();
//restrict editors to owner
protection.getRange().getA1Notation();
var eds = protection.getEditors();
protection.removeEditors(eds);
//set unprotected ranges
var ranges = protection.getUnprotectedRanges();
var data = ["E5:H"]; // ADD YOUR RANGES HERE
data.forEach(res => { //LOOPS INTO EVERY ARRAY CONTAINING SPECIFIC RANGES
ranges.push(sheet.getRange(res));
protection.setUnprotectedRanges(ranges); //REMOVES THE PROTECTION ON THE RANGE
});
}
function unlockApparelRanges(){ //Function to unlock ranges on Apparel INV spreadshseet
var sheet = SpreadsheetApp.getActive().getSheetByName("Apparel INV");
// Remove all range protections in the spreadsheet
var protections = sheet.getProtections(SpreadsheetApp.ProtectionType.RANGE);
for (var i = 0; i < protections.length; i++) {
var protection = protections[i];
protection.remove();
}
var protection = sheet.protect();
//restrict editors to owner
protection.getRange().getA1Notation();
var eds = protection.getEditors();
protection.removeEditors(eds);
//set unprotected ranges
var ranges = protection.getUnprotectedRanges();
var data = ["E5:F"]; // ADD YOUR RANGES HERE
data.forEach(res => { //LOOPS INTO EVERY ARRAY CONTAINING SPECIFIC RANGES
ranges.push(sheet.getRange(res));
protection.setUnprotectedRanges(ranges); //REMOVES THE PROTECTION ON THE RANGE
});
}