隐藏一行以及后续内容,根据一个包含脚本的复选框。

huangapple go评论64阅读模式
英文:

How can I hide a line and some following, depending a tick box with a script

问题

我一直在创建一个小脚本,根据复选框来隐藏行。
如果复选框为“true”,我想隐藏它,但问题是我还想隐藏这个复选框和下一个复选框之间的所有行。

我的电子表格看起来是这样的:
点击这里查看图像描述

到目前为止,我编写的代码只隐藏第一行(带有复选框的行),如下所示:

function HIDE_OVER_PROJ() {
  var REA_PROJsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("TEST_PROJETS");
  var over = REA_PROJsheet.getRange(2, 3, REA_PROJsheet.getLastRow()).getValues();
  Logger.log(over)
  for (let i = 0; i < over.length; i++) {
    if (over[i][0] == (true)) {
      REA_PROJsheet.hideRows([i + 2]);
    }
  }
}

我可以怎么做来隐藏两个复选框之间的其他行,知道有时可能有10行,有时只有2行,还有时可能有7行... 谢谢。

英文:

I have been creating a small script to hide rows depending a tick box.
If the tick box is "true", I want to hide it, but the point is that I also want to hide all the next lines between this tick box and the next one.

my spreadsheet is looking like that :
enter image description here

and the code I did so far and who is hidding only the first line (the one with the tick box) is looking like this :

function HIDE_OVER_PROJ() {
  

  var REA_PROJsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(&quot;TEST_PROJETS&quot;);



  var over = REA_PROJsheet.getRange(2, 3, REA_PROJsheet.getLastRow()).getValues();
  Logger.log(over)
  for (let i = 0; i &lt; over.length; i++) {
    if (over[i][0] == (true)) {
      REA_PROJsheet.hideRows([i + 2]);
      }
  }

}

what can I do to mask the other lines between the 2 tick box, knowing that sometimes there is 10lines, sometimes only 2, some other times 7...
thanks

答案1

得分: 0

这是您需要的内容,您还必须添加一个触发器,以便每次编辑列时执行该函数。

function HIDE_OVER_PROJ() {
  var REA_PROJsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("TEST_PROJETS");
  var over = REA_PROJsheet.getRange(2, 3, REA_PROJsheet.getLastRow() - 1).getValues();

  var startRow = -1;
  var hideRows = false;

  REA_PROJsheet.showRows(2, REA_PROJsheet.getLastRow() - 1);

  for (let i = 0; i < over.length; i++) {
    if (over[i][0] === true) {
      if (!hideRows) {
        startRow = i + 2;
      }
      hideRows = true;
    } else if (over[i][0] === false && hideRows) {
      if (startRow !== -1) {
        REA_PROJsheet.hideRows(startRow, i - startRow + 1);
      }
      startRow = -1;
      hideRows = false;
    }

    if (hideRows && over[i][0] === "") {
      REA_PROJsheet.hideRows(i + 2);
    }
  }

  if (startRow !== -1) {
    REA_PROJsheet.hideRows(startRow, over.length - startRow + 2);
  }
}

注意:以上代码已翻译并返回,不包括注释部分。

英文:

this is what you need, you must also add a trigger, so that every time you edit the column the function is executed.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function HIDE_OVER_PROJ() {
  var REA_PROJsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(&quot;TEST_PROJETS&quot;);
  var over = REA_PROJsheet.getRange(2, 3, REA_PROJsheet.getLastRow() - 1).getValues();

  var startRow = -1;
  var hideRows = false;

  REA_PROJsheet.showRows(2, REA_PROJsheet.getLastRow() - 1);

  for (let i = 0; i &lt; over.length; i++) {
    if (over[i][0] === true) {
      if (!hideRows) {
        startRow = i + 2;
      }
      hideRows = true;
    } else if (over[i][0] === false &amp;&amp; hideRows) {
      if (startRow !== -1) {
        REA_PROJsheet.hideRows(startRow, i - startRow + 1);
      }
      startRow = -1;
      hideRows = false;
    }

    if (hideRows &amp;&amp; over[i][0] === &quot;&quot;) {
      REA_PROJsheet.hideRows(i + 2);
    }
  }

  if (startRow !== -1) {
    REA_PROJsheet.hideRows(startRow, over.length - startRow + 2);
  }
}

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月14日 05:10:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76244892.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定