Thursday, November 14, 2013

Emailing Someone a Fancy PDF When They Submit a Google Form

7:26 AM

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.

Written by

We are one of the initiators of the development of information technology in understanding the need for a solution that is familiar and close to us.

0 comments:

Post a Comment

 

© 2013 Klick Dev. All rights resevered.

Back To Top