Skip to main content

Emailing Someone a Fancy PDF When They Submit a Google Form

Google Forms are a great way to gather and process Application Forms, but often the data entered is very text heavy and apart from a Google Spreadsheet being a hopeless way to read text, the submitted might need a "better formatted" version of their submission.

Here's a code snippet that builds on the ideas in a previous post,
Using Google Docs To Make Google Spreadsheets Easier to Read, where we use Apps Script to make it easy to add {tags} to a document that correspond to the names of headers in a spreadsheet and then use that document as a template.

This code, gathers the student's name and email and shows how to create a rendered Google Document and then email the PDF of their data back to them when they submit a Google Form.


var folder_id = 'YOUR_FOLDER_ID_HERE'
var institution_folder = DocsList.getFolderById( folder_id )
    var personal_folder = institution_folder.createFolder(student_name) 
    
    var template_id = 'YOUR_TEMPLATE_DOC_ID' // The Application Form
    var template_doc = DocsList.getFileById(template_id)
    var new_doc_id = template_doc.makeCopy("Application: " + new_doc_title).getId()
    var doc = DocsList.getFileById(new_doc_id)  //Move to destination folder
    doc.addToFolder(personal_folder)
    
    //Open the document for content editing
    var new_doc = DocumentApp.openById( new_doc_id )
    //Render the values into the doc
    var s = ''
    for ( var key in values) {  
      var value = values[key][0]
      var tag = "{" + key + "}"
      s+=  tag + " " + values[key][0] + "\r"
      new_doc.replaceText(tag, value ) 
    }
    //Replace any unreplaced tags for tidiness
    new_doc.replaceText("\{.*?\}", "" ) 
    new_doc.saveAndClose() // Force changes
   
    var new_doc = DocumentApp.openById(new_doc_id)
    var pdf_blob = doc.getAs(MimeType.PDF)
    personal_folder.createFile(pdf_blob)
    
    //Send email to student.
    var text = new_doc_title + "\r\r\r"
    text = text +  'Your application has been received, thank you. \r'
    text = text + "\r\r\r"
    
    MailApp.sendEmail(student_email, 'Application Received: ' + new_doc_title , text, {name: 'Automatic Script',
                      noReply:true,
                      attachments: [pdf_blob],
                      }) 
    doc.setTrashed(true)//Optional...you might choose to keep it.

The above code would be added somewhere in your onFormSubmit() function.

Comments

Popular posts from this blog

Inserting A Google Doc link into a Google Spreadsheet (UPDATED 6/12/2017)

This article looks at using Apps Script to add new features to a Google Spreadsheet. At the University of York, various people have been using Google spreadsheets to collect together various project related information. We've found that when collecting lots of different collaborative information from lots of different people that a spreadsheet can work much better than a regular Google Form. Spreadsheets can be better than Forms for data collection because: The spreadsheet data saves as you are editing. If you want to fill in half the data and come back later, your data will still be there. The data in a spreadsheet is versioned, so you can see who added what and when and undo it if necessary The commenting features are brilliant - especially the "Resolve" button in comments. One feature we needed was to be able to "attach" Google Docs to certain cells in a spreadsheet. It's easy to just paste in a URL into a spreadsheet cell, but they can often

A Working Booking System In Google Sheets

Working with Andras Sztrokay we had another go at a booking system. This time it was to enable staff to book out a number of iPads over a number of days. You select the days you want, then select the Booking menu. Andras did an amazing job. It even creates a daily bookings sheet so you can see who has which iPads. To see this in action, go  here  and  File > Make a Copy (I won't be able to support you this is just provided to maybe give someone else a leg up, good luck!)

One-To-Many Relationship in a Google Spreadsheet

It's often the case that you want and need to be creating a database to store your data, but Google Spreadsheets are just so handy aren't they? But Google Spreadsheets are very good at relational data. Here's an example where, you want to have one column for the name of your recipe and another for the ingredients ( comma separated ). How you use this script is you click on the cell you want to be relational and choose the Admin > Show Relationship Editor. This opens up a dialog window showing you all the options included so far. You then alter the ingredients and it saves a comma separated list into the spreadsheet. Here's the spreadsheet . Use File > Make a copy to see it work and rummage around in the code. If anyone can help make the UI prettier I'd be grateful, thanks.