Home > Mobile >  G Sheet Script to send email when a value is over a certain level
G Sheet Script to send email when a value is over a certain level

Time:06-24

I am trying to get a basic script working where when a certain value is in any cell in a range, an email notification is sent:

The range is 1 column with around 40 rows, with either A or B in them, I want the notification to be sent if any of those values is 'B'.

Ideally, I'd like this to run once per day, so everyday it will check and see if a value is B and then send a notification.

What I have so far is:

function EmailUpdates() {
  var ss = SpreadsheetApp.getActive();
  var sheet = ss.getActiveSheet();
  var data = ss.getRange("Pacing!M6:M50").getValues();
  var subject = "Overpacing";
  var message = "Changes are needed";
  if(data == "B");
  {
  MailApp.sendEmail("[email protected]",subject,message);
  }
}

At the moment the email notification is working fine, however it's not properly checking the if date = b part of the code, and sending off the email regardless of whether A or B are in the cells.

Would be much appreciated if someone with more experience (which is probably 99% of people here) could take a look and spot what's going wrong.

Thanks for any help!

CodePudding user response:

  • getValues() returns a 2D array

  • Use Array.some to check if "any" of those values is 'B'.

    if (data/*looks like [[m5],[m6],..]*/.some(([m]) => m === "B")){/*Send mail*/}
    
  • Related