将 google 工作表中的值发送到电子邮件的脚本

Script to send values in google sheets to an email

我找到了一些脚本并尝试对其进行修改,以使其适用于我的用例,但 运行 仍然存在错误。基本上我有一个 google 表单,当用户提交时,我希望他们刚刚输入的行发送到我的电子邮件。我不断收到错误消息:"Cannot read property "namedValues" from undefined。(第 14 行,文件 "Code")"

感谢任何帮助,谢谢!!

function sendFormByEmail(e) 
{    
  var email = "dummyemail@dummy.com"; 

  var s = SpreadsheetApp.getActiveSheet();
  var headers = s.getRange(1,1,1,s.getLastColumn()).getValues()[0];    
  var message = "";
  var subject = "New Hire: ";

  // 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.
  subject += e.namedValues[headers[2]].toString() + " - starts " + e.namedValues[headers[15]].toString();

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

}

可以检查headers[i]是否为空,替换为:

message += "" + headers[i] + " : " + datarow[i] + "\n\n";

与:

if (datarow[i] != "") {
message += "" + headers[i] + " : " + datarow[i] + "\n\n"; 
}

此外,如果您允许用户编辑他们的回复,那么您的数据行将不正确。您可以使用:

var datarow = sheet.getRange(e.range.getRow(),1,1,sheet.getLastColumn())‌.getValues()[0];