【发布时间】:2019-06-12 02:45:33
【问题描述】:
我正在尝试使用带有谷歌表单的应用脚本来填充文档模板,然后我通过电子邮件将其作为 pdf 附件发送。我使用了这里的初始模板: http://www.tjhouston.com/2012/03/merge-info-from-google-forms-to-pdf-document-and-send-via-email/
并从此处进行所有建议的更改: Generate and send pdf through Google Forms to my email address - doesn't send, debugging to no assitance
但是,我无法让它工作。我非常感谢任何输入,因为我无能为力。奇怪的是,记录器没有记录任何东西。
var docTemplate = "documentIDhere"; // *** change template ID if new google doc is used***
var docName = "Recruiting Requirement Profile -";
function onFormSubmit(e) {
//Get information from form and set as variables
Logger.log(e)
var Q1 = e.values[2];
var Q2 = e.values[3];
var Q3 = e.values[4];
// Get document template, copy it as a new temp doc, and save the Doc’s id
var copyId = DriveApp.getFileById(docTemplate).makeCopy(docName).getId();
var copyDoc = DocumentApp.openById(copyId);
var copyBody = copyDoc.getBody();
copyBody.replaceText('keyQ1', Q1);
copyBody.replaceText('keyQ2', Q2);
copyBody.replaceText('keyQ3', Q3);
copyDoc.saveAndClose();
var pdf = DriveApp.getFileById(copyId).getAs("application/pdf");
var subject = DocName+Q3+"-"+Q1;
var body = "Here is your "+docName+Q3+"-"+Q1;
MailApp.sendEmail("name@name.com", subject, body, {htmlBody: body, attachments: pdf});
// Delete temp file
DriveApp.getFileById(copyId).setTrashed(true);
}
【问题讨论】:
-
在您的脚本中,有几个拼写错误。例如,它们是
MailApp和DriveApp。请再次确认。那么,为了正确理解您的情况,能否提供I cannot get it to work.的更多信息,修改拼写后,如果出现错误,请出示。顺便问一下,你如何运行你的脚本? -
谢谢Tainaike,我已经纠正了所有的拼写错误。我试图在测试文件上调试它,似乎只要我使用 e.values[] 它就停止工作。意思是,如果我将变量定义为字符串,例如“测试” pdf 文件到达,但是一旦我尝试通过 e.values[] 获取表单/表格值,我就不再收到电子邮件了。我通过在表单中输入数据来运行脚本,因为 e.values 不能通过调试工作。我用字符串作为 e.values[] 的占位符对其进行了调试,它起作用了。我还删除了附件周围的括号:pdf。
-
@Tanaike 日志告诉我
undefined。 -
感谢您的回复和更新。你如何运行你的脚本?从
the log just tells me undefined.,我想你可以直接在脚本编辑器中运行onFormSubmit()的功能。如果是这样,就会发生这样的错误。e的onFormSubmit(e)是 an event object。所以请将函数安装为a trigger。这样,当表单提交时,函数就会运行并给出对象e。 -
@Tanaike 非常感谢!!现在它可以工作了 - 必须将该功能设置为触发器。
标签: google-apps-script google-docs google-forms