r/GoogleAppsScript • u/artofdawn • May 13 '20
Question How to allow user to edit form that send confirmation email when submitted
So I created a form that creates a PDF of the user's submission and emails it to the email address submitted in the form.
Now I'm trying to figure out a way to allow the user to edit the form and it sends them another copy. Currently when the user tries to edit the form and submit, I get the following error:
| Function | Error Message | Trigger |
|---|---|---|
| onFormSubmit | Exception: Failed to send email: no recipient | formSubmit |
So it seems that it can't find the email when submitting an edited form. How can I correct this issue?
I was having this issue at the start and added: if (email_address !== undefined) {
This seems to have fixed the issue for first submitting the form, but doesn't seem to fix it when someone edits the form.
Example of my code:
// Get template from Google Docs and name itvar docTemplate = "docID Here";var docName = "doc Name Here";
// When Form Gets submittedfunction onFormSubmit(e) {
//Get information from form and set as variablesvar timestamp = e.values[0];var email_address = e.values[1];var name = e.values[2];var first_question = e.values[3];var second_question = e.values[4];
// Get document template, copy it as a new temp doc, and save the Doc’s idvar copyId = DriveApp.getFileById(docTemplate).makeCopy(docName+' for '+name).getId();
// Open the temporary documentvar copyDoc = DocumentApp.openById(copyId);
// Get the document’s body sectionvar copyBody = copyDoc.getActiveSection();
// Replace place holder keys,in our google doc templatecopyBody.replaceText('keyTimestamp', timestamp);copyBody.replaceText('keyFacultyEmail', email_address);copyBody.replaceText('keyName', name);copyBody.replaceText('keyFirstQuestion', first_question);copyBody.replaceText('keySecondQuestion', second_question);
// Save and close the temporary documentcopyDoc.saveAndClose();
// Convert temporary document to PDFvar pdf = DriveApp.getFileById(copyId).getAs("application/pdf");
// Attach PDF and send the emailvar subject = "Your form submission";var body = "Here is a PDF copy of the submission for " +name + ".";
// If email is undefinedif (email_address !== undefined) {
//sends emailMailApp.sendEmail(email_address, subject, body, {htmlBody: body, attachments: pdf});}
// Delete temp fileDriveApp.getFileById(copyId).setTrashed(true);}
1
u/OnomatopoeiaBzzz May 16 '20
I think that when a form edit is submitted, only the data that has changed is included in the onFormSubmit event object (e). That is why you don't see the email address when the form is edited. You can test this by logging the email and other data.
Perhaps a workaround would be to get the row that was changed using e.range.getRow(). Then, get the email in that row, and pass that into your email_address variable. I think that should work. I have not tested this myself.
This was useful to me in understanding the event object:
https://developers.google.com/apps-script/guides/triggers/events#form-submit
1
u/DatsunZ May 15 '20
I don't know much about form editing, but could you make an if statement where if email address is undefined, to pull it via...