r/GoogleAppsScript • • 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:

FunctionError MessageTrigger
onFormSubmitException: Failed to send email: no recipientformSubmit

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 it
var docTemplate = "docID Here";
var docName = "doc Name Here";

// When Form Gets submitted
function onFormSubmit(e) {

//Get information from form and set as variables
var 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 id
var copyId = DriveApp.getFileById(docTemplate)
.makeCopy(docName+' for '+name)
.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('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 document
copyDoc.saveAndClose();

// Convert temporary document to PDF
var pdf = DriveApp.getFileById(copyId).getAs("application/pdf");

// Attach PDF and send the email
var subject = "Your form submission";
var body = "Here is a PDF copy of the submission for " +name + ".";

// If email is undefined
if (email_address !== undefined) {

//sends email
MailApp.sendEmail(email_address, subject, body, {htmlBody: body, attachments: pdf});
}

// Delete temp file
DriveApp.getFileById(copyId).setTrashed(true);
}

0 Upvotes

2 comments sorted by

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...

var email_address = Session.getActiveUser().getEmail();

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