简体   繁体   English

基于单元格值的 Google 工作表保护

[英]Google sheet protect based on cell value

I have like 30 tabs in one google sheet file and all of them have the same structure but different data我在一个谷歌表格文件中有 30 个标签,它们都具有相同的结构但数据不同

I want a script that locks (protect) the whole sheet based on the value我想要一个根据值锁定(保护)整个工作表的脚本

this value is defined as range and I want to lock the tab if the value is (audited)此值被定义为范围,如果该值是(已审核),我想锁定选项卡

  • cell position : A5单元格位置:A5
  • cell value : "audited"单元格值:“已审核”
function protectAudited() {
  var ss=SpreadsheetApp.getActive();
  var shts=ss.getSheets();
  shts.forEach(function(sh) {
    if(sh.getRange('A5').getValue()=='audited') {
      sh.protect();
    }
  });
}

Perhaps like this:也许像这样:

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();
      }
    }
  });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM