0
votes

I have a google form with the following columns:

Name | email | have_kids

where Name is just text input, email is just an email, and have_kids is a yes/no radio button

ex. dmkumar | [email protected] | Yes

when the user submits the form, the data is sent to a google spreadsheet. I would like to have it set up so that if the user selected "Yes" to have_kids, then an email is sent to that user. Here is the code I have right now. I have a trigger event to run the function, from the spreadsheet, on the form submit.

Here is the code I have now (that is not working):

function emailConfirmation(e) {
  var userEmail = e.values[11]; //column k
  var check = e.values[14]; //column n


  /**
   * Un-comment this to use it for debugging
   */
  //for (var i in e.values) {
  //  Logger.log("" + i + ":" + e.values[i]);
  //}

  var subject = "Sending email from a spreadsheet";
  var message = "Hello World";
  if(check == "Yes")
  {
    MailApp.sendEmail(userEmail, subject, message);
  }
}

I am new to google scripting, so simple explanations would be much appreciated! Thanks!

1
Could you explain what you mean by "that is not working"? It's hard to know what's going on without an error, etc. Also, could you check and see if your emails are appearing in the Sent folder of the spreadsheet owner's email account? - Tricky12
When I submit a form, with some of my own data (and make sure to click yes on have_kids) I don't get an email. In terms of errors, the logs say that e is undefined if I run it from there, but I would expect that since no form was submited - bestfriendkumar
Are you sure that e.values is actually filling in your userEmail var? Also, you should try a simple email to see if the .sendEmail() is working for you at all. Like MailApp.sendEmail("[email protected]","Test","This is a test email");. - Tricky12
I did that and it worked, I also figured out how to get the most recent form submission into the email. I'm gonna tinker with it some more, and I'll post it when I figure out the full problem. - bestfriendkumar
Good to hear mate, good luck! - Tricky12

1 Answers

1
votes

This is the answer I came up, because I'm sure people will stumble upon this page sarcasm

But it works great, none the less.

function emailConfirmation(e) {

  var userEmail = e.namedValues["Email Address"].toString(); //Where "Email Address" is the name of a column
  var kids = e.namedValues["Some of my guests are children who will attend the kids' camp."].toString();

  var subject = "Sending email from a spreadsheet";

  if(kids == "Yes")
  {
     MailApp.sendEmail(userEmail, subject, kids);
  }  
}