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.
0 comments:
Post a Comment