如何解决' TypeError:无法调用方法& quot; getRange& quot;为null'在Google Apps脚本中 [英] How to fix 'TypeError: Cannot call method "getRange" of null' in Google Apps Script

查看:40
本文介绍了如何解决' TypeError:无法调用方法& quot; getRange& quot;为null'在Google Apps脚本中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设置一个脚本,以使用Google表格中的数据自动填充Google文档中的模板文档.我一直遇到"TypeError:无法调用null的方法"getRange""错误.如何解决此错误?

I'm setting up a script to auto-populate a template document in Google Docs using data from Google Sheets. I keep encountering the 'TypeError: Cannot call method "getRange" of null' error. How do I resolve this error?

根据我的阅读,此错误通常是由于未正确定义变量(在这种情况下,是工作表的名称)引起的.但是,我确定名称与工作表中的文件名相同.我试图在两个地方都更改名称,甚至制作一个全新的文档.

From what I've read, this error usually comes from not having the variable defined properly (in this case, the name of the sheet). However, I'm sure the name is the same as the file name in sheets. I've tried changing the name in both places and even making an entirely new document.

4.  var ss = SpreadsheetApp.openById("13YTb-lQzjijkfOKGsZRQf8fcpt_7sMy5_DX7bvlz5Yo");
5.
6.  var sheet = ss.getSheetByName("SAT");
7.
8.  var documentId = DriveApp.getFileById('1xrc7K9WfNiB_33y0xsB33209IZPBcpskThanC_yfzCw').makeCopy().getId();
9.  
10.  DriveApp.getFileById(documentId).setName('SAT Vocab ' + date);  
11.  
12.  var body = DocumentApp.openById(documentId).getBody();
13.
14.  var range = sheet.getRange("A2:A16");

这是我收到的错误消息:

This is the error message I receive:

TypeError:无法调用null的方法"getRange".(第14行,文件代码")

TypeError: Cannot call method "getRange" of null. (line 14, file "Code")

这是Logger.log(ss +''+ sheet);中的日志:[19-06-20 12:30:52:007 CDT]未定义未定义

This is the log from Logger.log(ss + ' ' + sheet);: [19-06-20 12:30:52:007 CDT] undefined undefined

推荐答案

问题:

sheet.getRange("A2:A16")

TypeError:无法调用方法"getRange"的null.(第14行,文件代码")

TypeError: Cannot call method "getRange" of null. (line 14, file "Code")

该错误表示 sheet null ,并且 null 没有 getRange 方法.只有真正的 Sheet 类.

The error means sheet is null and null doesn't have a getRange method. Only a real Sheet class does.

根据文档所述,唯一的一个原因,返回的 sheet null .

如果没有具有给定名称的图纸,则返回null.

Returns null if there is no sheet with the given name.

只有一个答案.就是那个工作表名称不存在的时候.

There is only one answer. It's when that sheet name doesn't exist.

工作表名称是指电子表格/工作簿中的选项卡名称.它不引用文件名或文档名称.默认情况下,第一个工作表/标签被命名为"Sheet1".试试

sheet name refers to the tab name in a spreadsheet/workbook. It does not refer to the filename or document name. By default, the first sheet/tab is named "Sheet1". Try

ss.getSheetByName("Sheet1");

如果仍然无法获取工作表,请检查工作表名称中是否有空格和不可打印的字符.或者,将工作表重命名为简单的名称,例如 S1 .

If you still have trouble getting the sheet, check for spaces, non printable characters in the sheet name. Alternatively, Rename the sheet to something simple like S1.

这篇关于如何解决' TypeError:无法调用方法& quot; getRange& quot;为null'在Google Apps脚本中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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