我想在工作现场填写一份Google表单,当我提交时,我需要一个包含提交信息的PDF通过电子邮件发送到我的办公室。我无法获得e.values(表单)或e.responses(表单)工作
下面是我在哪里至今:
// Get template from Google Docs and name it
var docTemplate = "12vZhIPt1CkWPCCN3jRE1Kpbp0dBW3key-4I_Stz0vNc";
var docName = "JobsiteInspectionReport";
// When Form Gets submitted
function onFormSubmit(e) {
//Get information from form and set as variables
var variablename = "static entry or form value"
var email_address = "[email protected], [email protected]";
// Use this section to assign static values
var jobsite = "test data";
var date_time = "test data2";
var submit_name = "test data3";
var weather = "test data4";
var temp = "test data5";
var super_name = "test data6";
var job_num = "test data7";
// Use this section when attached to form
var jobsite = e.responses[2];
var date_time = e.responses[3];
var submit_name = e.responses[4];
var weather = e.responses[5];
var temp = e.responses[6];
var super_name = e.responses[7];
var job_num = e.responses[8];
// Use this section when attached to a sheet
var jobsite = e.values[2];
var date_time = e.values[3];
var submit_name = e.values[4];
var weather = e.values[5];
var temp = e.values[6];
var super_name = e.values[7];
var job_num = e.values[8];
//General Items
// Get document template, copy it as a new temp doc, and save the Doc’s id
var copyId = DriveApp.getFileById(docTemplate)
.makeCopy(docName+' for '+jobsite)
.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('keySiteName', jobsite);
copyBody.replaceText('keyDateTime', date_time);
copyBody.replaceText('keySubmitName', submit_name);
copyBody.replaceText('keyWeather', weather);
copyBody.replaceText('keyTemp', temp);
copyBody.replaceText('keySuperName',super_name);
copyBody.replaceText('keyJobNum', job_num);
// Save and close the temporary document
copyDoc.saveAndClose();
// Convert temporary document to PDF
var pdf = DriveApp.getFileById(copyId).getAs("application/pdf");
// Attach PDF and send the email
var subject = "Jobsite Inspection Report";
var body = "Here is the Jobsite Inspection Report for " + jobsite + "";
MailApp.sendEmail(email_address, subject, body, {htmlBody: body, attachments: pdf});
// Delete temp file
DriveApp.getFileById(copyId).setTrashed(true);
}
我已经安装了onFormSubmit触发两个表和形式,并通过删除电子成功发送邮件带有附件在两种形式的脚本和床单。值(在工作表脚本中,当脚本安装在表单上时,我使用e.response)并将静态值分配给变量(请参阅标记为“附加到表单时使用此部分”的代码部分)。我收到一封电子邮件,正确的模板上带有正确的静态数据。
当我尝试使用e.values或e.responses部分时,什么都没有。我甚至没有收到电子邮件或错误。
几小时后,我确实收到一封电子邮件(其中有几个),声明“TypeError:无法从未定义的属性读取2”(第25行,代码文件)“我在考虑e.values [ 2]是我的问题(或其格式化或更具体定义的方式)。脚本似乎在寻找表单?
您是否查看执行脚本以查看整个脚本是否完成?每当你收到意想不到的结果时,在VIEW菜单下选择Execution Transcript。如果出现错误,它会告诉你错误发生在哪一行。阅读[故障排除](https://developers.google.com/apps-script/troubleshooting)页面。 –
谢谢,我试过了脚本,当我尝试通过提交表单来运行脚本时(onFormSubmit触发器附加到响应电子表格中)脚本是空的,并提示我“运行一些脚本”。当我在编辑器页面中运行脚本时,我得到未定义的错误,因为表单值(e.values)尚未创建。 (我的理解方式) –
几个小时后,我确实收到了一封电子邮件(其中有几封),声明“TypeError:无法从未定义的属性读取2”(第25行,“代码”文件)“我在想e.values [2]是我的问题(或其格式化或更具体定义的方式)。脚本似乎在寻找表单? –