当电子表格未使用时间触发器激活时,GAS importHtml无法正确更新或通过电子邮件发送

时间:2014-01-12 21:14:31

标签: triggers google-apps-script

我正在使用importHtml命令将气压数据输入电子表格。然后,如果气压高于或低于某个值,我会给它发电子邮件。当我运行所有打开的脚本时,这非常有用。但是,我已经尝试过每四个小时检查一次,但是变量似乎没有抓住正确的信息。

正确运行的电子邮件说:“这只是一个测试。循环变量结束为1,当前压力为29.73”。

当它无法正常工作的电子邮件时说:“这只是一个测试。循环变量结束为16,当前压力为#N / A”。

以下是代码:

function checkPressure() {

  // Get the data from the "Chart Data" sheet.

  var ss = SpreadsheetApp.openById("0Ah2VsNnRu1aWdEFkWGtTQklqTTd6bVY4UW8yQ1F3TWc");
  var sheet = ss.getSheetByName("Chart Data");
  var dataRange = sheet.getRange(2,1,16,4);
  var dataBp = dataRange.getValues();
  Browser.msgBox(dataBp);

  // Initialize variables/constants

  var highPressure = false;
  var lowPressure = false;
  var highBpLimit = 30.01;
  var lowBpLimit = 29.99;
  var i = 0;
  var headers = ("Date", "Time", "Date/Time", "Pressure");
  var myEmail = "MYEMAILHERE";

  while (!highPressure && !lowPressure && i < dataBp.length) {
    if (dataBp[i][3] > highBpLimit) {
      highPressure = true;
      var highBpReading = dataBp[i][3];
      var highBpDate = dataBp[i][0];
      var highBpTime = dataBp[i][1];
    }
    if (dataBp[i][3] < lowBpLimit) {
      lowPressure = true;
      var lowBpReading = dataBp[i][3];
      var lowBpDate = dataBp[i][0];
      var lowBpTime = dataBp[i][1];
    }
    i++;
  }
   if (highPressure) {
    var message = "There is a barometric pressure of " + highBpReading + " at " + highBpTime + " on the date of " + highBpDate;
    MailApp.sendEmail(myEmail, "High BP Warning", message);
  }
    if (lowPressure) {
    var message = "There is a barometric pressure of " + lowBpReading + " at " + lowBpTime + " on the date of " + lowBpDate;
    MailApp.sendEmail(myEmail, "Low BP Warning", message);
  }

  MailApp.sendEmail(myEmail,"Test BP Warning", "This is just a test. The loop variable ended as " + i + " and the current pressure is " + dataBp[0][3]);
}

0 个答案:

没有答案