Skip to content

Commit 03fc65a

Browse files
hokolomoporrahir
authored andcommitted
[FIX] functions: fix LINEST error massage
The functions using the helper `tryCastAsNumberMatrix` had a wrong error message: - the two arguments in the translated string were inverted - an argument of `_t` was `typeof cell`, which isn't translated - the other argument of the `_t` was something like `the first argument (data_y)`, which wasn't translated either closes #7109 Task: 5059375 Signed-off-by: Rémi Rahir (rar) <rar@odoo.com>
1 parent 785707a commit 03fc65a

File tree

3 files changed

+34
-16
lines changed

3 files changed

+34
-16
lines changed

src/functions/helpers.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,24 @@ export function tryCastAsNumberMatrix(data: Matrix<any>, argName: string): Matri
7878
data.forEach((row) => {
7979
row.forEach((cell) => {
8080
if (typeof cell !== "number") {
81-
throw new Error(
82-
_t(
83-
"Function [[FUNCTION_NAME]] expects number values for %s, but got a %s.",
84-
typeof cell,
81+
let message = "";
82+
if (typeof cell === "object") {
83+
message = _t(
84+
"Function [[FUNCTION_NAME]] expects number values for %s, but got an empty value.",
8585
argName
86-
)
87-
);
86+
);
87+
} else if (typeof cell === "string") {
88+
message = _t(
89+
"Function [[FUNCTION_NAME]] expects number values for %s, but got a string.",
90+
argName
91+
);
92+
} else if (typeof cell === "boolean") {
93+
message = _t(
94+
"Function [[FUNCTION_NAME]] expects number values for %s, but got a boolean.",
95+
argName
96+
);
97+
}
98+
throw new Error(message);
8899
}
89100
});
90101
});

src/functions/module_statistical.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -832,9 +832,9 @@ export const GROWTH: AddFunctionDescription = {
832832
assertNonEmptyMatrix(knownDataY, "known_data_y");
833833
return expM(
834834
predictLinearValues(
835-
logM(tryCastAsNumberMatrix(knownDataY, "the first argument (known_data_y)")),
836-
tryCastAsNumberMatrix(knownDataX, "the second argument (known_data_x)"),
837-
tryCastAsNumberMatrix(newDataX, "the third argument (new_data_y)"),
835+
logM(tryCastAsNumberMatrix(knownDataY, "known_data_y")),
836+
tryCastAsNumberMatrix(knownDataX, "known_data_x"),
837+
tryCastAsNumberMatrix(newDataX, "new_data_y"),
838838
toBoolean(b)
839839
)
840840
);
@@ -944,8 +944,8 @@ export const LINEST: AddFunctionDescription = {
944944
): (number | string)[][] {
945945
assertNonEmptyMatrix(dataY, "data_y");
946946
return fullLinearRegression(
947-
tryCastAsNumberMatrix(dataX, "the first argument (data_y)"),
948-
tryCastAsNumberMatrix(dataY, "the second argument (data_x)"),
947+
tryCastAsNumberMatrix(dataX, "data_x"),
948+
tryCastAsNumberMatrix(dataY, "data_y"),
949949
toBoolean(calculateB),
950950
toBoolean(verbose)
951951
);
@@ -987,8 +987,8 @@ export const LOGEST: AddFunctionDescription = {
987987
): (number | string)[][] {
988988
assertNonEmptyMatrix(dataY, "data_y");
989989
const coeffs = fullLinearRegression(
990-
tryCastAsNumberMatrix(dataX, "the second argument (data_x)"),
991-
logM(tryCastAsNumberMatrix(dataY, "the first argument (data_y)")),
990+
tryCastAsNumberMatrix(dataX, "data_x"),
991+
logM(tryCastAsNumberMatrix(dataY, "data_y")),
992992
toBoolean(calculateB),
993993
toBoolean(verbose)
994994
);
@@ -1883,9 +1883,9 @@ export const TREND: AddFunctionDescription = {
18831883
): Matrix<number> {
18841884
assertNonEmptyMatrix(knownDataY, "known_data_y");
18851885
return predictLinearValues(
1886-
tryCastAsNumberMatrix(knownDataY, "the first argument (known_data_y)"),
1887-
tryCastAsNumberMatrix(knownDataX, "the second argument (known_data_x)"),
1888-
tryCastAsNumberMatrix(newDataX, "the third argument (new_data_y)"),
1886+
tryCastAsNumberMatrix(knownDataY, "known_data_y"),
1887+
tryCastAsNumberMatrix(knownDataX, "known_data_x"),
1888+
tryCastAsNumberMatrix(newDataX, "new_data_y"),
18891889
toBoolean(b)
18901890
);
18911891
},

tests/functions/module_statistical.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3551,6 +3551,13 @@ describe("LINEST formula", () => {
35513551
const model = createModelFromGrid(grid);
35523552
expect(getEvaluatedCell(model, "A10").value).toBe("#ERROR");
35533553
});
3554+
3555+
test("Error message with empty values is correct", () => {
3556+
const model = createModelFromGrid({ A1: "=LINEST(C1:C2)" });
3557+
expect((getEvaluatedCell(model, "A1") as ErrorCell).error.message).toBe(
3558+
"Function LINEST expects number values for data_y, but got an empty value."
3559+
);
3560+
});
35543561
});
35553562

35563563
describe("LOGEST formula", () => {

0 commit comments

Comments
 (0)