2

我使用了在网上找到的多个脚本,并且遇到了同样的问题。我有几个谷歌文档表单,我需要通过电子邮件接收提交的数据(不仅仅是表单已提交和相应电子表格已更新的通知)。该脚本可以正常工作,但由于某种原因已停止:

function sendFormByEmail(e) 
{    
// Remember to replace XYZ with your own email address
var email = "info.wchs@gmail.com"; 

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

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

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

我收到一个错误:TypeError:无法从未定义中读取属性“namedValues”。(第 20 行)

我什么都没做,现在我找不到任何表单提交到电子邮件脚本来工作。任何人都可以帮忙吗?

4

2 回答 2

0

这可能是因为某些列中的空白数据。我建议删除/删除表单的所有现有触发器,然后使用下面的代码(我提到了 Amit Agarwal 的代码 - www.labnol.org)

function Initialize() {

  var triggers = ScriptApp.getProjectTriggers();

  for(var i in triggers) {
    ScriptApp.deleteTrigger(triggers[i]);
  }

  ScriptApp.newTrigger("SendGoogleForm")
  .forSpreadsheet(SpreadsheetApp.getActiveSpreadsheet())
  .onFormSubmit()
  .create();

}

function SendGoogleForm(e) 
{  
  // The variable e holds all the form values in an array.
  // Loop through the array and append values to the body.
  try 
  {      
    // You may replace this with another email address
    var email = "xyw@mail.com";

    // Optional but change the following variable
    // to have a custom subject for Google Form email notifications
    var subject = "Training Application Form Submitted";  

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

    // Only include form fields that are not blank
    for ( var i in columns ) 
    {
      var header = columns[i];
      if ( e.namedValues[header] && (e.namedValues[header] != "") ) 
      {
        message += header + ' :: '+ e.namedValues[header] + "\n\n"; 
      }
    }
    // This is the MailApp service of Google Apps Script
    // that sends the email. You can also use GmailApp for HTML Mail.

    MailApp.sendEmail(email, subject, message); 

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

}

然后只需保存,运行初始化函数,接受权限。

于 2015-03-04T12:36:34.227 回答
0

您必须在那里进行一些调试,插入测试用例,直到您解决问题,例如,如果您收到line 20错误,请在访问它之前进行namedValues检查,如下所示:e

for(var i in headers)
    if (e && e.namedValues)
       message += headers[i] + ' = '+ e.namedValues[headers[i]].toString() + "\n\n"; 
于 2012-12-20T17:45:56.967 回答