Last active
April 14, 2021 16:35
-
-
Save FeMaffezzolli/c425e15d5a7b4c2044c660987b314506 to your computer and use it in GitHub Desktop.
Google Sheets, keeping sheet protection when duplicating
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function duplicateProtectedSheet() { | |
| // Novas abas | |
| const sheets = [ | |
| "NOVA-ABA-1", | |
| "NOVA-ABA-2", | |
| "NOVA-ABA-3", | |
| "NOVA-ABA-4", | |
| ] | |
| var ss = SpreadsheetApp.getActiveSpreadsheet(); | |
| // Aba de referência | |
| sheet = ss.getSheetByName("NOME-DA-ABA-REFERENCIA"); | |
| sheets.forEach(function(sheetName) { | |
| sheet2 = sheet.copyTo(ss).setName(sheetName); | |
| var p = sheet.getProtections(SpreadsheetApp.ProtectionType.SHEET)[0]; | |
| var p2 = sheet2.protect(); | |
| p2.setDescription(p.getDescription()); | |
| p2.setWarningOnly(p.isWarningOnly()); | |
| if (!p.isWarningOnly()) { | |
| p2.removeEditors(p2.getEditors()); | |
| p2.addEditors(p.getEditors()); | |
| // p2.setDomainEdit(p.canDomainEdit()); // only if using an Apps domain | |
| } | |
| var ranges = p.getUnprotectedRanges(); | |
| var newRanges = []; | |
| for (var i = 0; i < ranges.length; i++) { | |
| newRanges.push(sheet2.getRange(ranges[i].getA1Notation())); | |
| } | |
| p2.setUnprotectedRanges(newRanges); | |
| }) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment