如何从 Google 工作表中的一系列单元格中获取值并将其与预定义值进行比较以在满足条件时发送邮件

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 以在 spreadsheet.I 中的值范围满足某些条件时自动发送邮件需要读取多个单元格(A1 到 A10)的值并检查其是否小于 3,如果是这样,请向提到的邮件 ID 发送邮件。我已经完成了脚本,但它的唯一读取值来自第一个单元格并相应地发送邮件。我的目的是读取每个选定的单元格值,然后进行比较。任何纠正它的想法。

Google sheet

下面是我正在使用的代码。

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 only.ie 的值在这种情况下只读取值 10 而 compared.i 需要读取所有值从A2A10,检查是否小于3,如果有一个小于3,则需要触发发送邮件命令。 非常感谢任何帮助。

您需要解决一些问题:

getValue() 只得到一个值。您需要范围内的所有值。所以使用getValues()Ref doc here.

您收到的值是一个数组。您需要遍历数组并检查每个值。

注意这个

"Returns a two-dimensional array of values, indexed by row, then by column"

您可能还想指定从哪个 sheet 获取值。

所以我会尝试这样的事情:

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

    }

  }

}