TypeError: Cannot call method “getActiveSheet” of null. at myFunction(Code:6)

旧时模样 提交于 2020-07-23 08:28:17

问题


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:

  1. Bind your script to a spreadsheet by opening a sheet and going to Tools > Script Editor, then running it from there.
  2. Instead of using getActiveSpreadsheet(), use SpreadsheetApp.openById('id') or SpreadsheetApp.openByUrl('url') instead. These will open the spreadsheet by its unique ID or URL and can be used from standalone scripts.

References:

  1. Class SpreadsheetApp
  2. getActiveSpreadsheet
  3. openById()
  4. openByUrl()


来源:https://stackoverflow.com/questions/56741432/typeerror-cannot-call-method-getactivesheet-of-null-at-myfunctioncode6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!