How do I resolve this “TypeError: Cannot read prop

2019-09-20 18:31发布

The script below from the simple form below always generates the following error;

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

Form Response Sheet:

https://docs.google.com/spreadsheets/d/1RxBquGs4NGhX2TVRqXLRzntMubMzANB9lJVg9LB2KFA/edit#gid=1946610866

Script:

function onFormSubmit(e) {
  var timestamp = e.values[0];
  var email     = e.values[1];
  var name      = e.values[2];

  var template   = HtmlService.createTemplateFromFile("ProfileEmail");
  template.name  = name;
  template.email = email;

  MailApp.sendEmail(email,
                "Thank you for your time!",
                "",
                { htmlBody: template.evaluate().getContent() });
}

2条回答
The star\"
2楼-- · 2019-09-20 19:22

In the new version of forms and sheets, you can also use named values, example.

var timestamp = e.namedValues.Timestamp;
var email = e.namedValues.email;
var name = e.namedValues.name;

The value following "namedValues" needs to be the same as the column header your looking for.

You can always add "Logger.log(e)" just above timestamp there and get what ever values are passed to the script, to see if your even receiving what your looking for.

With out seeing the rest of code the html part, not exactly sure where issue is.

查看更多
Rolldiameter
3楼-- · 2019-09-20 19:25

If you're testing your trigger function in the debugger, e will be undefined.

See How can I test a trigger function in GAS?.

查看更多
登录 后发表回答