TypeError:无法调用方法"getActiveSheet";的null.在myFunction(代码:6) [英] TypeError: Cannot call method "getActiveSheet" of null. at myFunction(Code:6)

查看:84
本文介绍了TypeError:无法调用方法"getActiveSheet";的null.在myFunction(代码: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.


选项:

  1. 通过打开工作表并转到Tools > Script Editor,然后从那里运行脚本,将脚本绑定到电子表格.
  2. 代替使用getActiveSpreadsheet(),而使用SpreadsheetApp.openById('id')SpreadsheetApp.openByUrl('url').这些将通过电子表格的唯一ID或URL打开电子表格,并且可以在独立脚本中使用.
  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.


参考文献:

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

这篇关于TypeError:无法调用方法"getActiveSheet";的null.在myFunction(代码:6)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆