简体   繁体   English

根据复选框显示和隐藏行

[英]Show & Hide Rows Based on Check Boxes

I will begin by saying that I am incredibly inexperienced with coding, so any help would be greatly appreciated!我首先要说的是,我对编码非常缺乏经验,因此将不胜感激任何帮助!

I am trying to show/hide rows based on whether a checkbox is ticked or not.我试图根据是否勾选复选框来显示/隐藏行。 For example, I have a checkbox in A5 - If it is unchecked, I want it to hide rows 6:18, and if it is checked, I want it to show rows 6:18.例如,我在 A5 中有一个复选框 - 如果未选中,我希望它隐藏 6:18 行,如果选中,我希望它显示 6:18 行。

The same goes for a checkbox in A19, to hide/show rows 20:26 respectively. A19 中的复选框也是如此,分别隐藏/显示 20:26 行。 A27 for rows 28:37 etc. A27 用于行 28:37 等。

I have found a previous forum post, in which I will link the code below, however the first checkbox is hiding ALL rows (Eg. if A5 is unchecked, it is hiding rows 6:18, 20:26 AND 28:37. When it is then checked, it is only showing rows 6:18.我找到了以前的论坛帖子,我将在其中链接下面的代码,但是第一个复选框隐藏所有行(例如,如果未选中 A5,则隐藏行 6:18、20:26 和 28:37。当然后检查它,它只显示 6:18 行。

function onEdit(e) {
  var cfg = { // Please set this object.
    A5: {startRow: 6, endRow: 18},
    A19: {startRow: 20, endRow: 26},
    A27: {startRow: 28, endRow: 37},
  };

  var activeRange = e.range.getA1Notation();
  var ranges = Object.keys(cfg);
  if (cfg[activeRange]) {
    var sheet = e.source.getActiveSheet();
    var values = sheet.getRange(ranges[0] + ":" + ranges[ranges.length - 1]).getValues();
    values.forEach(function(e, i) {
      if (e[0]) {
        sheet.showRows(cfg[ranges[i]].startRow, cfg[ranges[i]].endRow - cfg[ranges[i]].startRow);
      } else {
        sheet.hideRows(cfg[ranges[i]].startRow, cfg[ranges[i]].endRow - cfg[ranges[i]].startRow);
      }
    });
  }
}

As discussed, the first checkbox is hiding ALL rows (Eg. if A5 is unchecked, it is hiding rows 6:18, 20:26 AND 28:37. When it is then checked, it is only showing rows 6:18.如前所述,第一个复选框隐藏所有行(例如,如果未选中 A5,则隐藏行 6:18、20:26 和 28:37。然后选中时,它仅显示行 6:18。

  • You want to hide the rows from 6 to 18, when the checkbox of "A5" is unchecked. 如果未选中“ A5”复选框,则要隐藏6至18的行。
  • You want to hide the rows from 20 to 26, when the checkbox of "A19" is unchecked. 当未选中“ A19”复选框时,您想隐藏20至26行。
  • You want to hide the rows from 28 to 37, when the checkbox of "A27" is unchecked. 当未选中“ A27”复选框时,您想隐藏28至37之间的行。
  • When the checkbox is checked, you want to show the rows. 选中复选框后,您要显示行。
  • You want to achieve above using the OnEdit event trigger with Google Apps Script. 您希望通过将OnEdit事件触发器与Google Apps脚本结合使用来实现上述目的。

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

From: 从:

var values = sheet.getRange(ranges[0] + ":" + ranges[ranges.length - 1]).getValues();
values.forEach(function(e, i) {
  if (e[0]) {

To: 至:

var values = sheet.getRange(ranges[0] + ":" + ranges[ranges.length - 1]).getValues().filter(String); // Modified
values.forEach(function(e, i) {
  if (e[0] === true) { // Modified

If I misunderstood your question and this was not the result you want, I apologize. 如果我误解了您的问题,而这不是您想要的结果,我深表歉意。

Tanaike code did not work for me if the rows were not empty. 如果行不为空,Tanaike代码对我不起作用。 This worked for me: 这对我有用:

 function onEdit(e) { var cfg = { // Please set this object. A5: {startRow: 6, endRow: 18}, A19: {startRow: 20, endRow: 26}, A27: {startRow: 28, endRow: 37}, }; var activeRange = e.range.getA1Notation(); var sheet = e.source.getActiveSheet(); var ranges = Object.keys(cfg); var cells = [] for(var j = 0; j < ranges.length; j++){ cells.push(sheet.getRange(ranges[j])) } if (cfg[activeRange]) { cells.forEach(function(cell, i) { if (cell.getValue()) { sheet.showRows(cfg[ranges[i]].startRow, cfg[ranges[i]].endRow - cfg[ranges[i]].startRow); } else { sheet.hideRows(cfg[ranges[i]].startRow, cfg[ranges[i]].endRow - cfg[ranges[i]].startRow); } }); } } 

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

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