Dave Lalande
Dave Lalande

Reputation: 194

Setting Script Property using it in "if" in Google

I have a script that is supposed to mail a chart URL when a metric falls out of X range and Only if someone has entered a new metric. It's mailing everytime I run the script so I know the setProperty is not working and the "if" statement isn't working because it's only supposed to mail if X is TRUE.

The sheet... https://docs.google.com/spreadsheet/ccc?key=0Ai_2YLvaQba0dHN1dWFpY0ZSbGpudWF4cTJYNlFwNHc&usp=sharing

My code...

function myAverages() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var value = ss.getSheetByName("EmailServices").getRange("P2").getValue().toString();
  var outside = ss.getSheetByName("EmailServices").getRange("S2").getValue().toString;
  var mailed = ss.getSheetByName("EmailServices").getRange("T2").getValue().toString();
  var stemiAverage = ss.getSheetByName("EmailServices").getRange("R2").getValue().toString();
  var stemiChart = ss.getSheetByName("EmailServices").getRange("U2").getValue().toString();
  var last = ScriptProperties.getProperty('last');

  //if "value" is not equal to ScriptProperty "last" AND "outside" is TRUE, then mail   
  if(value =! last, outside = "TRUE")

  {
   MailApp.sendEmail("[email protected]", "Metric Warning",
    "Yearly STEMI Average has fallen below 65%, it is now: " + stemiAverage + "%" +
    "\n\nTo see the current trending chart " + stemiChart + "\n\n The sheet that    calculated this is here " + ss.getUrl());
   ScriptProperties.setProperty('last','value');
  }
}

Thanks for any help you can lend. I'm teaching myself this stuff and feel very much like a newby. I've tried a hundred or more combinations to the script before posting here.

Upvotes: 0

Views: 4066

Answers (2)

Serge insas
Serge insas

Reputation: 46794

As a complement to Phil's answer and following your comment, there is another error in your script :

   ScriptProperties.setProperty("last","value");

should be

   ScriptProperties.setProperty({'last':value},true);

The true parameter is optional, see documentation on ScriptProperties, it gives you the possibility to delete all other keys.

So you're complete function with corrections would be :

function myAverages(){
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var value = ss.getSheetByName("EmailServices").getRange("P2").getValue().toString();
  Logger.log('value = '+value)    
      var outside = ss.getSheetByName("EmailServices").getRange("S2").getValue().toString();
  Logger.log('outside='+outside)
      var mailed = ss.getSheetByName("EmailServices").getRange("T2").getValue().toString();
  Logger.log('mailed = '+mailed)    
      var stemiAverage = ss.getSheetByName("EmailServices").getRange("R2").getValue().toString();
      var stemiChart = ss.getSheetByName("EmailServices").getRange("U2").getValue().toString();
      var last = ScriptProperties.getProperty('last');
  Logger.log('last='+last)    
       if(value != last && outside == "true"){
          MailApp.sendEmail("[email protected]", "Metric Warning",
          "Yearly STEMI Average has fallen below 65%, it is now: " + stemiAverage + "%" +
          "\n\nTo see the current trending chart " + stemiChart + "\n\n The sheet that    calculated this is here " + ss.getUrl());
          ScriptProperties.setProperty({'last':value});
  Logger.log('mail sent')
         }
    }

Upvotes: 3

Phil Bozak
Phil Bozak

Reputation: 2822

You need to use the AND operator: &&.

And also, checking for equality requires 2 equals signs: ==.

So be sure you try this:

if (value!=last && outside=="TRUE") {

Upvotes: 0

Related Questions