0

我有一个由时钟事件调用的触发函数。我正在使用 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);
    }
  }
}
4

2 回答 2

0

改变:

var ss = SpreadsheetApp.getActiveSpreadsheet();

到:

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

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

于 2012-08-24T06:56:42.893 回答
0

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

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

于 2012-08-24T20:13:01.940 回答