电子邮件Google表单结果不起作用

时间:2012-05-17 20:36:28

标签: google-apps-script

每次提交表单时,我都会使用脚本将表单结果通过电子邮件发送给我。整个代码在此处发布到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());
}

脚本现在可以正常使用原始电子表格,但是当我尝试将它与新表单和电子表格一起使用时,它无法发送电子邮件。表格填写电子表格就好了,我们收到通常的电子邮件,说明电子表格已经更新,但脚本电子邮件没有通过。

有趣的是,当我调试脚本时,它会发送电子邮件,只是所有字段中的空条目(这是我所期望的)。 Debug不报告脚本本身的任何错误。我错过了什么?

3 个答案:

答案 0 :(得分:1)

您是否以新表格授权了脚本?由于您的脚本需要发送电子邮件,因此需要明确授权。

答案 1 :(得分: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();

答案 2 :(得分:0)

确保将触发器设置为“提交时”。我被设置为“On Open”。