-2

I'm working on this template to send an email to the person that fill a form, the problem is that everytime I try to run it I get this error

TypeError: Cannot read property "values" from undefined. (line 4, file "Code")

Can someone help me out figuring out what the problem is?

function emailOnFormSubmit(e) {
    
    // Create as many variables as answers (columns in your spreadsheet) you require to send
    var Timestamp = e.values[0];
    var mail = e.values[1];
    var name = e.values[2];
    var Break = e.values[3];
    
    
    // The subject of the email
    var subject = "Break time! " + name;

    // emailBody is for those devices that can't render HTML, is plain text
    var emailBody = "Hola " + name + "tu hora de salida es " + Timestamp + "tu hora de regreso es " + Timestamp + time (0,20,0) +
                    "\nFrom " + city + 
                    "\nWith email " + mail + 
                    "\nRegister on " + timestamp +
                    "\n\nThank you for register!"; 
    
    // html is for those devices that can render HTML
    // nowadays almost all devices can render HTML
    var htmlBody =  "Thank you, your form was submitted on <i>" + timestamp + "</i>" + 
                    "<br/><br/>The details you entered were as follows: " +
                    "<br/>Your Name: <font color=\"red\"><strong>" + name + "</strong></font>" +
                    "<br/>From: " + city + 
                    "<br/>With email: " + mail;
    
    // More info for Advanced Options Parameters 
    // https://developers.google.com/apps-script/reference/mail/mail-app#sendEmail(String,String,String,Object)
    var advancedOpts = { name: "No reply", htmlBody: htmlBody };

    // This instruction sends the email
    MailApp.sendEmail(mail, subject, emailBody, advancedOpts);

}
Rubén
  • 34,714
  • 9
  • 70
  • 166
  • Put this at line 2: `console.log(e);` Put this at line 3: `console.log(e.values[0]);` What is the output of both of these? – KayakinKoder Dec 19 '17 at 21:01
  • It give's me this error message "TypeError: Cannot read property "values" from undefined. (line 3, file "Code")" – Alan Uhh Yeah Dec 19 '17 at 21:04
  • Check if it exists first. if (e.values[0]){ var Timestamp = e.values[0]; } – Dr Upvote Dec 19 '17 at 21:05
  • You definitely have a problem with e, so you need to figure out why first, but to remove the error, your best bet is to do something like: let { values = [] } = e; let timestamp = values[0] || ""; You'll get back undefined instead of an error. – stevenlacerda Dec 19 '17 at 21:06
  • where does `emailOnFormSubmit` get called – Daniel A. White Dec 19 '17 at 21:06
  • `e` is undefined, simple as that. Show us where `e` comes from and we might be able to help. – takendarkk Dec 19 '17 at 21:07
  • Same error message even if I add the if – Alan Uhh Yeah Dec 19 '17 at 21:08
  • This information is entered in a google spreadsheet, so the script is wrong it self correct? – Alan Uhh Yeah Dec 19 '17 at 21:11
  • 1
    If you are running the script by clicking the 'play' button, there is no event (no form submission if the script is triggered on form submit). Hence that event object (e) will be undefined. – JPV Dec 19 '17 at 21:17
  • Ok, so what you need to do is inspect e in your console. Look at result of `console.log(e);` If you are using google chrome or firefox, you can click on the result of it and expand to see the details of e. You should see that it does not have anything in it named "values". Right? – KayakinKoder Dec 19 '17 at 21:19

1 Answers1

0

Little context:

Your error says TypeError: Cannot read property "values" from undefined. (line 4, file "Code") and I think line 4 is var Timestamp = e.values[0];.

In this line, you're trying to fetch the values passed in Object e at 0th position and error is saying that the Object e is not defined. This usually happens when we try to run a function manually without supplying all the arguments it needs.

Here, as per the information you have provided, it means that you want this function to be triggered whenever anyone fills a form and this function will send an email to that person. For this function to work properly, you must supply Object e to this function while calling it.

Why is this error coming up:

When you're trying to run it manually (by selecting it from dropdown and clicking execute button) then you're not providing it with the argument it needs that's Object e.

Proposed solution:

Create a trigger based on form submission and call this function from there, that trigger will provide this argument while calling it. I mean, whenever any form will be submitted, that trigger will call this function with that response.

Pang
  • 9,564
  • 146
  • 81
  • 122
Umair Mohammad
  • 4,489
  • 2
  • 20
  • 34