根据最后一行的条件触发电子邮件警报 [英] Trigger email alert based on last row's condition NOT WORKING
问题描述
我尝试构建脚本,但是存在一些问题.表格格式是2列,分别是日期和值.这些是需要的:
I attempted to build a script but there are some issues. The table format are 2 columns which are date and values. These are the needs:
理想状态
- 获取Google表格中名为测试"的最后一个填充行(今天的日期).
- 在该行中检查F列中的值是否大于0.5.
- 如果该值大于0.5,则触发一封电子邮件.
- 在电子邮件正文中,应注明在[日期]找到的结果."
这是我的出发点,但没有产生我想要的东西.这些是问题:
This was my starting point but it does not produce what I want. These are the issues:
当前状态
1.脚本捕获过去F列大于0.5的每一行.我只想检查今天(那将是最后一行).它不应该浏览过去的所有内容.
1.The script grabs every row in which column F was greater than 0.5 in the past. I only want to check for today (which would be the last row). It should not look through everything in the past.
2.电子邮件正文指出:在[行号]上找到结果".这没有任何意义.我希望显示日期,而不是行号.
2.The email body states: Result found on [row number]". This makes no sense. I want the date to show, not the row number.
这是当前代码.请帮忙.
This is the current code. Please help.
function readCell() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("test");
var values = sheet.getRange("F3:F").getValues(); //this has the values
var date = sheet.getRange("D3:D").getValues(); // this has the date
var results = [];
for(var i=0;i<values.length;i++){
if(values[i]>=0.5)
{
results.push("Result found on:" +(i+3));
}
}
MailApp.sendEmail('blabla@gmail.com', 'Alert', results.join("\n"));
};
在这种情况下,最后一行是217行,而不是218行,假设sheet.getLastRow()会忽略#DIV/o!价值观.参见截图.
Last Row in this context is Row 217, not 218, assuming sheet.getLastRow() would ignore #DIV/o! values. See screenshot for this.
最新更新
当前的错误与"toDateString"相关.我认为我的Google表格可能落后一天.因此,今天是1月10日,我的Google表格中的最后一行是1月9日.我认为这就是错误发生的原因.你确定吗?在这种情况下,如何将其更改为今天1天?
LATEST UPDATE
The current Error is related "toDateString". I think it may be related that my Google Sheet is one day behind. So, it today is Jan 10, the last row in my Google Sheet is Jan 9th. I think that is why the error happens. Can you confirm? In that case, how do I change it to today-1 day?
请参阅下文.
See below.
推荐答案
以下是检查最后一行的方法:
Here's how you can check the last row:
function readCell() {
var sheet = SpreadsheetApp.getActive().getSheetByName('test');
var lastRow = sheet.getLastRow();
var value = sheet.getRange('F' + lastRow).getValue();
var date = sheet.getRange('D' + lastRow).getValue();
if (value >= 0.5) {
var result = 'Result found on: ' + date;
MailApp.sendEmail('blabla@gmail.com', 'Alert', result);
}
};
看到您的数据后,我认为下面的代码会更适合您.
After seeing your data, I think the code below would suit you better.
function readCell() {
var sheet = SpreadsheetApp.getActive().getSheetByName('test');
var dates = sheet.getRange('D1:D').getValues();
var date = null;
var dateRow = 0;
var dateCount = dates.length;
var yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);
var yesterdayString = yesterday.toDateString();
for (dateRow; dateRow < dateCount; ++dateCount) {
date = dates[dateRow];
if (date instanceof Date) {
date = date.toDateString();
if (date === yesterdayString) {
++dateRow;
// To account for zero-based array
break;
}
}
}
var value = sheet.getRange('F' + dateRow).getValue();
if (value >= 0.5) {
var result = 'Result found on: ' + date;
MailApp.sendEmail('blabla@gmail.com', 'Alert', result);
}
};
这篇关于根据最后一行的条件触发电子邮件警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!