Google Sheets – Send Email If One Cell is Greater Than or Equal to Another

google sheetsgoogle-apps-script

For some reason this script is not working and I was wondering if someone may guide me on fixing this?

In my Google Spreadsheets, I just want the value in cell A2 to send an email if it's greater than or equal to the value in B2. I have gotten it to work if it's a specific number or word but not sure if it what I'm doing wrong here to compare cells.

function ifstatement() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("data");
  var value = sheet.getRange("A2").getValue();
  if(value >= "B2") sendEmail(value)



};

function sendEmail(value){
  var recipient="fakeemail@gmail.com";
  var subject=" test subject " +value;
  var body=" test body "+value;
  MailApp.sendEmail(recipient, subject, body);
};

Best Answer

if (value >= "B2") ...

That's just comparing value to the string "B2". You wanted to compare to the content of the cell B2. That is,

if (value >= sheet.getRange("B2").getValue())  ...