Google Script 通过电子邮件发送表单值,错误:无法读取属性“namedValues"; [英] Google Script send form values by email, error: cannot read property "namedValues"

查看:39
本文介绍了Google Script 通过电子邮件发送表单值,错误:无法读取属性“namedValues";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

项目密钥:MyvPlY2KvwGODjsi4szfo389owhmw9jII

我正在尝试运行一个脚本,该脚本在每次提交表单时通过电子邮件发送我的表单内容.我一直按照下面这个链接中的说明进行操作,直到我开始出现错误,并且收到了更改脚本以按 ID 打开电子表格的建议:

http://www.snipe.net/2013/04/email-contents-google-form/

当我完成表格时,它应该将内容通过电子邮件发送到我的电子邮件中.

我现在遇到的问题是通过表单值的函数不起作用.关于以下代码段,它返回错误TypeError:无法读取未定义的属性namedValues".(第 15 行,文件代码")":

for(var i in headers)message += headers[i] + ': '+ e.namedValues[headers[i]].toString() + "

";

我对谷歌脚本不太熟悉,所以我也不知道如何解决这个问题.你有什么推荐的吗?我在下面包含了整个脚本.

函数 sendFormByEmail(e){//记得用你自己的邮箱地址替换这个邮箱地址var email = "sample@email.com";var s = SpreadsheetApp.openById("1hOqnK0IVa2WT6-c-MY0jyGGSpIJIV2yzTXdQYX4UQQA").getSheets()[0];var headers = s.getRange(1,1,1,s.getLastColumn()).getValues()[0];var 消息 = "";var subject = "新用户表单";//变量 e 将所有表单值保存在一个数组中.//遍历数组并将值附加到主体.for(var i 在标题中)message += headers[i] + ': '+ e.namedValues[headers[i]].toString() + "

";//将电子表格中的变量插入主题.//在本例中,我希望将新员工的姓名和开始日期作为//电子邮件主题.这些是我的表格中的第 3 列和第 16 列.//这会创建一个电子邮件主题,例如新员工:Jane Doe - 2013 年 4 月 23 日开始"主题 += e.namedValues[headers[2]].toString() + " - 开始 " + e.namedValues[headers[15]].toString();//发送电子邮件MailApp.sendEmail(email, subject, message);}

解决方案

我也遇到了同样的错误,我花了一些时间才弄清楚如何解决它.

问题在于您是在表单中编写代码,而您应该在电子表格中进行编写.

当我在表单内创建函数并注册事件时,它被调用但参数没有相同的结构(并且没有字段namedValues,因此错误无法读取属性namedValues"来自不明确的").检查这一点的更好方法是将对象记录为 JSON 字符串:

Logger.log("e:" + JSON.stringify(e));

因此,我为解决此问题而采取的步骤:

<块引用>

  1. 创建电子表格
  2. 通过电子表格创建表单(在电子表格中选择工具->创建表单),然后创建您的表单
  3. 返回电子表格,然后创建脚本(工具->脚本编辑器)
  4. 编写函数
  5. 注册函数(编辑->当前项目的触发器):函数sendFormByEmail,事件:来自电子表格->表单提交

希望能帮到你

Project key: MyvPlY2KvwGODjsi4szfo389owhmw9jII

I am trying to run a script that sends the contents of my form by email each time the form is submitted. I was following the instructions from this link below exactly until I started getting errors and I received advice to change my script to open spreadsheets by id:

http://www.snipe.net/2013/04/email-contents-google-form/

When I complete the form, it is supposed to email the contents to my email.

The problem I am nowhaving is that the function which goes through the form values doesn't work. It's returning the error "TypeError: Cannot read property "namedValues" from undefined. (line 15, file "Code")" in regards to the piece of code below:

for(var i in headers) 
   message += headers[i] + ': '+ e.namedValues[headers[i]].toString() + "

";

I am not too familiar with google scripts so I'm not sure how to work around this issue either. Is there anything you recommend? I have included the entire script below.

function sendFormByEmail(e) 
{    
  // Remember to replace this email address with your own email address
  var email = "sample@email.com"; 

  var s = SpreadsheetApp.openById("1hOqnK0IVa2WT6-c-MY0jyGGSpIJIV2yzTXdQYX4UQQA").getSheets()[0];
  var headers = s.getRange(1,1,1,s.getLastColumn()).getValues()[0];    
  var message = "";
  var subject = "New User Form";

  // The variable e holds all the form values in an array.
  // Loop through the array and append values to the body.

  for(var i in headers) 
    message += headers[i] + ': '+ e.namedValues[headers[i]].toString() + "

";     

  // Insert variables from the spreadsheet into the subject.
  // In this case, I wanted the new hire's name and start date as part of the
  // email subject. These are the 3rd and 16th columns in my form.
  // This creates an email subject like "New Hire: Jane Doe - starts 4/23/2013"
  subject += e.namedValues[headers[2]].toString() + " - starts " + e.namedValues[headers[15]].toString();

  // Send the email
  MailApp.sendEmail(email, subject, message); 
}

解决方案

I have the same error and it took me some time to figure out how to solve it.

The problem is that you are writing your code in the form, and you should do it in your spreadsheet.

When I created the function inside the form and registered the event, it was called but the parameter didn't have the same structure (and didn't have the field namedValues, so the error "Cannot read property "namedValues" from undefined"). A better way to check this is to log the object as a JSON string:

Logger.log("e:  " + JSON.stringify(e));

So, the steps I have made to correct this issue:

  1. Create a spreadsheet
  2. Create the form through the spreadsheet (inside the spreadsheet select Tools->Create a form), and create your form
  3. Go back to your spreadsheet, and create the script (Tools->Script Editor)
  4. Write your function
  5. Register the function (Edit->Current Project's Triggers): function sendFormByEmail, Event: From Spreadsheet -> On form submit

Hope this helps

这篇关于Google Script 通过电子邮件发送表单值,错误:无法读取属性“namedValues";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆