简体   繁体   English

如何使我的“显示和隐藏行”脚本在 Google 表格中正常运行

[英]How to make my 'Show and hide rows' script function properly in Google Sheets

I am trying to have a script in (google sheets) sheet "Info Pull" show/hide rows based on the contents of Column C.我试图在(谷歌表格)工作表“信息拉取”中使用一个脚本,根据 C 列的内容显示/隐藏行。

Currently I am running the following formula in Column C:目前我在 C 列中运行以下公式:

=if(or(len(H47)>0,len(I47)>0,len(J47)>0,len(K47)>0),"unhide","hide")

which produces either 'hide' or 'unhide' depending on whether or not cells H,I,J,and K 47 are returning a value.根据单元格 H、I、J 和 K 47 是否正在返回值,它会产生“隐藏”或“取消隐藏”。

If any of the aforementioned cells return a value, the corresponding cell in Column C turns to 'unhide' and vice-versa if all the cells return no value.如果上述任何单元格返回值,则 C 列中的相应单元格将变为“取消隐藏”,如果所有单元格均未返回值,则反之亦然。

I've been trying to have this script hide/unhide the rows based on the contents of Column C however, the script is only partially functioning.我一直在尝试让这个脚本根据 C 列的内容隐藏/取消隐藏行,但是,该脚本只是部分运行。

If I actively type 'hide' into a C cell, the row will hide.如果我主动在 C 单元格中键入“隐藏”,该行将隐藏。 However, if I paste 10 rows worth of 'hide' into 10 C cells, only the first row will hide.但是,如果我将 10 行的“隐藏”粘贴到 10 个 C 单元格中,则只有第一行会隐藏。 Likewise, the script is not re-evaluating and hiding/unhiding based off of the changing results of the IF formula.同样,脚本不会根据 IF 公式的变化结果重新评估和隐藏/取消隐藏。 If I depopulate cell H47, turning C47 to hide, the cell does not hide.如果我减少单元格 H47 的数量,将 C47 转为隐藏,则该单元格不会隐藏。

Also, if I set cell A1 to 'hide' and paste '=$A$1' in a group of cells in Column C, only the first cell in the group will hide and it will not unhide if I set A1 to 'unhide'.此外,如果我将单元格 A1 设置为“隐藏”并将“=$A$1”粘贴到 C 列的一组单元格中,则只有该组中的第一个单元格会隐藏,如果我将 A1 设置为“取消隐藏”,则不会取消隐藏. I think this may have something to do with my trigger but I am relatively new to scripting and am unsure.我认为这可能与我的触发器有关,但我对脚本编写比较陌生并且不确定。

function onEdit(e) {
  hideAndShowRows(e);
}

    function hideAndShowRows(e) {

  var sheetsToWatch = ['Info Pull'];
  var columnToWatch = 3;

  var sheet = e.range.getSheet();
  if (sheetsToWatch.indexOf(sheet.getName()) < 0)  {
    return;
  }
  var editedRow = e.range.getRow();
  var cellToWatch = sheet.getRange(editedRow, columnToWatch);
  if (cellToWatch.getValue().toLowerCase() === 'hide') {
    sheet.hideRows(editedRow);
  }
  if (cellToWatch.getValue().toLowerCase() === 'unhide') {
    sheet.showRows(editedRow);
  }
}

I would like the cells to hide/unhide based off of whether or not the formula in Column C returns hide/unhide.我希望单元格根据 C 列中的公式是否返回隐藏/取消隐藏来隐藏/取消隐藏。

Your code is "failing" because it only gets the row number of the top-left cell of the edited range您的代码“失败”,因为它只获取编辑范围左上角单元格的行号

var editedRow = e.range.getRow();

and

var cellToWatch = sheet.getRange(editedRow, columnToWatch);

You could use the following properties to get the range dimensions您可以使用以下属性来获取范围维度

e.range.rowStart
e.range.columnStart
e.range.rowEnd
e.range.columnEnd

or to get the reference of the edited range use或获取编辑范围使用的参考

e.range.getA1Notation();

Related有关的

Here is a modified function that will solve few issues:这是一个修改后的函数,可以解决几个问题:

function onEdit(e) {
  hideAndShowRows(e);
}

    function hideAndShowRows(e) {

  var sheetsToWatch = ['Setup'];
  var columnToWatch = 3;

  var sheet = e.range.getSheet();
  if (sheetsToWatch.indexOf(sheet.getName()) < 0)  {
    return;
  }
  var editedRow = e.range.getRow();
  var numOfRows = e.range.getNumRows()
  for (var i = 0; i<numOfRows ; i++){
  var cellToWatch = sheet.getRange(editedRow + i, columnToWatch);
  if (cellToWatch.getValue().toLowerCase() === 'hide') {
    sheet.hideRows(editedRow + i);
  }
  if (cellToWatch.getValue().toLowerCase() === 'unhide') {
    sheet.showRows(editedRow + i);
  }
  }
}

Firstly, your code only works on the row that was edited, it doesn't look for any other row for changes.首先,您的代码仅适用于已编辑的行,它不会查找任何其他行进行更改。 Specifically this line:特别是这一行:

var editedRow = e.range.getRow()

e is a event object passed to simple triggers, which contain info like which range was edited was in this case. e是传递给简单触发器的事件对象,其中包含在这种情况下编辑的范围等信息。 The above function getRow() gets the first row in the range which was edited, hence when you edit multiply rows only the first one gets hidden.上面的函数getRow()获取已编辑范围中的第一行,因此当您编辑乘法行时,只有第一行被隐藏。 To get all the rows that were edited you will have use getNumRows() and loop through each one of them like so:要获取所有已编辑的行,您将使用getNumRows()并循环遍历其中的每一行,如下所示:

var editedRow = e.range.getRow();
  var numOfRows = e.range.getNumRows()
  for (var i = 0; i<numOfRows ; i++){
  var cellToWatch = sheet.getRange(editedRow + i, columnToWatch);
  if (cellToWatch.getValue().toLowerCase() === 'hide') {
    sheet.hideRows(editedRow + i);
  }
  if (cellToWatch.getValue().toLowerCase() === 'unhide') {
    sheet.showRows(editedRow + i);
  }
  }

Finally, a caveat when you point all the cells to A1 and only edit row A (just cell A1), the program will only look for changes in that row ie row A. You can find more resource here .最后,当您将所有单元格指向 A1 并且仅编辑 A 行(仅单元格 A1)时,需要注意的是,程序将仅查找该行(即 A 行)中的更改。您可以在此处找到更多资源。

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

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