How can I map a Google form submission to a template and email to specific individuals as a PDF?

73 views Asked by At

I am trying to have users submit information through Google forms and have the information inserted into a Google Doc as a template. The completed document would then be converted to a PDF and emailed out to the individual. The issue that I am having is that nothing seems to be produced upon form submission. Help would be appreciated!

Here is the code that I am using:

//Get template from Google Docs and name it
var docTemplate = "1ebZTRMRJTxEkNQl1Y2XTSCmIMhmThSMk3BDEQrJbOBE"; 
var docName = "Unit Overview Template";

//When Form Gets Submitted
function onFormSubmit(e){
  //Get information from form and set as variables
  var email_address = "e.values[5]";
  var teacher_name = e.values[2];
  var unit_name = e.values[6];
  var unit_length = e.values[7];
  var unit_start = e.values[3];
  var course_period = e.values[4];
  
  //Get document template, copy it as a new temp doc, and save the Doc's id
  var copyId = DocsList.getFilebyId(docTemplate)
  .makeCopy(docName+' for ' +teacher_name)
  .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('keyFullName', teacher_name);
  copyBody.replaceText('keyUnitName', unit_name);
  copyBody.replaceText('keyUnitDays', unit_length);
  copyBody.replaceText('keyUnitPeriod', course_period);
  copyBody.replaceText('keyUnitStart', unit_start);
  
  //Save and close the temporary document
  copyDoc.saveAndClose();
  
  //Convert temporary document to PDF
  var pdf = DocsList.getFilebyId(copyID).getAs("application/pdf");
  
  //Attach PDF and send the email
  var subject = "Unit Lesson Overview";
  var body = "Here is your completed Unit Overview for " + unit_name + "";
  MailApp.sendEmail(email_address, subject, body, {htmlBody: body, attachments: pdf});
                    
  //Delete temp file
    DocsList.getFilebyId(copyId).setTrashed(true);
}

1

There are 1 answers

0
Amit Agarwal On

Use DriveApp instead of DocsList as the latter is deprecated. Also make sure you have a trigger setup for onFormSubmit under Resources - current project triggers inside the Script Editor.