2017-06-01 44 views
0

我目前有一個可正常工作的Google腳本,可讀取Google表格中儀表板上的值以進行遊泳池化學監控。人們會提交表格,如果這些數字大於或小於設定的數字,它會發送一封包含特定信息的電子郵件。更清潔的方式從表格發送我的電子郵件

這工作正常,但如果有多個數字是關閉的,我會收到多個電子郵件討厭。

我正在尋找一個簡單,乾淨的輸出,而不是多個電子郵件。

這是我的代碼的一部分(其餘是相同的只是不同的池)。

 function sendEmails() { 
     var sheet = SpreadsheetApp.getActiveSheet(); 

    //----vvvvvPool 
     var IDPpHReading = sheet.getRange('A6:A7').getValue(); 
     var IDPpHTest = sheet.getRange('B6:B7').getValue(); 
     var IDPORP = sheet.getRange('A9:A10').getValue(); 
     var IDPSetPoint = sheet.getRange('B9:B10').getValue(); 

     var IDPCL = sheet.getRange('A12:A13').getValue(); 
     var IDP_Filter1_INF = sheet.getRange('A16:A17').getValue(); 
     var IDP_Filter1_EFF = sheet.getRange('B16:B17').getValue(); 
     var IDP_Filter2_INF = sheet.getRange('A20:A21').getValue(); 
     var IDP_Filter2_EFF = sheet.getRange('B20:B21').getValue(); 

// insert email here 
    var emailAddress = "[email protected]"; 
//-------------------- IDP pH 
    if (IDPpHReading > 7.8) { 
     var subject = "High pH Reading IDP"; 
     var message = " An Aquatic Specialist recently logged a chemical reading that was too high for the Indoor Pool. Please check the situation asap."; 

     MailApp.sendEmail(emailAddress, subject, message); 
    } 
    else if (IDPpHReading < 7.2) { 
     var subject = "Low pH Reading IDP"; 
     var message = " An Aquatics Specialist recently logged a chemical reading that was too low for the Indoor Pool. Please check the situation asap."; 

     MailApp.sendEmail(emailAddress, subject, message); 
    } 
    if (IDPpHTest > 7.8) { 
     var subject = "High pH Test IDP"; 
     var message = " An Aquatics Specialist recently logged a chemical reading that was too high for the Indoor Pool. Please check the situation asap."; 

     MailApp.sendEmail(emailAddress, subject, message); 
    } 
    else if (IDPpHTest < 7.2) { 
     var subject = "Low pH Test IDP"; 
     var message = " An Aquatics Specialist recently logged a chemical reading that was too low for the Indoor Pool. Please check the situation asap."; 

     MailApp.sendEmail(emailAddress, subject, message); 
    } 

}; 

我給這家去將是使每一個獨特的消息:

var message 1; 
var message 2; 
etc 

,然後讓電子郵件 - MailApp.SendEmail(EMAILADDRESS,主題,MESSAGE1 +消息2 ....);

但我覺得這是一個馬虎的做法。

+0

只需設置'主題=「」'和'消息=」 ''在開始處,然後在特定的讀數適用時連接額外的部分到主體/主體。 – halfer

回答

0

我不得不這樣做了幾次,我通常做的是使用字符串連接建立的消息,然後將其發送:

var subject = "Abnormal pH Reading IDP";  
    var message = 'An Aquatics Specialist recently logged a chemical reading at the indoor pool and found:\n'; 
    var needsCheck = false; 

    if (IDPpHReading > 7.8) { 

      var message = message + "\nA reading that was too high."; 
      var needsCheck = true; 

     } 
     else if (IDPpHReading < 7.2) { 
      var message = message + "\nA reading that was too low."; 
      var needsCheck = true; 

     } 
     if (IDPpHTest > 7.8) { 
      var message = message + "\nA test that was too high."; 
      var needsCheck = true; 

     } 
     else if (IDPpHTest < 7.2) { 
      var message = message + "\nA test that was too low."; 
      var needsCheck = true; 
     } 

    if (needsCheck){ 
    var message = message + "\n\nPlease check the situation asap."; 
    MailApp.sendEmail(emailAddress, subject, message); 
    }