2

Web で見つけた複数のスクリプトを使用しましたが、同じ問題が発生しています。送信されたデータを電子メールで受け取る必要がある Google ドキュメント フォームがいくつかあります (フォームが送信され、対応するスプレッドシートが更新されたという通知だけではありません)。このスクリプトは動作していましたが、何らかの理由で停止しました:

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: Cannot read property "namedValues" from undefined. (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 に答える