I am sure this is an error on my part, but since the script works, not sure if this is a problem waiting to show up at some point. I have 11 tabs, and the script will run through the first 9 tabs and hide any rows without data, and then set the cursor for each tab to B4, except for tabs 10 and 11 will set to A1. But there is a red curly bracket and I am not sure how to correct it. One is at the end of the first line, the corresponding one is the line with }); Thanks for any help.
function onOpen() {
SpreadsheetApp.getActive().getSheets().forEach(function (s,index,array) { //for each sheet
if(index<9){ //if sortable page, not maps or docs
var nameOfSheet= array[index].getName()
var ssa = SpreadsheetApp.getActive().getSheetByName(nameOfSheet) //get active sheet
var max = ssa.getMaxRows()//max rows for this sheet
var last = ssa.getLastRow() //last row with data
ssa.hideRows(last+1, max-last)
}
if(index>8){
s.setActiveSelection("A1"); //set active cell for sheet to A1 if map or documentation sheet
SpreadsheetApp.flush(); // Force this update to happen before moving to next sheet
}else{
s.setActiveSelection("B4"); //set active cell for sheet to B4 for other sheets
SpreadsheetApp.flush(); // Force this update to happen before moving to next sheet
}
});
var sheet = SpreadsheetApp.getActive().getSheetByName('Active(Date)'); //set back to first sheet
var range = sheet.getRange('B4'); //put cursor on search box
range.activate();
}
The Monaco editor uses red highlight to indicate that the code lacks proper indentation. The code may work fine and have correct semantics, but unusual indentation makes it harder to follow the logic when reading the code.
To indent the code properly, press Control+Shift+I (Windows) or ⇧⌥F (macOS), or right click to display the contextual menu, then select Format Document. To view editor commands and keyboard shortcuts, press F1.
The code quoted in the question can be simplified and indented properly like this:
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();
}
Reference