2012-12-20 32 views
2

我已經使用了遍佈網絡的多個腳本,並且遇到了同樣的問題。我有幾個谷歌文檔的形式,我需要通過電子郵件接收提交的數據(不只是通知表單已提交和相應的電子表格已更新)。這個劇本是工作,但已經停止由於某些原因:在電子郵件中接收Google文檔表單數據

function sendFormByEmail(e) 
{  
// Remember to replace XYZ with your own email address 
var email = "[email protected]"; 

// 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 
} 

對此我收到一個錯誤:類型錯誤:無法從不確定的讀取屬性「namedValues」。 (第20行)

我什麼也沒有改變,現在我找不到任何表單提交到電子郵件腳本工作。誰能幫忙?

回答

0

你必須做一些調試出現,將測試案例,直到你解決您的問題,比如,像你得到一個line 20錯誤namedValues,訪問它像這樣前檢查e

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

這可能是由於某些列中的空白數據。我建議刪除/移除所有現有的觸發器的形式,然後用下面的代碼(我稱之爲代碼由阿米特·阿加瓦爾 - 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 = "[email protected]"; 

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

} 

然後,只需保存,運行用來初始化功能,接受權限。