有没有一种方法可以存储当前单元格的值并将其恢复回Google表格中?

时间:2019-06-27 21:17:34

标签: google-apps-script google-sheets

我有一个脚本运行在一个单元格内,该单元格进行API调用并返回一个值。

我想通过API更新值或决定保留以前返回的原始值。有可能吗?

我曾考虑过将值缓存在相邻单元格上,作为缓存(备份)并从那里取回它,但是通过setValue()和查询单元格存储值时,事情变得相当复杂。

这是我要实现的目标的原型: enter image description here

这是代码段:

function getBittrexMarketSymbol(sourceCell) {
  // Import current value of the cell and store it here.
  var getCurrentValue; // This bit I don't know how to do.
  getCurrentValue = 0.00123; // For now, we set a dummy value.

  // Query the adjacent cell for the flag
  var range = SpreadsheetApp.getActiveRange();
  var col = range.getColumn();
  var row = range.getRow();
  var range2 = SpreadsheetApp.getActiveSheet().getRange(row,col+1);
  var flag = range2.getValue();

  if(typeof sourceCell === 'undefined'){
    // DEBUG Override
    var marketSymbol = 'None';
    Logger.log('No market symbol found: [MARKET_SYMBOL=%s]', marketSymbol)
    return lastprice = -1;
  }

  // Based on a condition we choose to return the live API value
  // or the original value that was already in the cell, prior to execution.
  if(flag===1){
    var marketSymbol = sourceCell;
    var url = "https://api.bittrex.com/v3/markets/"+ marketSymbol +"/ticker";
    var response = UrlFetchApp.fetch(url);
    var text = response.getContentText();
    var myjson = JSON.parse(text);
    var lastprice = myjson.lastTradeRate;
    return lastprice;
  }else{
    return getCurrentValue; 
  }
}

因此,基本上,我想运行API调用或选择使用粉红色背景保留该单元格上的所有现有值。

请注意,我必须更改单元格地址以强制重新评估脚本。

0 个答案:

没有答案