Get YouTube subscribers that watch and like your videos
Get Free YouTube Subscribers, Views and Likes

Google Sheets: Copy With Protections

Follow
Spencer Farris

How to copy a sheet with its protections.
Important note:
* You cannot just call prot[i].getRange() because the range value includes data about the sheet it's protecting, not just the row/column or A1 notation value. Make sure to call prot[i].getRange().getA1Notation() so it runs correctly.

Sheet (to copy):
https://docs.google.com/spreadsheets/...

Script:
function copyWithProtections(){
const sh = SpreadsheetApp.getActiveSpreadsheet();
const ss = sh.getSheetByName("Template");
const prot = ss.getProtections(SpreadsheetApp.ProtectionType.RANGE)

let nSheet = ss.copyTo(sh).setName(sh.getNumSheets()1);
let p;

for (let i in prot){
p = nSheet.getRange(prot[i].getRange().getA1Notation()).protect();
p.removeEditors(p.getEditors());
if (p.canDomainEdit()) {
p.setDomainEdit(false);
}
}
}

posted by nidantsqq