2

Here's a google app script that sends a custom email to those submitting a Google Form (emails collected in column E):

function sendFormEmails(e) {
    var emailAddress = e.values[4];
    var subject = "subject";
    var message = "message";
    var fromAddress = "fromemail@emaildomain.com";
    var ccAddress = "ccemail@emaildomain.com";
    MailApp.sendEmail(emailAddress, subject, message, {htmlBody: message, from: fromAddress, cc: ccAddress});
}

A On Form Submit trigger is set up on this function. When someone submits the google form for the first time, it works fine (aside from sending the email from the specified fromAddress; it only sends from the Google account associated with the script). When the respondent edits their form, however, this function also executes. In this case, an email is sent from the Google account associated with the script and cc'd to (ccemail@emaildomain.com). It isn't sent to the respondent unless they change their email on the edit form. I don't want this function to fire at all when a response is edited. Is there a simple conditional logic to write into this app script so that it only fires when a new response is submitted? Also, is there any way to have the email sent from the specified fromAddress opposed to the google account associated with the script?

I tried the onFormSubmit function, but this still sends when a Form is edited.

Josh Kumin
  • 21
  • 2
  • try checking the [trigger.evertType](https://developers.google.com/apps-script/reference/script/event-type) – Cooper Jul 15 '23 at 21:23

1 Answers1

2

Regarding the from address, based off of this answer, you should be able to set it using GmailApp.sendEmail Looking at the documentation, the only caveat is that the specified from address needs to be set up as an alias on the account associated with the script. MailApp.sendEmail does not appear to support the from parameter according to the documentation, so if you want to use this method, you'll need to set up the script on that account if possible.

Regarding differentiating between onFormSubmit and onFormEdit, similar to this answer, you'll want to leverage the event object. It looks like Google doesn't really distinguish between the two events, submitting a form and editing a previously submitted form will both trigger the same onFormSubmit. Unfortunately, there's no top level parameter on the event object that you can use, but every edit/update to a previously submitted form submission includes a little note (Responder updated this value.) on the edited cell, so we can test for the presence of at least one of these notes to run the code conditionally. Most likely you can do something like this:

function sendFormEmails(e) {
   var notes = e.range.getNotes();
   var hasNotes = notes[0].some((e) => e !== ''); // checks to see if at least one of the cell values includes a note
   if (!hasNotes) {
    var emailAddress = e.values[4];
    var subject = "subject";
    var message = "message";
    var fromAddress = "fromemail@emaildomain.com";
    var ccAddress = "ccemail@emaildomain.com";
    MailApp.sendEmail(emailAddress, subject, message, {htmlBody: message, from: fromAddress, cc: ccAddress});
   }
}
  • I just tested this. When someone edits the form for the first time, this function still executes. BUT, for any subsequent edit it doesn't. Perhaps, since the notes don't appear before the first edit, it still runs? Any idea for preventing the function from running on the first edit? EDIT: Actually, it appears it still runs even after the first edit. – Josh Kumin Jul 15 '23 at 22:29
  • Are you able to share a sample spreadsheet that I can use to check this out? As far as I understand, on a brand new submission, `e.range.getNotes()` will just give you an array of empty strings (ie `[[''],[''],['']]`), and then when a user updates a form it will be an array of notes (ie `[[''],['This was updated'],['']]`). – Miguel Rivera Rios Jul 15 '23 at 22:51
  • I'm sorry, I'm an idiot, I completely forgot what the structure of spreadsheet cells looked like. If a brand new form is submitted, `getNotes` will return `[['', '', '']]`, and then when a form is updated it will return `[['', 'This was updated', '']]`. I modified the code, so it should hopefully work now. – Miguel Rivera Rios Jul 15 '23 at 23:04