跳到主要内容

LINEST

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

Syntax

expression.LINEST(arg1, arg2, arg3, arg4);

expression - A variable that represents a ApiWorksheetFunction class.

Parameters

NameRequired/OptionalData typeDefaultDescription
arg1RequiredApiRange | ApiNameThe set of y-values from the <em>y = mx + b</em> equation.
arg2OptionalApiRange | ApiNameAn optional set of x-values from the <em>y = mx + b</em> equation.
arg3OptionalApiRange | ApiName | booleanA logical value: the constant <em>b</em> is calculated normally if this parameter is set to -true or omitted,\ and <em>b</em> is set equal to 0 if the parameter is -false.
arg4OptionalApiRange | ApiName | booleanA logical value: return additional regression statistics if this parameter is set to -true,\ and return m-coefficients and the constant <em>b</em> if the parameter is -false or omitted.

Returns

number

Example

const worksheet = Api.GetActiveSheet();

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

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

//get x and y ranges
let yRange = worksheet.GetRange("A1:A3");
let xRange = worksheet.GetRange("B1:B3");
let func = Api.GetWorksheetFunction();

//invoke LINEST method
let ans = func.LINEST(yRange, xRange, constant, stats);

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