2017-05-30 136 views
0

我已经使用IMPORTHTML函数将实时数据导入Google Spreadsheets。我的要求是每次单元格的值超出特定数字时,我都需要发送一封电子邮件。我发现网页下面,从Google电子表格发送电子邮件“ONLY ONCE”

{ 
    var ss = SpreadsheetApp.getActive(); 
    var sheet = ss.getSheetByName("Profit Ratio"); 

    var valueToCheck = sheet.getRange("D7").getValue(); 

    if(valueToCheck > 3) { 
     MailApp.sendEmail("zak*******@gmail.com", "Current Profit Ratio", "Current Profit Ratio is 1:" + valueToCheck+ " !!!"); 
    } 
} 

但这样做的问题是,当值超出,它让发送电子邮件的每一分钟,直到它低于3.我不希望这样的事情发生。如果电子邮件发送超过3次,我只需要发送一次电子邮件,而下一封电子邮件则必须在发送后再次超过3次。另外,我希望获得制作3<it<10。我该如何做这两件事?

回答

0

您可以使用属性服务来跟踪标准的当前满足是否已经在执行之间得到处理。

{ 
    var ss = SpreadsheetApp.getActive(); 
    var sheet = ss.getSheetByName("Profit Ratio"); 
    var valueToCheck = sheet.getRange("D7").getValue(); 
    var prop = PropertiesService.getDocumentProperties(); // Quick access to properties 


    if (valueToCheck > 3       // Between 3 and 10 
     && valueToCheck < 10 
     && !prop.getProperty("processed") { // Has not been dealt with 
    MailApp.sendEmail(
     "zak*******@gmail.com", 
     "Current Profit Ratio", 
     "Current Profit Ratio is 1:" + valueToCheck + " !!!"); 
    prop.setProperty("processed", true);  // Switch the flag so we don't send any more emails 
    } else if (valueToCheck <= 3) {    // You might want to add || value to check >= 10 here? 
    prop.setProperty("processed", false);  // Set property to false so that next time the value exceeds 3 we process it again 
    } 
}