Search code examples
google-apps-scriptgoogle-sheetsgoogle-sheets-formula

Using this Script/formula for to get value for some selected Sheets


This script/formula add two adjacent columns together (Column F & G) and put it in column H for all sheets in the google sheets, I need further assistance in making it calculate only some selected sheets in google sheet.
enter image description here

function runMacro() {
  const sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
  sheets.forEach(addFormula_);
}

function addFormula_(_sh) {    const formulaString = '=Filter(F2:F+G2:G,F2:F<>"")'
  const theAddress = 'H2';
  _sh.getRange(theAddress).setFormula(formulaString);

}


Solution

  • // By Name - Only Specified Sheets
    function runMacro() {
    
      const includeSheets = [`Sheet1`, `Sheet2`, `Sheet3`, `etc`]
    
      SpreadsheetApp.getActiveSpreadsheet().getSheets()
                    .filter(sheet => includeSheets.includes(sheet.getName()))
                    .forEach(sheet => addFormula_(sheet))
    
    }