TypeError:无法调用方法"getActiveSheet";的null.在myFunction(代码:6) [英] TypeError: Cannot call method "getActiveSheet" of null. at myFunction(Code:6)
问题描述
在日志中,我可以看到应用已定义为SpreadsheetApp,但是getActiveSpreadsheet()返回null.依次导致getActiveSheet()声明它不能使用null调用方法.
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:无法调用null的方法"getActiveSheet".在myFunction(代码:6)
TypeError: Cannot call method "getActiveSheet" of null. at myFunction(Code:6)
推荐答案
问题:
您正在尝试运行独立脚本,就像它们已绑定到电子表格一样. (使用getActive...
)
来自类电子表格文档":
From the Class Spreadsheet Documentation:
在电子表格的上下文中运行的函数可以通过调用此函数来获取对相应Spreadsheet对象的引用.
Functions that are run in the context of a spreadsheet can get a reference to the corresponding Spreadsheet object by calling this function.
选项:
- 通过打开工作表并转到
Tools > Script Editor
,然后从那里运行脚本,将脚本绑定到电子表格. - 代替使用
getActiveSpreadsheet()
,而使用SpreadsheetApp.openById('id')
或SpreadsheetApp.openByUrl('url')
.这些将通过电子表格的唯一ID或URL打开电子表格,并且可以在独立脚本中使用.
- 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.
参考文献:
这篇关于TypeError:无法调用方法"getActiveSheet";的null.在myFunction(代码:6)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!