Make Google Sheets Script Work on Multiple Tabs – Google Apps Script

google sheetsgoogle-apps-script

Below is my script or code:

function myFunction() {

}
function onEdit() {
  var s = SpreadsheetApp.getActive().getSheetByName('Sheet1');
  s.showRows(1, s.getMaxRows());

  s.getRange('H:H')
    .getValues()
    .forEach( function (r, i) {
    if (r[0] == 'Complete') 
      s.hideRows(i + 1);
    });
}

I would like this script to work on multiple tabs such as Sheet2, Sheet3, etc.

I have looked all over for a script that would work with mine to make it work on multiple tabs. If you know how to do this, please help!

Best Answer

Replace

var s = SpreadsheetApp.getActive().getSheetByName('Sheet1');

by

var s = SpreadsheetApp.getActiveSheet();

The above line will assign the sheet that was edited to s.