简体   繁体   English

在大型电子表格中,基于单元格值隐藏行会生成错误“超出最大执行时间”

[英]Hiding row based on cell value, in large spreadsheet generates error “Exceeded Maximum Execution Time”

I am attempting to hide rows based on a value in the cell. 我试图根据单元格中的值隐藏行。

I am using Google Sheets as an Inventory for items. 我正在使用Google表格作为商品库存。 On the last column if I mark it "Yes" I'd like the whole row to hide. 在最后一列上,如果我将其标记为“是”,则希望隐藏整行。

The code works on a template sheet, but when I try to activate it on my main Inventory it says "Exceeded Maximum Execution Time", which I would assume is happening because it is quite a large Inventory. 该代码在模板工作表上有效,但是当我尝试在主库存上激活它时,它会显示“超出最大执行时间”,我认为这是正在发生的原因,因为这是一个相当大的库存。

function onEdit() {

var ss = SpreadsheetApp.getActiveSpreadsheet();

var sheet = ss.getActiveSheet();

  var lastRow = sheet.getLastRow();

  for( i=1 ; i<=lastRow ; i++) { // i <= lastRow

      var status = sheet.getRange("AD"+i).getValue();

      if (status == "Yes") {

         sheet.hideRows(i);
    } 
   }
  }

I do see that on the Triggers you can set it up to be Time-driven. 我确实看到在触发器上可以将其设置为受时间驱动。

How can I make the code not exceed the 5 minute Maximum Time? 如何使代码不超过5分钟的最长时间?

As your script use the reserved function name onEdit, it's triggered each time an edit occurs and each time it loops through all the sheet rows and if the condition is met it hides the row even if it's already hidden. 当脚本使用保留的函数名称onEdit时,每次进行编辑并且每次循环遍历所有工作表行时都会触发该脚本,如果满足条件,则即使该行已经被隐藏,它也会将其隐藏。

Change your script to check the currently edited cell, use en on edit event object for that and only hide the current row if the condition is met. 更改脚本以检查当前已编辑的单元格,在编辑事件对象上使用en,并且仅在满足条件时隐藏当前行。 Something like the following: 类似于以下内容:

function onEdit(e){
   if(e.range.getColumn() === 30 && e.value === 'Yes'){
     e.range.getSheet().hideRows(e.range.getRow())
   }
}

NOTE: I didn't test it yet. 注意:我尚未测试。

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

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