protection - 根据单元格值保护多个范围
问题描述
我是一个使用谷歌脚本的新手,并且一直在查看其他关于如何根据值保护单元格的示例。到目前为止,我已经设法让它工作,但我想不出如何更新它来保护不仅仅是那个范围。
我制作了一张表格,经理在其中要求员工参加会议的时间段。我使用函数来检查是否错过了最后期限,如果是,则无法再更新右侧的行。我想对工作表上的所有行执行此操作,但我不知道如何更新下面的脚本以包含其余行。我也会在其他选项卡上执行此操作,因此如果将来有办法证明这一点,那将很有帮助。
我的目标是:
如果单元格 A3 显示“错过最后期限”,请保护 C3:C
如果单元格 A4 显示“错过最后期限”,请保护 C4:C
任何等等
工作表的样子: 截图
到目前为止我的脚本:
function CellProtect() {
var ss = SpreadsheetApp.getActive();
var source = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("HertsV2");
var cell = source.getRange("A3").getValue();
var range = ss.getRange('C3:3');
var protections = source.getProtections(SpreadsheetApp.ProtectionType.RANGE);
if (cell == "Deadline Missed" && protections.length==1) {
// Protect range C3:3 if cell 'A3' = Deadline Missed
var protection = range.protect().setDescription('HertsV2 August Protect');
protection.removeEditors(protection.getEditors());
} else {
// Remove protection if cell 'A3' is anything other than Deadline Missed
var protections = ss.getProtections(SpreadsheetApp.ProtectionType.RANGE);
for (var i = 0; i < protections.length; i++) {
var protection = protections[i];
if(protection.getRange().getRow() == range.rowStart) {
protection.remove();
}
}
}
}
解决方案
推荐阅读
- ios - resignFirstResponder() 的模糊使用
- azureservicebus - 如何查看 Azure 服务总线的用户错误日志?
- java - 即使在 Ubuntu 中安装后 Java 也无法运行
- azure - 将 Azure API for FHIR 数据复制到 datalake
- c++ - 从类方法调用朋友函数
- nginx - 启用 ssl 时,nginx 不再重写重定向位置
- python - 测试目录中的 Python 导入
- html - 我想在点击时使用 javascript 或 jquery 将动画播放状态更改为运行。任何帮助都会得到帮助
- javascript - 如何尽快运行 JavaScript 函数?
- python - 我如何抓取 Aliexpress 产品数据