![](/img/trans.png)
[英]How do I get the value in a Google Sheets cell and test its contents against a range of cells?
[英]How to get the values from a range of cells in Google Sheets and compare the same with a predefined value to send mail if the condition mets
我正在设置gooleappscript以在某些条件满足电子表格中o值的特定条件时自动发送邮件。我需要读取多个单元格(A1至A10)的值,并检查其值是否小于3,如果是,则发送邮件至提及的邮件ID。 我已经完成了脚本,但是它的唯一读取值来自第一个单元格,并相应地发送邮件。 我的目的是读取每个选定的单元格值,然后进行比较。 任何纠正它的想法。
下面是正在使用的代码。
function sendMail() { if (SpreadsheetApp.openById('1bmAFWR7YsfMlgxExLKY4**********').getRange("A2:A10").getValue()<3) ; MailApp.sendEmail("s*****@gmail.com", "*Subject*******", "Dear Sreenath, Your **** is getting expired in two days !"); }
我已经尝试过getRange("A2:A10").getValue()<3
但是它仅读取和比较A2
的值。在这种情况下,仅读取和比较了值10。我需要从A2
到A10
读取所有值并检查其是否小于3。如果其中任何一个小于3,则需要触发send mail命令。 任何帮助,高度赞赏。
您需要修复一些问题:
getValue()
仅获取一个值。 您需要范围内的所有值。 因此,请使用getValues()
。 参考文档在这里 。
您收到的值是一个数组。 您将需要遍历数组并检查每个值。
注意这个
“返回二维值数组,按行,然后按列索引”
您可能还需要指定从哪个工作表中获取值。
所以我会尝试这样的事情:
function sendMail() {
var ss = SpreadsheetApp.openById('1bmAFWR7YsfMlgxExLKY4**********'); // The spreadsheet
var sheet = ss.getSheetByName("The_Name_Of_Your_Sheet_Goes_Here"); // The sheet that has the data
var data = sheet.getRange("A2:A10").getValues(); // The values from the sheet
for (var i = 0; i < data.length; i++) { // Loop throught the values
if ( data[i][0] < 3 ) { // The array is a 2-d array. See ref doc in this post
MailApp.sendEmail("s*****@gmail.com", "*Subject*******", "Dear Sreenath, Your **** is getting expired in two days !"); // Send email
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.