简体   繁体   中英

Google Sheets: Hide rows in multiple sheets

I'm trying to hide rows across a multiple sheets. So I have a 6 sheets, but there are 4 similar sheets, the other one is more likely a summary of that 4 sheets and the other one is the sheet with a button. Here's a sample for your reference: LINK

It is working if I will put a HIDE ROWS button on sheets one by one, but it will take a lot of time for me to do it since I need to put it on different spreadsheets. My idea is to put a button per spreadsheet or upon opening it, the rows that are empty will be hidden. The SHOW ROWS button is perfectly working with it.

Here's the script that I'm using: LINK

PROBLEM:

  • Hide rows across multiple sheets with similar templates
  • Summary sheets rows should be hidden together with 4 sheets (not same rows)

I hope that someone can help me. Thank you in advance!

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:

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