Reputation: 41
I want to fill out a google form on a jobsite and when I submit, I need a pdf containing the submitted info to be emailed to my office.
Here's where I'm at so far:
// Get template from Google Docs and name it
var docTemplate = "12vZhIPt1CkWPCCN3jRE1Kpbp0dBW3key-4I_Stz0vNc";
var docName = "JobsiteInspectionReport";
// When Form Gets submitted
function onFormSubmit(e) {
//Get information from form and set as variables
var variablename = "static entry or form value"
var email_address = "[email protected], [email protected]";
// Use this section to assign static values
var jobsite = "test data";
var date_time = "test data2";
var submit_name = "test data3";
var weather = "test data4";
var temp = "test data5";
var super_name = "test data6";
var job_num = "test data7";
// Use this section when attached to form
var jobsite = e.responses[2];
var date_time = e.responses[3];
var submit_name = e.responses[4];
var weather = e.responses[5];
var temp = e.responses[6];
var super_name = e.responses[7];
var job_num = e.responses[8];
// Use this section when attached to a sheet
var jobsite = e.values[2];
var date_time = e.values[3];
var submit_name = e.values[4];
var weather = e.values[5];
var temp = e.values[6];
var super_name = e.values[7];
var job_num = e.values[8];
//General Items
// Get document template, copy it as a new temp doc, and save the Doc’s id
var copyId = DriveApp.getFileById(docTemplate)
.makeCopy(docName+' for '+jobsite)
.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('keySiteName', jobsite);
copyBody.replaceText('keyDateTime', date_time);
copyBody.replaceText('keySubmitName', submit_name);
copyBody.replaceText('keyWeather', weather);
copyBody.replaceText('keyTemp', temp);
copyBody.replaceText('keySuperName',super_name);
copyBody.replaceText('keyJobNum', job_num);
// Save and close the temporary document
copyDoc.saveAndClose();
// Convert temporary document to PDF
var pdf = DriveApp.getFileById(copyId).getAs("application/pdf");
// Attach PDF and send the email
var subject = "Jobsite Inspection Report";
var body = "Here is the Jobsite Inspection Report for " + jobsite + "";
MailApp.sendEmail(email_address, subject, body, {htmlBody: body, attachments: pdf});
// Delete temp file
DriveApp.getFileById(copyId).setTrashed(true);
}
I have installed the onFormSubmit trigger for both sheets and forms and have successfully sent emails with attachments from scripts in both forms and sheets by deleting the e.values (in the sheets script I use e.response when the script is installed on a form) and assigning static values to the variables (see the section of code marked "Use this section when attached to form"). I receive an email, on the correct template with the correct static data.
When I try to use the e.values or e.responses section, nothing. I don't even get an email or error.
I did get an email (several in fact) a few hours later stating "TypeError: Cannot read property "2" from undefined. (line 25, file "Code")" I'm thinking the e.values[2] is my problem (or the way it's formatted, or more specifically defined). The script appears to be looking for the form?
Upvotes: 4
Views: 4029
Reputation: 41
I finally gave up and started over and and it works! the script is the same as the one listed above (without all the commented sections I used while troubleshooting). the only thing I can say might be different are the setting on the form itself. For anyone having this same issue, I would check the settings on the form. On the form that works, the setting "Allow responders to edit responses after submitting" is un checked where as I think this was checked on the first form I used. If that's not it, I'm still at a loss.
Upvotes: 0
Reputation: 4034
The submit event object needs further work before you can access it the way you are indicating. If you want to access the responses as a simple array you'll need to transform from a FormResponse object and the indiidual ItemResponse values
function onFormSubmit(e) {
// you need to add this line to add offline event object access auth
// run once in script editor - you could delete it after if you want
var f = FormApp.getActiveForm();
// test for response and use static if none exists
var responses = [
"test data",
"test data2",
"test data3",
"test data4",
"test data5",
"test data6",
"test data7"
];
responses = (e && e.response) ?
e.response.getItemResponses()
.map(function (r) {
return r.getResponse();
}) : (e && e.values) ? e.values : responses;
// snip //
var jobsite = responses[2];
var date_time = responses[3];
var submit_name = responses[4];
var weather = responses[5];
var temp = responses[6];
var super_name = responses[7];
var job_num = responses[8];
// snip //
}
Upvotes: 1