I have the following code:
function onOpen() {
const ss = SpreadsheetApp.getActive();
ss.getSheets().forEach((sheet, index) => {
if (index < 9) {
const lastRow = sheet.getLastRow();
const numRowsToHide = sheet.getMaxRows() - lastRow;
if (numRowsToHide) sheet.hideRows(lastRow + 1, numRowsToHide);
sheet.setActiveSelection('B4');
} else {
sheet.setActiveSelection('A1');
}
SpreadsheetApp.flush();
});
ss.getRange('Active(Date)!B4').activate();
}
It runs through 9 of 11 sheets of the spreadsheet and hides all the rows that don't have data and positions the active cell. It then sets the active sheet as the first one and sets the active cell. It works fine except the time runs around 29.5 sec give or take, so it occasionally times out (over 30 sec). Is there anything I can do to make it more time efficient?