电子邮件脚本不起作用

时间:2014-07-13 22:44:26

标签: google-apps-script google-sheets

我正在尝试在提交表单时向我发送电子邮件,并且信息会填充电子表格。它在昨天工作,但从那以后我得到的都是一个错误

  

7/13/14 5:23 PM sendFormByEmail TypeError:无法调用方法   "的toString"未定义的。 (第20行,文件"电子邮件脚本")formSubmit

enter image description here

function sendFormByEmail(e) 
{    
  // Remember to replace XYZ with your own email address
  var email = "lumbyb@gmatc.matc.edu"; 

  // Optional but change the following variable
  // to have a custom subject for Google Docs emails
  var subject = "PC Repair Ticket";  

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

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

  // Credit to Henrique Abreu for fixing the sort order

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

  // This is the MailApp service of Google Apps Script
  // that sends the email. You can also use GmailApp here.

  MailApp.sendEmail(email, subject, message); 

  // Watch the following video for details
  // http://youtu.be/z6klwUxRwQI

  // By Amit Agarwal - www.labnol.org
}

1 个答案:

答案 0 :(得分:0)

在转换为字符串之前检查该值是否存在。

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