The below line gets an array of objects representing all protected ranges in the spreadsheet:
var protections = ss.getProtections(SpreadsheetApp.ProtectionType.RANGE);As we loop through getting the name of each named range from the above array (see previous blog post), we perform an additional if check to look for a specific name (studentDetails in this example):
if (name == 'studentDetails') {Upon finding the matching name we 'remove' the protection and 'break' out of the loop (as we no longer need to keep looping through the other names).
protections[i].remove();
break;
}
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 searchAndRemove() { | |
var ss = SpreadsheetApp.getActive().getSheetByName('Sheet1'); | |
/* | |
Get an array of objects representing all protected RANGEs in the spreadsheet. | |
If looking for sheet protection would use: (SpreadsheetApp.ProtectionType.SHEET)[0] | |
*/ | |
var protections = ss.getProtections(SpreadsheetApp.ProtectionType.RANGE); | |
// loop through protections array ************************* | |
for (var i=0; i<protections.length; i++) { | |
// get the name of each named range | |
var name = protections[i].getRangeName(); | |
Logger.log('Named range is: ' + name); | |
// look for name match and remove protected named range | |
if (name == 'studentDetails') { | |
protections[i].remove(); | |
break; | |
} | |
} // end of loop through protections array ***************** | |
} |
Search for and remove protected named ranges.xlsx
I can't get the name of the ranges, see below. Coold you please give me a help?
ReplyDeletefunction searchAndRemove() {
var ss = SpreadsheetApp.getActive().getSheetByName('Requerimento');
/*
Get an array of objects representing all protected RANGEs in the spreadsheet.
If looking for sheet protection would use: (SpreadsheetApp.ProtectionType.SHEET)[0]
*/
var protections = ss.getProtections(SpreadsheetApp.ProtectionType.RANGE);
// loop through protections array *************************
for (var i=0; i<protections.length; i++) {
// get the name of each named range
var name = protections[i].getRangeName();
Logger.log('Named range is: ' + name);
// look for name match and remove protected named range
if (name == 'studentDetails') {
protections[i].remove();
break;
}
}
[20-03-30 23:18:18:498 BRT] Named range is: null
[20-03-30 23:18:18:501 BRT] Named range is: null
[20-03-30 23:18:18:503 BRT] Named range is: null
[20-03-30 23:18:18:506 BRT] Named range is: null
[20-03-30 23:18:18:508 BRT] Named range is: null
[20-03-30 23:18:18:510 BRT] Named range is: null
[20-03-30 23:18:18:512 BRT] Named range is: null
Hi
DeleteHave you created a 'Named range' prior to implementing this script? The above blog post follows on from here where you need to create a 'Named range' called 'studentDetails': https://www.pbainbridge.co.uk/2019/04/protect-named-range-in-google-sheet.html
Thanks