收到错误消息TypeError:无法调用方法"getSheetByName"在先前的工作系统上为null [英] Receiving error message TypeError: Cannot call method "getSheetByName" of null on previously working systems

查看:67
本文介绍了收到错误消息TypeError:无法调用方法"getSheetByName"在先前的工作系统上为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些不同的系统,它们使用以下代码访问基础电子表格.

I have a few different systems that use the following code to access the underlying spreadsheets.

 var mainLinks = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Main");

他们已经工作了6个月,但是现在我收到以下错误,并提到了上一行.

They have worked for 6 months, but I am now receiving the below error with a reference to the above line.

 TypeError: Cannot call method "getSheetByName" of null.

此方法是否已更改?对于如何解决这个问题,有任何的建议吗?谢谢!

Has this method been changed? Any suggestions on how to fix this? Thanks!!

推荐答案

没有任何更改.您没有当前有效的电子表格.根据 SpreadsheetApp的文档所述.getActiveSpreadsheet

Nothing has changed. You don't have a currently active spreadsheet. According to the documentation for SpreadsheetApp.getActiveSpreadsheet

返回当前活动的电子表格,如果没有,则返回null.

Returns the currently active spreadsheet, or null if there is none.

因此, SpreadsheetApp.getActiveSpreadsheet()返回 null ,因为您当前没有活动的电子表格,然后尝试在null上调用方法并获得预期的错误...

So, SpreadsheetApp.getActiveSpreadsheet() returns null, because you have no currently active spreadsheet, then you try to call a method on null and get the expected error...

无法调用null的方法"getSheetByName".

Cannot call method "getSheetByName" of null.

一个避免错误的简单修复方法是检查像这样的 null

A simple fix to avoid the error would be to check for null like this

 var mainLinks;
 var activeSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
 if (activeSpreadsheet !== null) {
     mainLinks = activeSpreadsheet.getSheetByName("Main");
 } else {
     // handle case where there is no active spreadsheet
 }

有很多原因可能导致您没有有效的电子表格,如果没有更多信息我们也无法告诉您,但是您可以尝试...

There are many reasons that you might not have an active spreadsheet and we can't tell without more information, but you could try...

sheet = SpreadsheetApp.openById(**your spreadsheet key**).getActiveSheet()

要找到电子表格密钥,请打开电子表格并查看浏览器地址栏:密钥是 key = 和第一个非字母数字字符之间的位,通常是&#,如 #gid & = pli = 1 .

To find the spreadsheet key, open the spreadsheet and look at the browser address bar: the key is the bit between key= and the first non-alphanumeric character, usually a & or # as in #gid or &pli=1.

这篇关于收到错误消息TypeError:无法调用方法"getSheetByName"在先前的工作系统上为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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