2015-05-25 94 views
4

我想在工作现场填写一份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]是我的问题(或其格式化或更具体定义的方式)。脚本似乎在寻找表单?

+0

您是否查看执行脚本以查看整个脚本是否完成?每当你收到意想不到的结果时,在VIEW菜单下选择Execution Transcript。如果出现错误,它会告诉你错误发生在哪一行。阅读[故障排除](https://developers.google.com/apps-script/troubleshooting)页面。 –

+0

谢谢,我试过了脚本,当我尝试通过提交表单来运行脚本时(onFormSubmit触发器附加到响应电子表格中)脚本是空的,并提示我“运行一些脚本”。当我在编辑器页面中运行脚本时,我得到未定义的错误,因为表单值(e.values)尚未创建。 (我的理解方式) –

+0

几个小时后,我确实收到了一封电子邮件(其中有几封),声明“TypeError:无法从未定义的属性读取2”(第25行,“代码”文件)“我在想e.values [2]是我的问题(或其格式化或更具体定义的方式)。脚本似乎在寻找表单? –

回答

1

提交事件对象需要进一步工作,然后才能按照指示的方式访问它。如果您要访问的响应作为一个简单的数组,你需要从FormResponse对象和个体化ItemResponse变换值

function onFormSubmit(e) { 

    // you need to add this line to add offline event object access auth 
    // run once in script editor - you could delete it after if you want 
    var f = FormApp.getActiveForm(); 

    // test for response and use static if none exists 
    var responses = [ 
    "test data", 
    "test data2", 
    "test data3", 
    "test data4", 
    "test data5", 
    "test data6", 
    "test data7" 
    ]; 

    responses = (e && e.response) ? 
    e.response.getItemResponses() 
       .map(function (r) { 
       return r.getResponse(); 
       }) : (e && e.values) ? e.values : responses; 

    // snip // 

    var jobsite = responses[2]; 
    var date_time = responses[3]; 
    var submit_name = responses[4]; 
    var weather = responses[5]; 
    var temp = responses[6]; 
    var super_name = responses[7]; 
    var job_num = responses[8]; 

    // snip // 

} 
0

我终于放弃了,并开始了和它的作品!该脚本与上面列出的脚本相同(不包括我在故障排除时使用的所有评论部分)。我可以说的唯一不同之处就是表单本身的设置。对于任何人有这个相同的问题,我会检查表单上的设置。在工作的表单上,设置“允许响应者在提交之后编辑响应”的设置未被检查,因为我认为这是在我使用的第一个表单上进行检查的。如果不是这样,我仍然处于亏损状态。