在时钟事件触发器中,forumla字段未定义

时间:2012-08-23 13:18:52

标签: google-apps-script

我有一个由时钟事件调用的触发器函数。我正在使用range.getValues()从电子表格中读取值。我没有在结果数据数组中接收公式结果值,而是看到未定义的公式。

如何让公式在此环境中执行?

这是一个例子。每小时调用此函数。它应该等到股价超过15.01美元。相反,它只是在第一个小时通过电子邮件告诉我价格是“未定义”...

/*
    run a script at a time or times you designate:

    From the Script Editor, choose Resources > Current script's triggers. You 
    see a panel with the message No triggers set up. Click here to add one now.
    Click the link that says No triggers set up. Click here to add one now.
    Under Run, select the function you want executed on schedule.
    Under Events, select Time-driven.
    On the first drop-down list that appears, select Hour timer
*/
function onHour() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var s = ss.getSheetByName("onHour");
  if(s == null) {
    s = ss.insertSheet("onHour", 1);
    s.appendRow(["EVENT","PROCESSED","EMAIL","SUBJECT","BODY"]);
  }
  //here are the column headers in the sheet onHour
  var EVENT=0; // first row (A2) has formula: =F2>15.01 ... cell F2 is: =GoogleFinance(G2; "price") ... G2 = PBI
  var PROCESSED=1; // this is empty (until later)
  var EMAIL=2; // my email
  var SUBJECT=3; //email subject
  var BODY=4;//has formula: =F2  ... email the price (however, email body = "undefined") 
  var range = s.getRange("A2:D"); //get everything above (leaving the D range open-ended works fine)
  var values = range.getValues(); 
  for(var rowIndex = 0; rowIndex < values.length; rowIndex++) {
    var data = values[rowIndex];
    if(data[EVENT] && ! data[PROCESSED]) {//The first hour, cell data[EVENT] (A2) should be FALSE, but the IF condition evaluates to True ..
      //This emails me, but it should not have because the price has not reached $15.01
      MailApp.sendEmail(data[EMAIL], data[SUBJECT], data[BODY]);
      var cell = range.getCell(rowIndex+1, PROCESSED+1);
      //set the PROCESSED column (A3) to TRUE so I will only get 1 email
      cell.setValue(true);
    }
  }
}

2 个答案:

答案 0 :(得分:0)

变化:

var ss = SpreadsheetApp.getActiveSpreadsheet();

要:

var ss = SpreadsheetApp.openById("spreadsheet id");

“添加触发器”可以处理消息 - 建议人们使用openById而不是getActiveSpreadsheet

答案 1 :(得分:0)

将s.getRange(“A2:D”)更改为s.getRange(“A2:E”)

getValue按预期返回评估后的所有公式。我的范围不够大。