Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions src/functions/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,24 @@ export function tryCastAsNumberMatrix(data: Matrix<any>, argName: string): Matri
data.forEach((row) => {
row.forEach((cell) => {
if (typeof cell !== "number") {
throw new Error(
_t(
"Function [[FUNCTION_NAME]] expects number values for %s, but got a %s.",
typeof cell,
let message = "";
if (typeof cell === "object") {
message = _t(
"Function [[FUNCTION_NAME]] expects number values for %s, but got an empty value.",
argName
)
);
);
} else if (typeof cell === "string") {
message = _t(
"Function [[FUNCTION_NAME]] expects number values for %s, but got a string.",
argName
);
} else if (typeof cell === "boolean") {
message = _t(
"Function [[FUNCTION_NAME]] expects number values for %s, but got a boolean.",
argName
);
}
throw new Error(message);
}
});
});
Expand Down
20 changes: 10 additions & 10 deletions src/functions/module_statistical.ts
Original file line number Diff line number Diff line change
Expand Up @@ -832,9 +832,9 @@ export const GROWTH: AddFunctionDescription = {
assertNonEmptyMatrix(knownDataY, "known_data_y");
return expM(
predictLinearValues(
logM(tryCastAsNumberMatrix(knownDataY, "the first argument (known_data_y)")),
tryCastAsNumberMatrix(knownDataX, "the second argument (known_data_x)"),
tryCastAsNumberMatrix(newDataX, "the third argument (new_data_y)"),
logM(tryCastAsNumberMatrix(knownDataY, "known_data_y")),
tryCastAsNumberMatrix(knownDataX, "known_data_x"),
tryCastAsNumberMatrix(newDataX, "new_data_y"),
toBoolean(b)
)
);
Expand Down Expand Up @@ -944,8 +944,8 @@ export const LINEST: AddFunctionDescription = {
): (number | string)[][] {
assertNonEmptyMatrix(dataY, "data_y");
return fullLinearRegression(
tryCastAsNumberMatrix(dataX, "the first argument (data_y)"),
tryCastAsNumberMatrix(dataY, "the second argument (data_x)"),
tryCastAsNumberMatrix(dataX, "data_x"),
tryCastAsNumberMatrix(dataY, "data_y"),
toBoolean(calculateB),
toBoolean(verbose)
);
Expand Down Expand Up @@ -987,8 +987,8 @@ export const LOGEST: AddFunctionDescription = {
): (number | string)[][] {
assertNonEmptyMatrix(dataY, "data_y");
const coeffs = fullLinearRegression(
tryCastAsNumberMatrix(dataX, "the second argument (data_x)"),
logM(tryCastAsNumberMatrix(dataY, "the first argument (data_y)")),
tryCastAsNumberMatrix(dataX, "data_x"),
logM(tryCastAsNumberMatrix(dataY, "data_y")),
toBoolean(calculateB),
toBoolean(verbose)
);
Expand Down Expand Up @@ -1883,9 +1883,9 @@ export const TREND: AddFunctionDescription = {
): Matrix<number> {
assertNonEmptyMatrix(knownDataY, "known_data_y");
return predictLinearValues(
tryCastAsNumberMatrix(knownDataY, "the first argument (known_data_y)"),
tryCastAsNumberMatrix(knownDataX, "the second argument (known_data_x)"),
tryCastAsNumberMatrix(newDataX, "the third argument (new_data_y)"),
tryCastAsNumberMatrix(knownDataY, "known_data_y"),
tryCastAsNumberMatrix(knownDataX, "known_data_x"),
tryCastAsNumberMatrix(newDataX, "new_data_y"),
toBoolean(b)
);
},
Expand Down
7 changes: 7 additions & 0 deletions tests/functions/module_statistical.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3551,6 +3551,13 @@ describe("LINEST formula", () => {
const model = createModelFromGrid(grid);
expect(getEvaluatedCell(model, "A10").value).toBe("#ERROR");
});

test("Error message with empty values is correct", () => {
const model = createModelFromGrid({ A1: "=LINEST(C1:C2)" });
expect((getEvaluatedCell(model, "A1") as ErrorCell).error.message).toBe(
"Function LINEST expects number values for data_y, but got an empty value."
);
});
});

describe("LOGEST formula", () => {
Expand Down