Protect the Same Ranges of Every Tab

How to protect the same asynchronous ranges on every tab on a Sheet.
Important notes:
* The ranges in an array of A1 notation Strings of the ranges. Make sure the quotation marks are around them
* Don’t mix up the variables for each FOR() loop

Sheet (to copy from “Make a copy” in File)
https://docs.google.com/spreadsheets/d/1CNtzIrAI9bd8y1zW5FL34bPSyKFlN5fEEk-dizdxSVo/edit#gid=0

Script:
function protectRanges(){
const sh = SpreadsheetApp.getActive();
let ranges = [“A1:F5″,”I1:K1″,”I3:K3″,”I5:K5”];
let sheets = sh.getSheets();
let ss, p;
for (let i in sheets){
ss = sheets[i];
for (let j in ranges){
p = ss.getRange(ranges[j]).protect();
p.removeEditors(p.getEditors());
if (p.canDomainEdit()){
p.setDomainEdit(false);
}
}
}
}

Connect with me:
• spencer.farris@gmail.com
• spencerfarris.me
• www.linkedin.com/in/spencer-farris/
• Twitter @FarrisSpencer
• Google Product Expert support.google.com/docs/profile/12305

Leave a Reply

Your email address will not be published. Required fields are marked *