TRY NEW VERSION

LINEST

LINEST(arg1, arg2, arg3, arg4) → { number }

Returns statistics that describe a linear trend matching known data points, by fitting a straight line using the least squares method.

Parameters:

Name Type Description
arg1 ApiRange

The set of y-values from the y = mx + b equation.

arg2 ApiRange

An optional set of x-values from the y = mx + b equation.

arg3 boolean

A logical value: the constant b is calculated normally if this parameter is set to true or omitted, and b is set equal to 0 if the parameter is false.

arg4 boolean

A logical value: return additional regression statistics if this parameter is set to true, and return m-coefficients and the constant b if the parameter is false or omitted.

Returns:

Type
number

Example

Copy code
builder.CreateFile("xlsx");

const oWorksheet = Api.GetActiveSheet();

//configure function parameters
var yValues = [1.5, 2, 3];
var xValues = [2, 3.1, 3.5];
var constant = true;
var stats = true;

//set values in cells
for (var i = 0; i < yValues.length; i++) {
  oWorksheet.GetRange("A" + (i + 1)).SetValue(yValues[i]);
}
for (var n = 0; n < xValues.length; n++) {
  oWorksheet.GetRange("B" + (n + 1)).SetValue(xValues[n]);
}

//get x and y ranges
var yRange = oWorksheet.GetRange("A1:A3");
var xRange = oWorksheet.GetRange("B1:B3");
var oFunction = Api.GetWorksheetFunction();

//invoke LINEST method
var ans = oFunction.LINEST(yRange, xRange, constant, stats);
console.log(ans)

//print answer 
 oWorksheet.GetRange("D1").SetValue(ans);


builder.SaveFile("xlsx", "LINEST.xlsx");
builder.CloseFile();

Resulting document