0

我有一个脚本,当指标超出 X 范围并且仅当有人输入新指标时,它应该发送图表 URL。每次我运行脚本时它都会发送邮件,所以我知道 setProperty 不起作用并且“if”语句不起作用,因为它只应该在 X 为 TRUE 时发送邮件。

工作表... https://docs.google.com/spreadsheet/ccc?key=0Ai_2YLvaQba0dHN1dWFpY0ZSbGpudWF4cTJYNlFwNHc&usp=sharing

我的代码...

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("dave@mydomain.com", "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');
  }
}

感谢您提供的任何帮助。我正在自学这些东西,感觉非常像新手。在发布到这里之前,我已经尝试了一百种或更多的脚本组合。

4

2 回答 2

3

作为对菲尔的回答和您的评论的补充,您的脚本中还有另一个错误:

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

应该

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

true参数是可选的,请参阅ScriptProperties 上的文档,它使您可以删除所有其他键。

因此,您的完整功能与更正将是:

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("me@mydomain.com", "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')
         }
    }
于 2013-02-10T22:26:01.737 回答
0

您需要使用 AND 运算符:&&

此外,检查相等性需要 2 个等号:==.

所以一定要试试这个:

if (value!=last && outside=="TRUE") {
于 2013-02-10T21:29:36.340 回答