Google Apps脚本:通过编辑对Google表单的回复来创建pdf

时间:2013-10-12 08:15:12

标签: google-apps-script google-form

我希望有人可以帮助我,我想做的是让Google表单创建一个新文档,并在编辑表单时通过电子邮件发送该文件。

据我所知,当您编辑表单时,电子表格中的脚本不会运行,所以我已将脚本放入表单中,这就是我遇到问题的地方。我无法让脚本读取表单上重新提交的值,它只是在返回时继续显示为“未定义”

这是我到目前为止的脚本。

function Test(e) {

  var form = FormApp.openById('****Form Key****'); //Enter Form ID here

var docTemplate = "***doc to be used***";  
   var docName      = "***Name of document***";

   var formResponse = form.getResponses();

  var one =  e.response;  //I have tried e.value but does not pull through
  var two =  e.response;  // Column 2

  var copyId = DocsList.getFileById(docTemplate)
                .makeCopy(docName)
                .getId();
// Open the temporary document
   var copyDoc = DocumentApp.openById(copyId);
// Get the document’s body section
   var copyBody = copyDoc.getActiveSection(); 

  copyBody.replaceText('one', one);
  copyBody.replaceText('two', two);

 // Save and close the temporary document
   copyDoc.saveAndClose();

// Convert temporary document to PDF by using the getAs blob conversion
   var pdf = DocsList.getFileById(copyId).getAs("application/pdf");  

//Sends the email
  var email_Address =  "***Email Address***"
     var subject = "***Subject";
  var body    = "**Body***"; 
  MailApp.sendEmail(email_Address,  subject, body, {attachments: pdf});  

}}

任何帮助将不胜感激,因为我已经坚持了一段时间。谢谢。

2 个答案:

答案 0 :(得分:1)

在表单响应触发器功能中,该事件是Class FormResponse的实例。因此,您无需打开表单或获得回复......您只需要一个人通过e.response访问您。 (请参阅Understanding Events中的表单响应事件。)

// Handle form response event
// This function must be a Form Response Trigger, attached to
// a Form (not a Spreadsheet).
function rightTest( e ) {
  var formResponses = e.response.getItemResponses(); // array of responses
  var one = formResponses[0];
  var two = formResponses[1];
  ...
}

答案 1 :(得分:1)

截至2015年4月,不支持DocsList,只需替换DocsList即可。使用DriveApp。 How to update DocsList to DriveApp in my code