Skip to content
sumnail

セルに値をセット後に即時反映させる

created at : 2024/05/21

GAS

flush()

SpreadsheetApp.flush()を使うことで、スプレッドシートに対する保留中の変更をすべて適用することが出来ます。

flush()なし

パフォーマンス向上のため、ループ処理完了後に一度にセットされる。 処理中にセットした値を直ぐに使いたい(pdf化や再取得など)場合は、値が反映出来ていないことがあります。

js
function withoutFlush() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getActiveSheet();
  for (let i = 1; i < 20; i++) {
    sheet.getRange(i, i).setValue("test");
  }
}

flush()あり

ループごとにセットされる。 値が即時反映されるので、セット後直ぐに使う場合に有効です。

js
function withFlush() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getActiveSheet();
  for (let i = 1; i < 20; i++) {
    sheet.getRange(i, i).setValue("test");
    SpreadsheetApp.flush(); 
  }
}