问题
In Log I can see app has been defined as SpreadsheetApp, but the getActiveSpreadsheet() is returning null. which in turn causes the getActiveSheet() to say it cannot call method with null.
I've already tried clearing my browser history, opening a new blank spreadsheet but get same error.
function myFunction() {
var app = SpreadsheetApp;
Logger.log(app);
var ss = app.getActiveSpreadsheet();
Logger.log(ss);
var activeSheet = ss.getActiveSheet();
TypeError: Cannot call method "getActiveSheet" of null. at myFunction(Code:6)
回答1:
Problem:
You're trying to run standalone scripts as if they're bound to a spreadsheet. (using getActive...)
From the Class Spreadsheet Documentation:
Functions that are run in the context of a spreadsheet can get a reference to the corresponding Spreadsheet object by calling this function.
Options:
- Bind your script to a spreadsheet by opening a sheet and going to
Tools > Script Editor, then running it from there. - Instead of using
getActiveSpreadsheet(), useSpreadsheetApp.openById('id')orSpreadsheetApp.openByUrl('url')instead. These will open the spreadsheet by its unique ID or URL and can be used from standalone scripts.
References:
- Class SpreadsheetApp
- getActiveSpreadsheet
- openById()
- openByUrl()
来源:https://stackoverflow.com/questions/56741432/typeerror-cannot-call-method-getactivesheet-of-null-at-myfunctioncode6