【发布时间】:2021-05-18 02:49:02
【问题描述】:
每天创建 2-3 张,但至少要制作一张 这些范围正在保护 ["B3:U27", "W3:AP27", "B29:U33", "W29:AP33"]
我将 42 个范围减少到这 4 个范围以使其更快,但仍然 在 1 分钟内它可以保护大约 8 个文件问题是在几个月内它可以增长超过 100 个文件 这将使我达到 6 分钟的超时限制,并且会中断脚本。
这是我目前正在使用的脚本。 我想知道是否可以通过某种方式对其进行修改以忽略已经受保护的工作表?
function main(){ //Main function to run
var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
var disregard = ["List", "Data", "Template"]; //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{
unlockCertainRanges(sheets[x]);
}
}
}
function unlockCertainRanges(currentSheet){ //Function to unlock certain ranges on your spreadsheet
var sheet = currentSheet;
// 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 = ["B3:U27", "W3:AP27", "B29:U33", "W29:AP33"]; // 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
});
}
它可以是已经受到保护的东西,或者上面有挂锁不能触摸吗?
我试图找到一种方法来检索已受保护的工作表的名称。
我的意思是 getSheetName() 之类的东西,但适用于受保护的对象。
或者如果这个描述已经有这样的保护,可以把它放在例外中?
setDescription('已经保护');
我没有太多的编码经验;我找到了a very similar question,但我并没有看懂很多代码
有人有想法吗?
【问题讨论】:
-
可以看
Sheet.getProtections() -
@MetaMan 如果你是这个意思,删除这个部分也会重新扫描
// 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(); } -
您的问题是“如何使用脚本忽略已受保护的工作表?”您可以查看每个工作表是否具有保护,并且只返回没有保护的工作表的名称。
标签: google-apps-script google-sheets