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);
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())  ...
  • oh, so basically it was referencing itself as if the value is B2, send email? – Tyler Durden Jul 25 '15 at 5:44
  • Yes. Or "greater than", which is relevant only if the value at A2 happened to be a string, which would be compared to "B2" in alphabetical order. An inequality between a number and a string that can't be coerced into a number is always false in JS, afaik. – user79865 Jul 25 '15 at 5:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.