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. the same, if I write ok
on cell b2
, I would like to send an email with keyword
(cell 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)
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”的所有信息。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.