Reputation: 1
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?
Upvotes: 0
Views: 499
Reputation: 38200
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.
Upvotes: 1