在onFormSubmit触发器中获取TypeError? [英] Getting TypeError in onFormSubmit trigger?
问题描述
我使用Google表单教程来调整表单数据以合并为PDF,然后发送至电子邮件。当我尝试运行脚本时,出现以下错误消息:
TypeError:无法读取属性值来自未定义。 (第11行,文件代码)
我不知道如何解决问题。我在网上搜索了一个答案。这是一个脚本的副本。我标记了脚本发出错误的两行:
var docTemplate =1ZSqmId2BBjtz6PmgQEmusjnkHGsFKD1CBSq0rrQk6Kc;
var docName =TestCertificate;
//当Form Gets提交了
函数onFormSubmit(e){
//从窗体获取信息并设置变量
var email_address =EMAIL@example.com;
// **(这是下面这两行的错误!)**
var full_name = e.values [2];
var Activity = e.values [3];
//获取文档模板,将其复制为新的临时文档,并保存文档ID
$ b var copyId = DocsList.getFileById(docTemplate)
.makeCopy (docName +'for'+ full_name)
.getId();
//打开临时文件
var copyDoc = DocumentApp.openById(copyId);
//获取文档的正文部分
var copyBody = copyDoc.getActiveSection();
//替换占位符键,在我们的Google文档模板中
copyBody.replaceText('keyFullName',full_name);
copyBody.replaceText('keyActivity',Activity);
//保存并关闭临时文档
copyDoc.saveAndClose();
//将文档转换为PDF
var pdf = DocsList.getFileById(copyId).getAs(application / pdf);
//附上PDF并发送电子邮件
var subject =Report;
var body =这是+ full_name +的格式。
MailApp.sendEmail(email_address,subject,body,{htmlBody:body,attachments:pdf});
//删除临时文件
DocsList.getFileById(copyId).setTrashed(true);
}
以下是我正在测试的表单和证书的链接。
您看到的错误是因为您在脚本编辑器中运行了触发器功能。当你这样做的时候,事件参数 e
没有被定义 - 这就是错误信息的意思。
更多背景信息,请参阅如何测试GAS中的触发器功能?
下面是一个测试函数,它会多次运行您的 onFormSubmit()
函数,这已经在您的电子表格中。它读取工作表的每一行,生成一个对象来模拟提交表单时获得的事件,然后调用触发器函数。如果你在 onFormSubmit()
中放置断点,或者依靠 Logger.log()
,这个技巧将允许你测试你的触发器函数。
$ b $ pre $ function test_onFormSubmit(){
var dataRange = SpreadsheetApp.getActiveSheet()。getDataRange()
var data = dataRange.getValues();
var headers = data [0];
//从第1行开始,在第0行跳过标题
for(var row = 1; row< data.length; row ++){
var e = {};
e.values = data [row];
e.range = dataRange.offset(row,0,1,data [0] .length);
e.namedValues = {};
//循环标题创建namedValues对象
用于(var col = 0; col< headers.length; col ++){
e.namedValues [headers [col]] = e.values [COL];
}
//将模拟的事件传递给onFormSubmit
onFormSubmit(e);
}
}
我没有对原始函数进行其他调试...但是这会摆脱那个错误信息,所以你可以继续测试。
I used a Google Forms tutorial to tweak Form data to merge into a PDF and then send to an email. I am getting the following error message when I try to run the script:
TypeError: Cannot read property "values" from undefined. (line 11, file "Code")
I do not know how to fix the problem. I have searched the web for an answer. Here is a copy of the script. I marked the 2 lines where the script is giving an error:
var docTemplate = "1ZSqmId2BBjtz6PmgQEmusjnkHGsFKD1CBSq0rrQk6Kc";
var docName = "TestCertificate";
// When Form Gets submitted
function onFormSubmit(e) {
//Get information from form and set our variables
var email_address = "EMAIL@example.com";
//**(THIS IS WHERE THE ERROR IS OCCURRING ON THESE 2 LINES BELOW!)**
var full_name = e.values[2];
var Activity = e.values[3];
// Get document template, copy it as a new temp doc, and save the Doc’s id
var copyId = DocsList.getFileById(docTemplate)
.makeCopy(docName+' for '+full_name)
.getId();
// Open the temporary document
var copyDoc = DocumentApp.openById(copyId);
// Get the document’s body section
var copyBody = copyDoc.getActiveSection();
// Replace place holder keys,in our google doc template
copyBody.replaceText('keyFullName', full_name);
copyBody.replaceText('keyActivity', Activity);
// Save and close the temporary document
copyDoc.saveAndClose();
// Convert document to PDF
var pdf = DocsList.getFileById(copyId).getAs("application/pdf");
// Attach PDF and send the email
var subject = "Report";
var body = "Here is the form for " + full_name + "";
MailApp.sendEmail(email_address, subject, body, {htmlBody: body, attachments: pdf});
// Delete temp file
DocsList.getFileById(copyId).setTrashed(true);
}
Here are links to the form and certificate I was testing.
The error you're seeing is because you're running a trigger function in the Script Editor. When you do this, the Event Parameter e
is not defined - that's what the error message is saying.
For more background, see How can I test a trigger function in GAS?
Here's a test function that will run your onFormSubmit()
function multiple times, with the data that's already in your spreadsheet. It reads each row of the sheet, generates an object to simulate the Event you would get when a form was submitted, then calls the trigger function. If you place breakpoints inside onFormSubmit()
, or rely on Logger.log()
, this technique will allow you to test your trigger function.
function test_onFormSubmit() {
var dataRange = SpreadsheetApp.getActiveSheet().getDataRange()
var data = dataRange.getValues();
var headers = data[0];
// Start at row 1, skipping headers in row 0
for (var row=1; row < data.length; row++) {
var e = {};
e.values = data[row];
e.range = dataRange.offset(row,0,1,data[0].length);
e.namedValues = {};
// Loop through headers to create namedValues object
for (var col=0; col<headers.length; col++) {
e.namedValues[headers[col]] = e.values[col];
}
// Pass the simulated event to onFormSubmit
onFormSubmit(e);
}
}
I've done no other debugging of your original function... but this gets rid of that error message, so you can continue testing.
这篇关于在onFormSubmit触发器中获取TypeError?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!