Using google script I need to create a function that will send email notification when a newly added cell along Column D is less than 8. However, when I tested to input a value less than or even more than 8, I haven't received an email.
function sendEmailReport() {
var ss_link = 'Google Spreadsheet URL';
var ss = SpreadsheetApp.openByUrl('Google Spreadsheet URL').getSheetByName("Sheet Name").getRange("D:D"); //get column D in a specified sheet
var row = ss.getLastRow(); //get the last row on the column D. The last row contains newly added row.
var value = ss.getValue(); // then get the value of the cell at the last row along column D.
//if the value in the newly added row is less than 8. I want to send an email
if (value < 8) {
MailApp.sendEmail({
to: "Email Address",
subject: "Sample Subject",
body: "Sample Body" + ss_link
name: 'Department ABC'
});
}
}
Here is the fixed code:
function sendEmailReport() {
var ss_link = 'Google Spreadsheet URL';
var ss = SpreadsheetApp.openByUrl('Google Spreadsheet URL').getSheetByName('Sheet Name');
var row = ss.getLastRow();
var value = ss.getRange('D'+row).getValue();
//if the value in the newly added row is less than 8. I want to send an email
if (value < 8) {
MailApp.sendEmail({
to: 'Email Address',
subject: 'Sample Subject',
body: 'Sample Body' + ss_link
name: 'Department ABC'
});
}
}
This is a 'static' code: you have to run it manually every time you want to send the email. If you need to run it automatically it will need a more complicated solution with installable trigger onEdit()
, etc.
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.