Google scripting: return value instead of formula

2019-09-10 22:27发布

I made a script:

function getWN8() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[0];
  var startRow = 11;
  var queryString = Math.random();
  var dataRange = sheet.getRange(startRow, 3)
  var data = dataRange.getValues();
  var values = SpreadsheetApp.getActiveSheet().getDataRange().getValues()
  var input, cellFunction, range, row;


  for (var n=startRow-1; n < values.length;++n){
    input = values[n][0];
    row = values[n];
    cellFunction = '=INDEX(IMPORTHTML("http://en.wot-life.comeu/player/' + input + '", "table", 1),16,2)';

    for (var i = 0; i < row.length; ++i) {
        range = sheet.getRange((n+1), 2);
        range.setValue(cellFunction);
    }
  }
}

This script is reading values from first column, insert value from first column to http link and output is pushed into cells in 2nd column. But If I look in sheet, I have displayed numeric value in cell, but it is threated as an formula. Therefore I can't set conditional formatting.

So all I need to do, is to push value to the cell instead of formula. How can it be done ? I'm not very skilled in scripting, or programming.

Thanks for any help

Ivo

2条回答
Anthone
2楼-- · 2019-09-10 23:00

You might want to use setFormula(formula) or setFormulaR1C1(formula).

setFormula(formula)

  • Updates the formula for this range. The given formula must be in A1 notation.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

var cell = sheet.getRange("B5");
cell.setFormula("=SUM(B3:B4)");

setFormulaR1C1(formula)

  • Updates the formula for this range. The given formula must be in R1C1 notation.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

var cell = sheet.getRange("B5");
// This sets the formula to be the sum of the 3 rows above B5
cell.setFormulaR1C1("=SUM(R[-3]C[0]:R[-1]C[0])");

Hope this helps.

查看更多
够拽才男人
3楼-- · 2019-09-10 23:11

To turn the formula into a value you will have to add something to your last iteration like:

var val = range.getValue;
range.setValue(val);

Unfortunately, I haven't found a way calculate and put in the value directly by using a spreadsheet formula. To put in the value directly, it would require a code that is executing exactly the same math as the formula.

查看更多
登录 后发表回答