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?
SpreadsheetApp. This class allows users to open Google Sheets files and to create new ones.
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 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.
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() );
They don't seem to return a pointer to the same animal. I don't know why (don't have time to investigate) but
getActiveSheet()
has the getRangeByName()
method - and the one from getActive()
doesn't have itgetActiveSheet()
doesn't have the getCharts()
method - and the one from getActive()
does have itEither something really over my head makes this justifiable or it is just dismal language/syntax design (maybe some legacy crap that never got resolved?)
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() );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With