Google apps script Google表单脚本中的TypeError
在Google表单脚本编辑器中,我使用下面的代码在电子表格中插入表单数据的编辑响应链接Google apps script Google表单脚本中的TypeError,google-apps-script,google-sheets,google-forms,Google Apps Script,Google Sheets,Google Forms,在Google表单脚本编辑器中,我使用下面的代码在电子表格中插入表单数据的编辑响应链接 函数assignEditUrls(){ var form=FormApp.openById('1bAXKuBKQny9CLU3WOK4xxxxxxxxxxxxxxxxxxxxxxxxxxxx'); //在此处输入表单ID var sheet=SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Form Responses'); //根据需要更改图纸名称
函数assignEditUrls(){
var form=FormApp.openById('1bAXKuBKQny9CLU3WOK4xxxxxxxxxxxxxxxxxxxxxxxxxxxx');
//在此处输入表单ID
var sheet=SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Form Responses');
//根据需要更改图纸名称
var data=sheet.getDataRange().getValues();
var urlCol=7;//应该填充URL的列号;A=1,B=2等
var responses=form.getResponses();
var时间戳=[]、URL=[]、结果URL=[];
对于(var i=0;i
但当我运行这个脚本时,它会显示如下错误
TypeError: Cannot call method "getSheetByName" of null. (line 5, file "Code")Dismiss
如何解决此问题?此脚本按ID打开表单,并希望响应表位于脚本包含的电子表格中。所以你有两个选择:
- 将代码添加到收集响应的Google电子表格的脚本编辑器中,而不是表单编辑器中
- 或者
或者更好,正如Serge所说:
var sheet = SpreadsheetApp.openByID(form.getDestinationId()) .getSheetByName('Form Responses');
说明:错误消息表示您尝试使用方法
getSheetByName()
的对象是null
对象。该对象是从SpreadsheetApp.getActiveSpreadsheet()
获得的。该方法仅适用于电子表格绑定脚本。可能不是您要寻找的答案,但请看一看:作为此(好)答案的补充,您可以使用Form.getDestinationId方法获取目标电子表格的ID,请参见此处的文档:@Sergeinsas,哦,很好!更新。
var sheet = SpreadsheetApp.openByID(form.getDestinationId())
.getSheetByName('Form Responses');