如何使我的“显示和隐藏行”脚本在Google表格中正常运行 [英] How to make my 'Show and hide rows' script function properly in Google Sheets

查看:105
本文介绍了如何使我的“显示和隐藏行”脚本在Google表格中正常运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在(Google工作表)工作表 Info Pull中基于C列的内容显示/隐藏行的脚本。



当前我在列C中运行以下公式:

  = if(or(len(H47)> 0,len(I47)> ; 0,len(J47)> 0,len(K47)> 0),取消隐藏,隐藏)

根据单元格H,I,J和K 47是否返回值而产生隐藏或取消隐藏。



<如果任何上述单元格返回值,则列C中的相应单元格将变为取消隐藏,如果所有单元格均未返回值,则反之亦然。



我一直试图让此脚本根据C列的内容隐藏/取消隐藏行,但是该脚本仅部分起作用。



如果我在C单元格中主动键入 hide,该行将隐藏。但是,如果我将10行隐藏粘贴到10个C单元格中,则只会隐藏第一行。同样,脚本不会根据IF公式的更改结果重新评估和隐藏/取消隐藏。如果我将单元格H47人口减少,将C47转为隐藏,则该单元格不会隐藏。



此外,如果我将单元格A1设置为隐藏并将 = $ A $ 1粘贴到C列的一组单元格中,则仅该组中的第一个单元格如果我将A1设置为取消隐藏,它将隐藏并且不会取消隐藏。我认为这可能与触发器有关,但是我对脚本编写还比较陌生,不确定。

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

函数hideAndShowRows(e){

varsheetToWatch = [’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()==='取消隐藏'){
sheet.showRows(editedRow);
}
}

我希望这些单元格基于隐藏/取消隐藏C列中的公式是否返回隐藏/取消隐藏。

解决方案

这是一个修改后的函数,可以解决几个问题:

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

函数hideAndShowRows(e){

varsheetToWatch = [‘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);
}
}
}

首先,您的代码仅适用在已编辑的行上,它不会寻找任何其他行进行更改。特别是这一行:

  var editedRow = e.range.getRow()

e 事件对象传递给简单触发器,在这种情况下,该触发器包含诸如编辑范围的信息。
上面的函数 getRow()获取已编辑范围内的第一行,因此,当您编辑乘法行时,只会隐藏第一行。要获取所有已编辑的行,您将使用 getNumRows()并像这样遍历每个行:

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

最后,当您将所有单元格指向A1并仅编辑A行(仅A1单元格),程序只会在该行即A行中查找更改。您可以找到更多资源此处


I am trying to have a script in (google sheets) sheet "Info Pull" show/hide rows based on the contents of Column C.

Currently I am running the following formula in Column 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.

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.

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.

If I actively type 'hide' into a C cell, the row will hide. However, if I paste 10 rows worth of 'hide' into 10 C cells, only the first row will hide. Likewise, the script is not re-evaluating and hiding/unhiding based off of the changing results of the IF formula. If I depopulate cell H47, turning C47 to hide, the cell does not hide.

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'. 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.

解决方案

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. 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. To get all the rows that were edited you will have use getNumRows() and loop through each one of them like so:

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 i.e row A. You can find more resource here.

这篇关于如何使我的“显示和隐藏行”脚本在Google表格中正常运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆