use*_*294 4 google-sheets google-apps-script
我使用Google表单教程调整表单数据以合并为PDF,然后发送到电子邮件.我尝试运行脚本时收到以下错误消息:
TypeError:无法从undefined中读取属性"values".(第11行,文件"代码")
我不知道如何解决这个问题.我在网上搜索了一个答案.这是脚本的副本.我标记了脚本出错的2行:
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);
}
Run Code Online (Sandbox Code Playgroud)
这是我正在测试的表单和证书的链接.
您看到的错误是因为您在脚本编辑器中运行了触发器功能.执行此操作时,e
未定义事件参数- 这是错误消息所说的内容.
有关更多背景信息,请参阅如何在GAS中测试触发器功能?
这是一个测试功能,它将onFormSubmit()
使用电子表格中已有的数据多次运行您的功能.它读取工作表的每一行,生成一个对象来模拟提交表单时将获得的事件,然后调用触发器函数.如果您将断点放在内部onFormSubmit()
或依赖Logger.log()
,此技术将允许您测试触发器功能.
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);
}
}
Run Code Online (Sandbox Code Playgroud)
我没有对你的原始函数进行其他调试......但这消除了该错误消息,因此您可以继续测试.
归档时间: |
|
查看次数: |
8467 次 |
最近记录: |