首页 > 解决方案 > 根据单元格值保护多个范围

问题描述

我是一个使用谷歌脚本的新手,并且一直在查看其他关于如何根据值保护单元格的示例。到目前为止,我已经设法让它工作,但我想不出如何更新它来保护不仅仅是那个范围。

我制作了一张表格,经理在其中要求员工参加会议的时间段。我使用函数来检查是否错过了最后期限,如果是,则无法再更新右侧的行。我想对工作表上的所有行执行此操作,但我不知道如何更新下面的脚本以包含其余行。我也会在其他选项卡上执行此操作,因此如果将来有办法证明这一点,那将很有帮助。

我的目标是:

如果单元格 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();
      }
    }
  }  
} 

标签: protection

解决方案


推荐阅读