2012-05-17 34 views
0

我使用腳本將表單結果每次提交表單時通過電子郵件發送給我。整個代碼張貼在這裏谷歌論壇:電子郵件Google表單結果不起作用

http://groups.google.com/a/googleproductforums.com/d/topic/apps-script/1rObNfuez6k/discussion

下面是當前的代碼:

function contactUsMailer2(e) { 

// var ss = SpreadsheetApp.getActive(); 
// SpreadsheetApp.setActiveSpreadsheet(ss); 

    var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Data'); //default sheets - change if you 
    var setup = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Setup'); //rename the sheets 

    var width = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Data').getLastColumn(); //get "width" of sheet, used to enumrate fields 
    var myEmailAddress = setup.getRange('B3').getValue(); //get email recipient(s) from "Setup" (default) 
    var myEmailSubject = setup.getRange('B4').getValue(); //get a subject line from "Setup" (default) 
                  //from row 1 of "Data" 
    var sheetURL = SpreadsheetApp.getActiveSpreadsheet().getUrl(); //get sheet url to include in email 
    var formURL = SpreadsheetApp.getActiveSpreadsheet().getFormUrl(); //get form url to include in email 

    var column = 'A'; 
    var message = ''; 
    var htmlBody = '<html><body><table>'; //generated email will have both an HTML table and a plain-text part 
              //to be RFC compliant. beginning of html and table. 
    for (var c=1; c<=width; ++c) { 
     var data = sheet.getRange(1, c, 1, 1).getValue(); //this gets the "filed" names from the 1st row of "Sheet1" 

     try {  
      //retrieve field data and build the HTML and plain-text emails 
      var message = message + data + ': ' + e.namedValues[data].toString() + '\r\n'; //plain-text 
      var htmlBody = htmlBody + '<tr><td><b>' + data + ': </b></td><td>' + e.namedValues[data].toString() + '</td></tr>'; //HTML 
     } 
     catch (a) { 
      var message = message + data + ': n/a\r\n'; 
      var htmlBody = htmlBody + '<tr><td><b>' + data + ': </b></td><td>n/a</td></tr>'; 
     } 

     var column = String.fromCharCode(column.charCodeAt() + 1); //increment column letter 
    } 

    var htmlBody = htmlBody + '</table>'; //close table 

    var message = message + '\r\n\r\nData from form located at ' + formURL + '\r\n'; //put form url in text email 
    var message = message + 'Data posted to spreadsheet at ' + sheetURL; //put sheet url in text email 

    var htmlBody = htmlBody + '<br><br>Data from form located <a href=\"' + formURL + '\">here</a><br>'; //put form url in HTML email 
    var htmlBody = htmlBody + 'Data posted to spreadsheet <a href=\"' + sheetURL + '\">here</a><br>'; //put sheet url in HTML email 
    var htmlBody = htmlBody + '</body></html>'; //close html 

    MailApp.sendEmail(myEmailAddress, myEmailSubject, message, {htmlBody: htmlBody}) ; //send the email 
} 

function showid() { 
    Browser.msgBox("The sheet ID is:\r\n\r\n" + SpreadsheetApp.getActiveSpreadsheet().getId()); 
} 

腳本現在正常工作與原來的電子表格,但是當我嘗試用一​​種新的形式,用它而電子表格則無法發送電子郵件。填寫電子表格的表單很好,我們收到通常的電子郵件,說明電子表格已更新,但腳本電子郵件未通過。

有趣的是,當我調試它發送電子郵件的腳本時,只是在所有字段中空條目(這是我所期望的)。調試不會報告腳本本身的任何錯誤。我錯過了什麼?

+0

它失敗了,但錯誤信息是什麼?如果因爲觸發器而無法看到它,請將觸發器通知選項設置爲立即發送給您。 –

回答

0

確保觸發器設置爲「On Submit」。礦被設置爲「開放」。

0

嘗試刪除周圍 for (var c=1; c<=width; ++c) { var data = sheet.getRange(column + '1').getValue(); //this gets the "filed" names from the 1st row of "Sheet1"

報價我做一個腳本相似,我的東西了以下工作。

var rangeval = "B"+(i+1); var nameFld = filesheet.getRange(i+1,1).getValue();

+0

對不起喬,我應該在這裏發佈代碼。你所提到的這個問題已經得到糾正,因爲這是導致原始帖子的問題。以下是當前的代碼: – PapaTeve

+0

當前的代碼已添加到原始帖子 – PapaTeve

1

你有沒有授權在新表單中的腳本?由於您的腳本需要發送電子郵件,因此需要明確的授權。

+0

是的,我授權它,但你的問題讓我重新檢查觸發器。觸發器設置爲「打開」而不是「在提交」。我確定我的設置是正確的。感謝您的回答。 – PapaTeve