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

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

问题描述

项目关键字:MyvPlY2KvwGODjsi4szfo389owhmw9jII



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

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



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



我现在面临的问题是通过表单值的函数不起作用。它返回错误TypeError:Can not read propertynamedValuesfrom undefined。(line 15,fileCode)for the following piece of code:

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

我不太熟悉Google脚本,所以我不确定如何解决此问题。你有什么建议吗?我已经包含了下面的整个脚本。

 函数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 message =;
var subject =新用户表单;

//变量e保存数组中的所有表单值。
//遍历数组并将值附加到主体。

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

//将变量从电子表格插入主题。
//在这种情况下,我希望将新雇员的姓名和开始日期作为
//电子邮件主题的一部分。这是我的表格中的第3和第16列。
//这会创建一个电子邮件主题,如New Hire:Jane Doe - starts 4/23/2013​​
subject + = e.namedValues [headers [2]]。toString()+ - starts + e.namedValues [headers [15]]。toString();

//发送电子邮件
MailApp.sendEmail(email,subject,message);


解决方案

该函数似乎未定义因为'e'不作为函数上下文的一部分被接收。您需要设置提交表单的触发器,并将信息发送给函数。您可以使用以下代码:

  / *使用Google表单发送确认电子邮件* / 
函数Initialize(){
var triggers = ScriptApp.getProjectTriggers();
for(var i in triggers){
ScriptApp.deleteTrigger(triggers [i]);

ScriptApp.newTrigger(SendConfirmationMail)
.forSpreadsheet(SpreadsheetApp.getActiveSpreadsheet())
.onFormSubmit()
.create();
}


函数SendConfirmationMail(e){
尝试{
var ss,cc,sendername,subject,columns;
var header,message,value,textbody,sender,itemID,url;

//这是您的电子邮件地址,您将在CC
cc =name@email.com;

//这将显示为发件人姓名
sendername =要显示为发件人的姓名;

//可选,但更改以下变量
//为Google Docs电子邮件定制自定义主题
subject =选择一个approppiate主题;

//这是自动回复的正文
message =;

ss = SpreadsheetApp.getActiveSheet();
columns = ss.getRange(1,1,1,ss.getLastColumn())。getValues()[0];

//这是提交者的电子邮件地址
sender = e.namedValues [Username]。toString();

//只包含非空白的表单值
(列中的var键){
var key = columns [keys];
//使用这个来寻找一个特定的命名键
if(e.namedValues [key]){
if(key ==Username){
header =用户+ e.namedValues [key] +已提交表单,请查看以下信息。< br />;
} else {
message + = key +'::< br /> '+ e.namedValues [key] +< br />;
}
}
}
}

textbody = header + message;
textbody = textbody.replace(< br>,\\\
);

Logger.log(发送电子邮件);
GmailApp.sendEmail(cc,subject,textbody,
{cc:cc,name:sendername,htmlBody:textbody});


$ b} catch(e){
Logger.log(e.toString());
}
}


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() + "\n\n";

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() + "\n\n";     

  // 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); 
}

解决方案

The function appears to be undefined as 'e' is not received as part of the function context. You'll need to set a trigger for the submission of the form, and send the information to the function. You can use the following code:

/* Send Confirmation Email with Google Forms */
function Initialize() {
  var triggers = ScriptApp.getProjectTriggers();
  for (var i in triggers) {
    ScriptApp.deleteTrigger(triggers[i]);
  }
  ScriptApp.newTrigger("SendConfirmationMail")
  .forSpreadsheet(SpreadsheetApp.getActiveSpreadsheet())
  .onFormSubmit()
  .create();
}


function SendConfirmationMail(e) {
  try {
    var ss, cc, sendername, subject, columns;
    var header, message, value, textbody, sender, itemID, url;

// This is your email address and you will be in the CC
cc = "name@email.com";

// This will show up as the sender's name
sendername = "name to be displayed as sender";

// Optional but change the following variable
// to have a custom subject for Google Docs emails
subject = "Choose an approppiate subject";

// This is the body of the auto-reply
message = "";

ss = SpreadsheetApp.getActiveSheet();
columns = ss.getRange(1, 1, 1, ss.getLastColumn()).getValues()[0];

// This is the submitter's email address
sender = e.namedValues["Username"].toString();

// Only include form values that are not blank
for ( var keys in columns ) {
  var key = columns[keys];
//Use this to look for a particular named key
  if ( e.namedValues[key] ) {
    if ( key == "Username" ) {
      header = "The user " + e.namedValues[key] + " has submitted the form, please review the following information.<br />";
    } else {
        message += key + ' ::<br /> '+ e.namedValues[key] + "<br />"; 
      }
    }
  }
}

textbody = header + message;
textbody = textbody.replace("<br>", "\n");

Logger.log("Sending email");
GmailApp.sendEmail(cc, subject, textbody, 
                   {cc: cc, name: sendername, htmlBody: textbody});



} catch (e) {
    Logger.log(e.toString());
  }
}

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

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