diff --git a/src/functions/module_filter.ts b/src/functions/module_filter.ts index 64bc97dcb1..f2c2e931b4 100644 --- a/src/functions/module_filter.ts +++ b/src/functions/module_filter.ts @@ -146,7 +146,9 @@ export const FILTER = { const result: Matrix = []; for (let i = 0; i < _array.length; i++) { const row = _array[i]; - if (_conditions.every((c) => c[i])) { + if ( + _conditions.every((c) => (typeof c[i] === "boolean" || typeof c[i] === "number") && c[i]) + ) { result.push(row); } } diff --git a/tests/functions/module_filter.test.ts b/tests/functions/module_filter.test.ts index 145d96bc5a..5c8f72109e 100644 --- a/tests/functions/module_filter.test.ts +++ b/tests/functions/module_filter.test.ts @@ -109,6 +109,17 @@ describe("FILTER function", () => { expect(checkFunctionDoesntSpreadBeyondRange(model, "D1")).toBeTruthy(); }); + test("FILTER by string ignores the value", () => { + // prettier-ignore + const grid = { + A1: "Alice", B1: "yes", + A2: "Bob", B2: "TRUE", + }; + const model = createModelFromGrid(grid); + setCellContent(model, "A6", "=FILTER(A1:A2, B1:B2)"); + expect(getRangeValuesAsMatrix(model, "A6:A7")).toEqual([["Bob"], [null]]); + }); + test("FILTER accepts errors in first argument", () => { // prettier-ignore const grid = { @@ -120,6 +131,18 @@ describe("FILTER function", () => { setCellContent(model, "A6", "=FILTER(A1:A3, B1:B3)"); expect(getRangeValuesAsMatrix(model, "A6:A7")).toEqual([["#BAD_EXPR"], ["John"]]); }); + + test("FILTER accepts errors in condition arguments", () => { + // prettier-ignore + const grid = { + A1: "Alice", B1: "TRUE", C1: "TRUE", + A2: "Peter", B2: "=KABOUM", C2: "TRUE", + A3: "John", B3: "TRUE", C3: "=KABOUM", + }; + const model = createModelFromGrid(grid); + setCellContent(model, "A6", "=FILTER(A1:A3, B1:B3, C1:C3)"); + expect(getRangeValuesAsMatrix(model, "A6:A8")).toEqual([["Alice"], [null], [null]]); + }); }); describe("UNIQUE function", () => {