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