使用Google表格和脚本发送电子邮件

时间:2018-07-13 10:52:24

标签: javascript excel google-sheets google-drive-api

我正在尝试使用脚本在单元格被编辑后发送电子邮件。假设我有以下内容,仅在单元格b3上写有ok时,我才想发送包含关键字2(单元格a3)的电子邮件。同样,如果我在单元格ok上写着b2,我想用keyword(单元格a2)发送电子邮件

| COL A      | COL B  |
| keyword    |        |
| keyword2   |  ok    |

我使用了此脚本并能够发送电子邮件,但是它占用了两行,我只希望脚本占用ok中间的行(并非所有行)

您能帮我解决我的问题吗?

代码片段

/**
 * add trigger for onedit - 
 * see menu -> Resouces -> Current project's triggers
 */
function Initialize() {

    var triggers = ScriptApp.getProjectTriggers();

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

    ScriptApp.newTrigger("sendNotification")
        .forSpreadsheet(SpreadsheetApp.getActiveSpreadsheet())
        .onEdit()
        .create();

};


/**
 * 
 */


function sendNotification(e) {

    var sheet = SpreadsheetApp.getActiveSheet();
    var startRow = 2; // First row of data to process
    var numRows = 2; // Number of rows to process
    // Fetch the range of cells A2:B3
    var dataRange = sheet.getRange(startRow, 1, numRows, 10)
    // Fetch values for each row in the Range.
    var data = dataRange.getValues();
    for (var i = 0; i < data.length; ++i) {
        var row = data[i];
        var keyword = row[0]; // Colonna A
        var URL = row[3]; // Colonna D

        if ("B2" == e.range.getA1Notation() || "B3" == e.range.getA1Notation()) {
            if (e.value == "ok") {



                //Define Notification Details
                var recipients = "email@example.com";
                var subject = "Update" + e.range.getSheet().getName();
                var body = "cell " + keyword;

                //Send the Email
                MailApp.sendEmail(recipients, subject, body);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

从您的代码中看来,您似乎只检查了单词“ Ok”,但尚未创建变量来存储任何包含“ ok”的单元格,然后显示该变量的所有信息(如果包含“ ok”) '。