Home > Software design >  Looping a function over different sheets in google scripts
Looping a function over different sheets in google scripts

Time:05-13

so I have function lets call that XXXX . That function works over the course of one sheet only, trying to get it to work over all sheets as long as the sheet's name isn't "YYYY" or the sheet name doesn't include "ZZZZ" . What I have below runs our function (XXXX) on the first sheet then activates the last without running it over the sheets between the first and the last. Any help would be appreciated.

 function run_XXXX_for_all_tabs(){
 
 var spreadsheet = SpreadsheetApp.getActive();
 var allSheets =spreadsheet.getSheets();

  allSheets.forEach(function(sheet){
    if(sheet.getSheetName() !== "YYYY" && sheet.getSheetName().search("ZZZZ") < 0){
      sheet.activate();
      XXXX();
    }
  })
}

CodePudding user response:

Try this:

function myFunction() {

    SpreadsheetApp.getActiveSpreadsheet()
                  .getSheets()
                  .filter(i => i.getName() !== `YYYY` && !i.getName().includes(`ZZZZ`))
                  .forEach(i => {
                    i.activate()
                    XXXX()
                  })

}

enter image description here enter image description here

  • Related