Skip to content
Advertisement

How to get all sheets by openById while stand-alone

I am trying to make a stand-alone script to action for another spreadsheet. It’s working for the first sheet only, but it’s not working for all sheets.

function doSomething(){
    var ssID = "123abc";
    var ss = SpreadsheetApp.openById(ssID);

    //var sheet = ss.getActiveSpreadsheet().getSheets(); I also
    //   tried it, but it gave an error.
    var sheet = ss.getActiveSheet();

    var range = sheet.getRangeList(['A3:A','D3:D']);
    range.setFontFamily("Arial");
    // The rest. Do something
}

I need an example to work all sheets and need work all except individual sheets.

Advertisement

Answer

If you want to loop through all the sheets and set the font for the range it can be done this way:

function doSomething(){
  var ssID = "123abc";
  var ss = SpreadsheetApp.openById(ssID);
  var sheets = ss.getSheets();
  var sheets_to_skip = ['aaa', 'bbb']; // names of the sheets you want to skip

  sheets.forEach(sheet => {
    if (!sheets_to_skip.includes(sheet.getName())) {
      var range = sheet.getRangeList(['A3:A','D3:D']);
      range.setFontFamily("Arial");
    }
  })
}
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement