0
votes

I have a sheet, and in cell B1 I have a formula that states "TRUE" if certain conditions have been met on the sheet.

I have a time based trigger set up on my script to send me an email every 6 hours if B1 states TRUE.

Here is the script I am using:

function sendEmails2() {
   var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Function2")
   var range = sheet.getRange("B1")   

   if (range = "TRUE"){
      var emailAddress = "[email protected]"  
      var message = "Please check your MCR Tracker as it has new entries that neeed reviewing: URL" 
      var subject = "Your MCR Tracker has new entries"
      MailApp.sendEmail(emailAddress,"[email protected]", subject, message)
    }
}  

When I manually run this script to test, it is sending an email, whether the condition has been met or not, rather than only if the condition has been met.

I'm very new to apps script, some help on where I have gone wrong would be appreciated!

2

2 Answers

1
votes

The problem is that in the condition of the if statement you are using an assignment operator (=) instead of a comparison operator (==) and also that you should first get the value of range to compare it to true.

Try replacing

if (range = "TRUE"){
   ...
}

with

if (range.getDisplayValue() === "TRUE"){
   ...
}
0
votes

Since you're using a formula to display "TRUE" you should add .getDisplayValue() when you set the initial value of your range variable. Like this:

Replace var range = sheet.getRange("B1")

With var range = sheet.getRange("B1").getDisplayValue();