简体   繁体   English

使用谷歌表和脚本发送电子邮件

[英]use google sheets and script to send email

I am trying, with a script, to send an email once a cell is edited.我正在尝试使用脚本在编辑单元格后发送电子邮件。 Assuming I have the following sheet, I would like to send an email containing keyword2 (cell a3) only when ok is written on cell b3.假设我有以下工作表,我想仅在单元格 b3 上写ok时发送包含关键字 2(单元格 a3)的电子邮件。 the same, if I write ok on cell b2 , I would like to send an email with keyword (cell a2)同样的,如果我写ok细胞b2 ,我想和发送电子邮件keyword (单元格A2)

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

I used this script and was able to send an email, but it takes both rows, I only want the script to take the row where the ok is adiacent (not all rows)我使用了这个脚本并且能够发送一封电子邮件,但它需要两行,我只希望脚本获取ok是 adiacent 的行(不是所有行)

can you please help me out and address me on how I can solve this?你能帮我解决这个问题吗?

CODE SNIPPET代码片段

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

从您的代码看来,您只是在检查单词“Ok”,但您还没有创建一个变量来存储任何包含“ok”的单元格,然后显示该变量中包含“ok”的所有信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM