I believe your goal as follows.
- Using your script, you want to run the script for several sheets in a Google Spreadsheet, when a button is clicked.
- In your sample Spreadsheet, you want to run the script for the sheets of
"MS_Q1", "MS_Q2", "MS_Q3", "MS_Q4", "SUMMARY"
.
- For "SUMMARY" sheet, the start row is different from others. It's
7
.
Modification points:
- In this case, for the script, I would like to propose to modify the function
script_HideRows
.
- At first, the sheet names you want to run the script are set. And, using the sheet names, the script is run.
When above points are reflected to the script, it becomes as follows.
Modified script:
Please modify script_HideRows
of the script as follows.
function script_HideRows() {
var sheetNames = ["MS_Q1", "MS_Q2", "MS_Q3", "MS_Q4", "SUMMARY"]; // Please set the sheet names here. In this case, 4 sheets are used.
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.getSheets().forEach(sheet => {
var sheetName = sheet.getSheetName();
if (sheetNames.includes(sheetName)) {
if (sheetName == "SUMMARY") { // When the sheet is "SUMMARY", the start row is changed.
startRow = 7;
}
var numRows = sheet.getLastRow();
var elements = sheet.getRange(startRow, colToCheck, numRows).getValues();
for (var i=0; i<(numRows - startRow); i++) {
if (shouldHideRow(sheet, i, elements[i][0])) {
sheet.hideRows(startRow + i);
}
}
// Hide the rest of the rows
var totalNumRows = sheet.getMaxRows();
if (totalNumRows > numRows)
sheet.hideRows(numRows+1, totalNumRows - numRows);
}
});
}
Note:
- If the script is not changed even when you copied and pasted above script, please redeploy the Web Apps as new version.
References:
"MS_Q1", "MS_Q2", "MS_Q3", "MS_Q4"
, the hide rows are the same. Is my understanding correct? And. can I ask you about the detail ofnot same rows
ofSummary sheets rows should be hidden together with 4 sheets (not same rows)
? – Tanaike