Google Script通过电子邮件发送表单值,错误:无法读取属性" namedValues"

时间:2015-03-11 21:28:06

标签: javascript php google-apps-script google-apps

项目密钥:MyvPlY2KvwGODjsi4szfo389owhmw9jII

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

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

当我填写表格时,应该将内容通过电子邮件发送到我的电子邮箱。

我现在面临的问题是,通过表单值的函数不起作用。它返回错误" TypeError:无法读取属性" namedValues"来自undefined。 (第15行,文件"代码")"关于下面的代码:

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

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

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

3 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:3)

我有同样的错误,我花了一些时间来弄清楚如何解决它。

问题是您在表单中编写代码,并且应该在电子表格中执行此操作。

当我在窗体中创建函数并注册事件时,它被调用但参数没有相同的结构(并且没有字段namedValues,因此错误“无法读取属性”namedValues“来自未定义“)。检查这个的更好方法是将对象记录为JSON字符串:

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

所以,我为纠正这个问题所采取的步骤:

  
      
  1. 创建电子表格
  2.   
  3. 通过电子表格创建表单(在电子表格内选择工具 - >创建表单),然后创建表单
  4.   
  5. 返回电子表格,然后创建脚本(工具 - >脚本编辑器)
  6.   
  7. 编写您的功能
  8.   
  9. 注册功能(编辑 - >当前项目的触发器):function sendFormByEmail,Event:From Spreadsheet - &gt;在表格提交
  10.   

希望这有帮助

答案 2 :(得分:0)

此代码行中存在错误:

message += headers[i] + ': '+ e.namedValues[headers[i]].toString() + "\n\n";

这是这部分:

e.namedValues[headers[i]].toString()

我只是猜测你想要:

e.namedValues + [headers[i]].toString()

如果要连接值,则缺少加号。