5
6

More than 5 years have passed since last update.

Google Apps Script でスプレッドシートの値を強制的に再計算させる方法

Posted at

Google Apps Script から Google スプレッドシートの値を取得するだけでは値の再計算はされません。

TODAY() 関数や NOW() 関数を使用していてスクリプトを実行したときの時刻で再計算させたい場合、以下のようにします。

対処法

値の取得前にそのシート内のいずれかのセルの値を変更すると、値が再計算されます。

var SS = SpreadsheetApp.openById('SPREADSHEET_ID_HERE').getSheetByName('SHEET_NAME_HERE');
SS.getRange('A1').setValue('foo');  // A1セルに'foo'と入力
SpreadsheetApp.flush();  // 変更内容を即反映

ここではA1セルとしていますが、変更しても問題のないセルに置き換えてください。

参考

Applies all pending Spreadsheet changes.

Spreadsheet operations are sometimes bundled together to improve performance, such as when doing multiple calls to Range.getValue(). However, sometimes you may want to make sure that all pending changes are made right away, for instance to show users data as a script is executing.

5
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
6