javascript - How to get the values from a range of cells in Google Sheets and compare the same with a predefined value to send m

I was setting up an gooleappscript to automatically send mail if certain condition meets for a range o

I was setting up an gooleappscript to automatically send mail if certain condition meets for a range o value in spreadsheet.I need to read values of multiple cells (A1 to A10) and check whether its less than 3 , if so then send a mail to mentioned mail id. I have done the script but its only reading value form the first cell and sends mail accordingly. My purpose is to read each selected cell values and then pare it . Any idea to correct it.

Google sheet

Below is the code am using.

function sendMail() {
  if (SpreadsheetApp.openById('1bmAFWR7YsfMlgxExLKY4**********').getRange("A2:A10").getValue()<3) ;
  MailApp.sendEmail("s*****@gmail", "*Subject*******", "Dear Sreenath, Your **** is getting expired in two days !");
	}

I was setting up an gooleappscript to automatically send mail if certain condition meets for a range o value in spreadsheet.I need to read values of multiple cells (A1 to A10) and check whether its less than 3 , if so then send a mail to mentioned mail id. I have done the script but its only reading value form the first cell and sends mail accordingly. My purpose is to read each selected cell values and then pare it . Any idea to correct it.

Google sheet

Below is the code am using.

function sendMail() {
  if (SpreadsheetApp.openById('1bmAFWR7YsfMlgxExLKY4**********').getRange("A2:A10").getValue()<3) ;
  MailApp.sendEmail("s*****@gmail.", "*Subject*******", "Dear Sreenath, Your **** is getting expired in two days !");
	}

i have tried getRange("A2:A10").getValue()<3 but its reading and paring the value from A2 only.ie in this case only value 10 is read and pared.i need to read all values from A2 to A10 and check whether its less than 3. and if any one of it is less than 3 then need to trigger the send mail mand. Any help highly appreciated.

Share Improve this question edited Aug 28, 2019 at 8:42 user2342558 6,6476 gold badges41 silver badges64 bronze badges asked Aug 28, 2019 at 8:38 SreenathSreenath 131 silver badge5 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

You'll need to fix a few things:

getValue() gets just one value. You want all the values in the range. So use getValues(). Ref doc here.

The values you receive are an array. You'll need to loop through the array and check each value.

Note that this

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

You may also want to specify which sheet to get the values from.

So I would try something like this:

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.", "*Subject*******", "Dear Sreenath, Your **** is getting expired in two days !"); // Send email

    }

  }

}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745489861a4629939.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信