03 GAS Properties Service グローバル変数のように利用(永続化)

GASのグローバル変数は、参照はするけど変更を保存しておくことができない。
このようにすると、永続化してカウントし続ける事ができます。

var scriptProperties = PropertiesService.getScriptProperties();  
var count = scriptProperties.getProperty('count');

function loadCount(){
  var count = scriptProperties.getProperty("count");
  if (!count) return 0;
  count = Number(count) + 1
  return count;
}

function saveCount(count) {
  scriptProperties.setProperty("count", count);
}

function main(){
  var count = loadCount();
  Logger.log('count ' + count);
  saveCount(count);
}