I am guessing this is a simple thing to do but I am not sure how to do it. I have a google script that I want to run daily that checks the values of cells within one column in google sheets individually and sends an email if the value drops below a certain threshold.
What I have so far is
function sendEmail() {
var data = SpreadsheetApp.getActiveSheet().getRange('E2').getValue();
if (data < 300){
var emailAddress = "emailaddress@email.com";
var message = "test body";
var subject = "test subject";
MailApp.sendEmail(emailAddress, subject, message);
}
}
EDIT
Here is where I am now. It works except for pulling the emails from the cells.
function sendEmail() {
function letThemKnow(num) { //This creates a function named letThemKnow which is passed the variable num from the if/else statements
var emailAddress = email[i]; //Sets the variable to the email address of
var message = "Greetings! This is an automated message to let you know your license for " + software[i] + " expires in " + num + " days.";
var subject = "Impending expiration of " + software[i];
MailApp.sendEmail(emailAddress, subject, message);
}
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh = ss.getActiveSheet();
var lastRow = sh.getLastRow();
var data = sh.getRange(2, 5, lastRow, 1).getValues();
var software = sh.getRange(2, 1, lastRow, 1).getValues();
var email = sh.getRange(2, 7, lastRow, 1).getValues();
for (i = 0; i <= email.length; i++) {
for (i = 0; i <= software.length; i++) {
for (i = 0; i <= data.length; i++) {
var num = parseInt(data[i]);
if (num == 30) {
letThemKnow(num)
} else if (num == 14) {
letThemKnow(num)
} else if (num <= 7) {
letThemKnow(num)
}
}
} //
}
} //
Your for loop is starting at '0' and ending at '0'. Try this:
function sendEmail() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh = ss.getActiveSheet();
var lastRow = sh.getLastRow();
var data = sh.getRange(2,5,lastRow,1).getValues();
for (i=0;i <= data.length;i++){
var num = parseInt(data[i]);
if (num < 300) {
var emailAddress = "email@email.com";
var message = "test body";
var subject = "test subject";
MailApp.sendEmail({
name: "Your Name",
to: emailAddress,
subject: subject,
htmlBody: message
});
}
}
}
If the email is the same every time, you should probably define those variables outside the loop.
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.