简体   繁体   中英

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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