クロックイベントによって呼び出されるトリガー関数があります。range.getValues() を使用して、スプレッドシートから値を読み取っています。結果のデータ配列で数式の結果値を受け取る代わりに、数式がある場所に undefined が表示されます。
この環境で数式を実行するにはどうすればよいですか?
ここに例があります。この関数は 1 時間ごとに呼び出されます。株価が 15.01 ドルを超えるまで待つ必要があります。代わりに、最初の 1 時間に、価格が「未定」であることを知らせるメールが送信されます...
/*
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);
}
}
}