Trigger email alert based on last row's condit

2019-02-20 11:12发布

问题:

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:

IDEAL STATE

  1. Grab the last filled row (today's date) in the Google Sheets called "test".
  2. Check in that row if the value in column F is greater than 0.5.
  3. If it greater than 0.5, then trigger an email.
  4. In email body, it should state "Results found on [date]."

This was my starting point but it does not produce what I want. These are the issues:

CURRENT STATE

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.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"));
};

Last Row in this context is Row 217, not 218, assuming sheet.getLastRow() would ignore #DIV/o! values. See screenshot for this.

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.

回答1:

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);
    }
};