How to refresh a sheet's cell value in Google Apps Script

后端 未结 5 1287
囚心锁ツ
囚心锁ツ 2020-12-16 15:02

I\'m experimenting with Blockspring which provides a Google Sheets add-on which can, for example, run a function which returns data from a webservice e.g.

=B         


        
5条回答
  •  不知归路
    2020-12-16 15:26

    Example based on accepted answer above. Adding since I had to struggle a little to interpret how to use this to auto eval a cell formula on any sheet change. This code runs slow, but I'm sharing it as a general example in hopes it helps, YMMV.

    /* Force evaluation of custom formula on sheet change.
    * Temporarily changes formula text.
    * @param sheetName: String Needs to be present in the formula
    * @param row: Int
    * @param col: Int 
    */
    
    function forceEval(sheetName, row, col){
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var sheet = ss.getSheetByName(sheetName);
      var orig = sheet.getRange(row,col).getFormula(); 
      var temp = orig.replace("=", "?");
      sheet.getRange(row,col).setFormula(temp); 
      SpreadsheetApp.flush();
      sheet.getRange(row,col).setFormula(orig); 
    }
    

    Now call it using the onEdit trigger

    function onEdit(e){
        forceEval("MySheet", 1, 1)
    }
    

    This will temporarily re-write the formula in cell 1,1 replacing "=" with "?", then flush(), then change it back, forcing the evaluation after any edit in the sheet.

    Custom formula in cell might look something like this: =SUMSTATUS("MySheet","InProgress")

    Should work for: =BLOCKSPRING("get-stock-current-stats", "ticker", "MSFT")

提交回复
热议问题