用于将Google表单数据转换为PDF而不发送电子邮件的简短脚本

时间:2015-11-02 20:32:38

标签: google-apps-script google-form

我正在与this tutorial合作,尝试将从谷歌表单提交的数据转换为PDF。问题是,我没有收到任何电子邮件。电子表格更新正常,但没有电子邮件。

我对编码很满意,但我从未使用Google Drive脚本。似乎有些代码可能已经过时了。我改变了#34; DocsList"到" DriveApp"通过查找和替换,我改变了#34; MailApp"到gmail app。

我不知道从这里去哪里,甚至在等待formubmit事件时如何使用调试器。任何建议都会大大贬低。感谢。

这是我的代码:

//:שְׁמַע יִשְׂרָאֵל יהוה אֱלֹהֵינוּ יהוה אֶחָד
//Christian Shields (ChristianShields@gmail.com)
//Job Application Google Form to PDF converter
//11/2/15

//get the document from Google Docs and name it
var docTemplate = "1wgkvO-1xrVGJkD-JJ2FMRbQj_bfGu7qFAa6pTEJ61Cw";
var docName = "NewCherryHillJobApplication";

function onFormSubmit(e){
  //set the email address where the finished document will go.
  var docs_email = "ChristianShields@gmail.com";

  //get information from the newly submited form
  var full_name = e.values[2];
  var age = e.values[3];
  var address = e.values[4];
  var phone = e.values[5];
  var alt_phone = e.values[6];
  var email

  //Get the document template and copy it as a new temp document. Save the document ID's
  var copyId = DriveApp.getFileById(docTemplate)
  .makeCopy(docName+' for '+full_name)
  .getId();

  //open the temp document
  var copyDoc = DocumentApp.openById(copyId);
  //go to the temp documents body
  var copyBody = copyDoc.getActiveSection();

  //replace the text in the temp document
  copyBody.replaceText('keyFullName', full_name);
  copyBody.replaceText('keyAge', age);
  copyBody.replaceText('keyAddress', address);
  copyBody.replaceText('keyPhone', phone);
  copyBody.replaceText('keyAltPhone', alt_phone);
  copyBody.replaceText('keyEmail', email);

  //save and close the temp document
  copyDoc.saveAndClose();

  //convert the temp document to pdf
  var pdf = DriveApp.getFileById(copyId).getAs("application/pdf");

  //Attach PDF and send the email
  var subject = "New Doc's Job Application Submited Online";
  var body = "A new job application has been recieved from " + full_name + ". Primary phone number for this applicant is: " + phone + "";
  GmailApp.sendEmail(docs_email, subject, body, {htmlBody: body, attachments: pdf});

  // Delete temp file
  DriveApp.getFileById(copyId).setTrashed(true); 
} //End of dunction "onFormSubmit

2 个答案:

答案 0 :(得分:1)

我发现如果您向自己发送电子邮件,可能不会在收件箱中显示,请查看“已发邮件”以查看它们是否在那里。

答案 1 :(得分:1)

这行代码:

var copyBody = copyDoc.getActiveSection();

使用名为getActiveSection()的方法。该方法不在文档中,并且不会出现在自动完成列表中。它不存在。奇怪的是,它不会产生错误。我无法在任何已弃用的列表中找到它。

我刚刚运行了您的代码,它向我发送了一封包含pdf文件的电子邮件。我评论了一些部分。

function onFormSubmit(e){
  //set the email address where the finished document will go.
  var docs_email = "your_email@gmail.com";

  //get information from the newly submited form
  var full_name = 'First Last';
  var age = '99';
  var address = '77 Maple Grove';
  var phone = '800 Call Joe';
  var alt_phone = 'none';
  var email

  //Get the document template and copy it as a new temp document. Save the document ID's
  var copyId = DriveApp.getFileById('Your Document ID Here')
  .makeCopy('Novem3'+' for '+full_name)
  .getId();

  //open the temp document
  var copyDoc = DocumentApp.openById(copyId);
  //go to the temp documents body
  copyDoc.getBody()
  var copyBody = copyDoc.getActiveSection();

  //replace the text in the temp document
/*  copyBody.replaceText('keyFullName', full_name);
  copyBody.replaceText('keyAge', age);
  copyBody.replaceText('keyAddress', address);
  copyBody.replaceText('keyPhone', phone);
  copyBody.replaceText('keyAltPhone', alt_phone);
  copyBody.replaceText('keyEmail', email);
*/
  //save and close the temp document
  copyDoc.saveAndClose();

  //convert the temp document to pdf
  var pdf = DriveApp.getFileById(copyId).getAs("application/pdf");

  //Attach PDF and send the email
  var subject = "New Doc's Job Application Submited Online";
  var body = "A new job application has been recieved from " + full_name + ". Primary phone number for this applicant is: " + phone + "";
  GmailApp.sendEmail(docs_email, subject, body, {htmlBody: body, attachments: pdf});

  // Delete temp file
  //DriveApp.getFileById(copyId).setTrashed(true); 
} //End of dunction "onFormSubmit