Google scripting: return value instead of formula

陌路散爱 提交于 2019-12-12 02:14:05

问题


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


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/39999293/google-scripting-return-value-instead-of-formula

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!