【发布时间】:2019-08-14 15:01:35
【问题描述】:
我有一张受一些未受保护范围保护的工作表。
在作为管理员的第一阶段,我使用 SCRIPT 1 取消保护某些范围。不幸的是,这大约需要 5 分钟。执行脚本。
用户可以在工作表中添加一行,脚本应该取消保护添加行中的某些单元格。问题是当已经有一些范围不受保护时,我不知道如何取消保护范围。此时,当用户添加一个行脚本时,会运行一个 SCRIPT 1,它会遍历整个工作表,不幸的是它需要太多时间......
脚本 1 - 浏览整个文档(大约 5 分钟)
function protect(sheet) {
var protection = sheet.protect().setDescription('Protected sheet');
var arrayRanges = new Array;
for(var i=1;i<sheet.getLastRow();i++){
if(sheet.getRange(i,50).getValue() == "s"){
range = sheet.getRange(i,3,1,5);
arrayRanges.push(range);
} else if (sheet.getRange(i,50).getValue() == "d"){
range = sheet.getRange(i,2,1,6);
arrayRanges.push(range);
} else if (sheet.getRange(i,50).getValue() == "r"){
range = sheet.getRange(i,12,1,26);
arrayRanges.push(range);
}
}
protection.setUnprotectedRanges(arrayRanges);
protection.addEditor("xxx@gmail.com");
protection.removeEditors(protection.getEditors());
if (protection.canDomainEdit()) {
protection.setDomainEdit(false);
}
}
我尝试使用 SCRIPT 2 添加新的不受保护的范围,但它的工作方式是擦除 SCRIPT 1 的结果,只留下 SCRIPT 2 的结果。
SCRIPT 2 - 添加新的不受保护的范围
function protect(sheet) {
var protection = sheet.protect().setDescription('Protected sheet');
var arrayRanges = new Array;
var range = sheet.getRange(1,3,1,5);
arrayRanges.push(range);
protection.setUnprotectedRanges(arrayRanges);
protection.addEditor("xxx@gmail.com");
protection.removeEditors(protection.getEditors());
if (protection.canDomainEdit()) {
protection.setDomainEdit(false);
}
}
我通过 WebApp 运行脚本。
【问题讨论】:
-
为什么是"protection.addEditor("xxx@gmail.com");"行在“protection.removeEditors(protection.getEditors());”行之前?这没有任何意义..