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

3

22

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?

Omegaomelet answered 10/5, 2017 at 23:48 Comment(0)
N
11

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() );
Nari answered 11/5, 2017 at 16:20 Comment(1)
I tested in the Script Editor with SpreadsheetApp.getActive() === SpreadsheetApp.getActiveSpreadsheet(), and the answer was false, any ideas?Prevalent
N
3

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?)

Nikethamide answered 15/5, 2021 at 19:38 Comment(1)
Strange, it's other way around for me - object returned by getActiveSheet() has getCharts(), but doesn't have getRangeByName(). The object is actually of a class Sheet, not Spreadsheet (as returned by getActive() or getActiveSpreadsheet()). Besides, the question in OP wasn't about getActiveSheet() anyway.Upend
C
0

Following this article, function getActiveSpreadsheet() is deprecated.

So it's better to use function getActive().

Crinoline answered 19/3 at 19:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.