[英]Need help iterating down a column in google sheets and sending out an email based on each individual value in the cells
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. 我有一个要每天运行的google脚本,该脚本会分别检查google表格中一列内单元格的值,并在该值降至某个阈值以下时发送一封电子邮件。
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'. 您的for循环从“ 0”开始,到“ 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. 如果每次电子邮件都相同,则可能应该在循环外部定义这些变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.