简体   繁体   English

Google脚本-根据另一个工作表上的复选框隐藏/取消隐藏Google工作表

[英]Google script - Hide/Unhide google sheet based on checkbox on another sheet

I"m pretty sure I'm doing something wrong in the code, as to why it's not functioning the way I want it. Firstly, here's the code: 我很确定我在代码中做错了什么,以至于为什么它没有按照我想要的方式运行。首先,下面是代码:

function onEdit(a) {

var sheet = a.source.getActiveSheet();
var aa = SpreadsheetApp.getActiveSpreadsheet();
var COMP = aa.getSheetByName("COMP");
var COMPcell = sheet.getRange('B6').getValue();
if(COMPcell = 'TRUE'){COMP.showSheet();}else{COMP.hideSheet();}
}

In here, I have a checkbox on cell B6 of the 'active sheet' (named Monthly summary). 在这里,我在“活动工作表”的单元格B6上有一个复选框(名为“每月摘要”)。 When checked (and thus have a value of TRUE), I want the sheet named "COMP" to appear. 选中时(因此值为TRUE),我希望显示名为“ COMP”的工作表。 Otherwise, it should be hidden. 否则,应将其隐藏。 I'm not really good at coding and I've researched the above formula and modified it as to my requirement, but I can't get it to work. 我不太擅长编码,我已经研究了上面的公式,并根据需要对其进行了修改,但是我无法使其正常工作。

Any insights on this will be very much appreciated. 任何对此的见解将不胜感激。 Thanks! 谢谢!

  • You want to show the sheet of COMP only when the checkbox of "B6" is checked. 您只想在选中“ B6”复选框时显示COMP页。
  • You want to hide the sheet of COMP when the checkbox of "B6" is not checked. 如果未选中“ B6”复选框,则要隐藏COMP页。

If my understanding is correct, how about this modification? 如果我的理解是正确的,那么该修改如何? Please think of this as just one of several answers. 请认为这只是几个答案之一。

Modified script 1: 修改脚本1:

If your script is modified, in your script, COMPcell = 'TRUE' of the if statement is not compared the value. 如果修改了脚本, COMPcell = 'TRUE'在脚本中,不比较if语句的COMPcell = 'TRUE' In this case, please modify to COMPcell === true . 在这种情况下,请修改为COMPcell === true The modified script reflected this is as follows. 修改后的脚本反映如下。

function onEdit(a) {
  var sheet = a.source.getActiveSheet();
  var aa = SpreadsheetApp.getActiveSpreadsheet();
  var COMP = aa.getSheetByName("COMP");
  var COMPcell = sheet.getRange('B6').getValue();
  if (COMPcell === true) { // Modified
    COMP.showSheet();
  } else {
    COMP.hideSheet();
  }
}

Modified script 2: 修改脚本2:

When the event object and isChecked() are used, your script can also be modified as follows. 当使用事件对象和isChecked() ,还可以如下修改脚本。 In this script, only when the checkbox of "B6" is edited, the script works. 在此脚本中,仅当编辑“ B6”复选框时,脚本才起作用。

function onEdit(a) {
  var range = a.range;
  if (range.getA1Notation() == "B6") {
    var COMP = a.source.getSheetByName("COMP");
    if (range.isChecked()) {
      COMP.showSheet();
    } else {
      COMP.hideSheet();
    }
  }
}

References: 参考文献:

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

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