I am trying to use app script with a google forms sheet to populate a docs template which I then send as pdf attachment via Email. I have used the initial template from here: http://www.tjhouston.com/2012/03/merge-info-from-google-forms-to-pdf-document-and-send-via-email/
and made all suggested changes from here: Generate and send pdf through Google Forms to my email address - doesn't send, debugging to no assitance
However, I cannot get it to work. I would highly appreciate any input as i am at my wits end. Weirdly, the logger does not log anything.
var docTemplate = "documentIDhere"; // *** change template ID if new google doc is used***
var docName = "Recruiting Requirement Profile -";
function onFormSubmit(e) {
//Get information from form and set as variables
Logger.log(e)
var Q1 = e.values[2];
var Q2 = e.values[3];
var Q3 = e.values[4];
// Get document template, copy it as a new temp doc, and save the Doc’s id
var copyId = DriveApp.getFileById(docTemplate).makeCopy(docName).getId();
var copyDoc = DocumentApp.openById(copyId);
var copyBody = copyDoc.getBody();
copyBody.replaceText('keyQ1', Q1);
copyBody.replaceText('keyQ2', Q2);
copyBody.replaceText('keyQ3', Q3);
copyDoc.saveAndClose();
var pdf = DriveApp.getFileById(copyId).getAs("application/pdf");
var subject = DocName+Q3+"-"+Q1;
var body = "Here is your "+docName+Q3+"-"+Q1;
MailApp.sendEmail("[email protected]", subject, body, {htmlBody: body, attachments: pdf});
// Delete temp file
DriveApp.getFileById(copyId).setTrashed(true);
}
So the correct code can be found below. Important is to run the function as a trigger. To do this follow these steps:
Here is the code: