Home > Enterprise >  Moving a spreadsheet tab in standalone Google Apps script
Moving a spreadsheet tab in standalone Google Apps script

Time:05-29

I'm working on a standalone Google Apps script that (among other things) moves a particular tab in a Sheets workbook to the leftmost position. When the script is bound to a spreadsheet, the following code works just fine:

      var tab = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Current');
      SpreadsheetApp.setActiveSheet(tab);
      SpreadsheetApp.getActiveSpreadsheet().moveActiveSheet(1);

I can't figure out what I need to do to make it work in a standalone script. Any ideas?

Thanks in advance!

CodePudding user response:

You will need to 'open' the Spreadsheet in order to access it.

Although the Spreadsheet does not physically open, you will be able to modify it as normal using:

SpreadsheetApp.openById(...)

Or

SpreadsheetApp.openByUrl(...)

You will however, need to avoid using the 'active' methods, instead, opt for .getSheetByName() and related methods found here:

Spreadsheet Class

If you have further questions, please feel free to comment below!

CodePudding user response:

Move A Sheet to position zero from Standalone Script

function moveSheetToZero(name="Current") {
  const dss = SpreadsheetApp.openById(gobj.globals.testsourceid);
  const sh = dss.getSheetByName(name).activate();
  dss.moveActiveSheet(0);
}
  • Related