1

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="[email protected]"; var subject=" test subject " +value; var body=" test body "+value; MailApp.sendEmail(recipient, subject, body); }; 

1 Answer 1

1
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()) ... 
2
  • oh, so basically it was referencing itself as if the value is B2, send email? Commented Jul 25, 2015 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. Commented Jul 25, 2015 at 5:49

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.