I would like to create a Google Sheets with event triggers. I'm using Google Apps Script.
When a cell has value Ok
, a mail would be sent to warn a colleague (validation).
I found a script and it works.
Now I would like the mail body and mail subject to include cell values of the row where the trigger cell has the value OK
.
Here is an example of the sheet:
When I write "Ok"in K8, a mail is sent to a colleague. And I would like the body mail to contain information in F8, G8, and J8. Likewise, if I write "Ok" in K7, I would like the sent mail to contain info from F7, G7, and J7. (Example: "Your request has been validated. Necessity is level 5. Quantity needed is 1. Price is 17,94€.")
The script I'm using:
/**
* 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) {
if("K" == e.range.getA1Notation().charAt(0)) {
if(e.value == "ok") {
//Define Notification Details
var recipients = "user@example.com";
var subject = "Validation" ;
var body = "Achat valider, à procéder";
//Send the Email
MailApp.sendEmail(recipients, subject, body);
}
}
}
How do I get Var subject
and Var Body
to contain cell values?
Problem solved.
To summurize, when you want Email notification when cell value is changed and you want the mail to contain cells value from the same row you've edited :
Working fine for me. Thank you for the help :)
Short answer
Use one of the
Class Spreadsheet
methods to get the desired ranges and then thegetValue()
method of theClass Range
.Brief example
The following code will show a "toast" (pop-up window) displaying the value of the cell in the F column and in the same row of the cell edited.
References