首页 > 解决方案 > 基于单元格值的 Google 表格保护

问题描述

我在一个谷歌工作表文件中有 30 个标签,它们都有相同的结构但不同的数据

我想要一个基于值锁定(保护)整个工作表的脚本

该值被定义为范围,如果该值是(已审核),我想锁定选项卡

标签: google-apps-scriptgoogle-sheets

解决方案


function protectAudited() {
  var ss=SpreadsheetApp.getActive();
  var shts=ss.getSheets();
  shts.forEach(function(sh) {
    if(sh.getRange('A5').getValue()=='audited') {
      sh.protect();
    }
  });
}

或许是这样的:

function protectAudited() {
  var ss=SpreadsheetApp.getActive();
  var shts=ss.getSheets();
  shts.forEach(function(sh) {
    if(sh.getRange('A5').getValue()=='Audited') {
      sh.protect();
    }else {
      var protection=sh.getProtections(SpreadsheetApp.ProtectionType.SHEET)[0];
      if(protection && protection.canEdit()) {
        protection.remove();
      }
    }
  });
}

推荐阅读