增加Google表格缓存服务中保存的值

时间:2016-10-19 00:47:03

标签: google-sheets-api

我正在尝试使用谷歌缓存服务存储一个值,但它似乎没有像我想要的那样增加。这是代码 -

  //Starts a new instance of cache
  var cache = CacheService.getScriptCache();
  //Puts the value 2 into foo key of Cache
  cache.put('foo', 2);
  //Grabs that value 
  var startMessageRow = cache.get('foo');
  //stuff for an email I'm sending using this cached value
  var messageDataRange = sheet.getRange(startMessageRow, 2)
  var message = messageDataRange.getValues();
  //Here's where I'm trying to increment it, but the value is staying at 2
  cache.put('foo','startMessageRow'+1);

最后一行代码是我每次运行此脚本时尝试将值递增1的位置,但无论我尝试什么,该值都只会停留在两位。

1 个答案:

答案 0 :(得分:0)

我找到了一种更简单的方法。我只是在一个随机单元格中放置一个计数器,然后我每次运行脚本时只添加一个值。比使用缓存服务更容易!

  var cellRange = s1.getRange("W726"); //set this string to be the corresponding cell for daily counter
  var cell = cellRange.getValues(); //grabs the value from that cell
  var startMessageRow = cell; //sets StartMessage Row to be equal to the value of counter
  startMessageRow = parseInt(startMessageRow);//ints it

  cell = parseInt(cell); //ints it
  cell+=1; //increments
  cell = cell+""; // strings it
  cellRange.setValue(cell); // puts the new value in