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

Short Script for Converting Google Forms Data to PDF not sending email

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

我对编码很满意,但我从未使用过 Google 驱动器脚本。似乎某些代码可能已过时。我通过查找和替换将 "DocsList" 更改为 "DriveApp",并将 "MailApp" 更改为 gmail 应用程序。

我不确定从这里到哪里去,甚至不知道在等待 formsubmit 事件时如何使用调试器。任何建议将不胜感激。谢谢

这是我的代码:

//:שְׁמַע יִשְׂרָאֵל יהוה אֱלֹהֵינוּ יהוה אֶחָד
//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

我发现如果您将电子邮件发送给自己,它们可能不会出现在您的收件箱中,因此请查看 "Sent Mail" 看看它们是否存在。

这行代码:

var copyBody = copyDoc.getActiveSection();

使用名为 getActiveSection() 的方法。该方法不在文档中,也没有出现在自动完成列表中。它不存在。 St运行足够好,它不会产生错误。我在任何弃用列表中都找不到它。

我只是 运行 你的代码,它给我发了一封带有 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