更清洁的方式从Sheets发送我的电子邮件

时间:2017-06-01 01:12:06

标签: google-apps-script google-sheets

我目前有一个可用的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 = "pseudoMcFakeEmail@Battlecruiser.com";  
//-------------------- 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,subject,message1 + message2 ....);

但我觉得这是一种草率的方式。

1 个答案:

答案 0 :(得分: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);
  }