Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between getActive() and getActiveSpreadsheet() in Apps Script?

Whats the difference between these two methods from class SpreadSheetApp in Google Apps Script?

Both return "currently active spreadsheet, or null if there is none". Whats the difference between them, exactly? When should I use each one?

like image 457
nanquim Avatar asked May 10 '17 23:05

nanquim


People also ask

What is SpreadsheetApp?

SpreadsheetApp. This class allows users to open Google Sheets files and to create new ones.

What does SpreadsheetApp getActiveSheet do?

getActiveSheet()Gets the active sheet in a spreadsheet. The active sheet in a spreadsheet is the sheet that is being displayed in the spreadsheet UI.

How do I get the active sheet name in Google script?

How to get the name of the Google Sheets spreadsheet using Apps Script? To get the name of the Google Sheets spreadsheet, we need to first get a reference to the spreadsheet and then use the getName() method of the Spreadsheet object to get its name.


3 Answers

For me they're quite interchangeable really. Both return Spreadsheet object and has access to methods available to Class Spreadsheet. Feel free to use whichever you prefer.

Same results:

 Logger.log("getActiveSpreadSheet() "+SpreadsheetApp.getActiveSpreadsheet().getUrl());
 Logger.log("getActive() "+ SpreadsheetApp.getActive().getUrl() );
like image 165
noogui Avatar answered Oct 23 '22 13:10

noogui


They don't seem to return a pointer to the same animal. I don't know why (don't have time to investigate) but

  1. The sheet returned by getActiveSheet() has the getRangeByName() method - and the one from getActive() doesn't have it
  2. The sheet returned by getActiveSheet() doesn't have the getCharts() method - and the one from getActive() does have it

Either something really over my head makes this justifiable or it is just dismal language/syntax design (maybe some legacy crap that never got resolved?)

like image 42
Code Curious Avatar answered Oct 23 '22 14:10

Code Curious


For me they're quite interchangeable really. Both return Spreadsheet object and has access to methods available to Class Spreadsheet. Feel free to use whichever you prefer.

Same results:

 Logger.log("getActiveSpreadSheet() "+SpreadsheetApp.getActiveSpreadsheet().getUrl());
 Logger.log("getActive() "+ SpreadsheetApp.getActive().getUrl() );
like image 41
ReyAnthonyRenacia Avatar answered Oct 23 '22 12:10

ReyAnthonyRenacia