Skip to content

Commit 056b5c5

Browse files
committed
[IMP] functions: Add ISFORMULA function
closes #8067 Task: 6013888 X-original-commit: 90cb91a Signed-off-by: Lucas Lefèvre (lul) <lul@odoo.com> Signed-off-by: Rémi Rahir (rar) <rar@odoo.com>
1 parent 61f0761 commit 056b5c5

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

demo/data.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1418,6 +1418,7 @@ export const demoData = {
14181418
A231: "ARRAYTOTEXT",
14191419
A232: "FORMULATEXT",
14201420
A233: "TAKE",
1421+
A234: "ISFORMULA",
14211422
B1: "evaluation",
14221423
B2: "=ABS(-5.5)",
14231424
B3: "=ACOS(1)",
@@ -1651,6 +1652,7 @@ export const demoData = {
16511652
B231: "=ARRAYTOTEXT(A227:B228,1)",
16521653
B232: "=FORMULATEXT(B227)",
16531654
B233: "=TAKE(A227:B228,1,1)",
1655+
B234: "=ISFORMULA(D228)",
16541656
C1: "expected value",
16551657
C2: "5.5",
16561658
C3: "0",
@@ -1881,6 +1883,7 @@ export const demoData = {
18811883
C231: '{"TEXTBEFORE","Hello there";"REGEXEXTRACT","Kenobi"}',
18821884
C232: '\'=TEXTBEFORE("Hello there; General Kenobi", "; ")',
18831885
C233: "TEXTBEFORE",
1886+
C234: "TRUE",
18841887
D1: "is it compatible ?",
18851888
D2: "=IF(B2=C2,1, 0)",
18861889
D3: "=IF(B3=C3,1, 0)",
@@ -2114,6 +2117,7 @@ export const demoData = {
21142117
D231: "=IF(B231=C231,1, 0)",
21152118
D232: "=IF(B232=C232,1, 0)",
21162119
D233: "=IF(B233=C233,1, 0)",
2120+
D234: "=IF(B234=C234,1, 0)",
21172121
G1: "Name",
21182122
G2: "Robot1",
21192123
G3: "Robot2",

packages/o-spreadsheet-engine/src/functions/module_info.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
import { toCartesian } from "../helpers/coordinates";
12
import { getFullReference, setXcToFixedReferenceType, splitReference } from "../helpers/references";
23
import { _t } from "../translation";
34
import { CellValueType } from "../types/cells";
45
import { CellErrorType, EvaluationError } from "../types/errors";
56
import { AddFunctionDescription } from "../types/functions";
6-
import { FunctionResultObject, Matrix, Maybe } from "../types/misc";
7+
import { FunctionResultObject, Matrix, Maybe, UID } from "../types/misc";
78
import { arg } from "./arguments";
89
import { isEvaluationError, toString } from "./helpers";
910

@@ -196,3 +197,22 @@ export const NA = {
196197
},
197198
isExported: true,
198199
} satisfies AddFunctionDescription;
200+
201+
//--------------------------------------------------------------------------
202+
// ISFORMULA
203+
//--------------------------------------------------------------------------
204+
export const ISFORMULA = {
205+
description: _t(
206+
"Checks whether there is a reference to a cell that contains a formula, and returns TRUE or FALSE."
207+
),
208+
args: [arg("cell_reference (meta)", _t("A reference to a cell."))],
209+
compute: function (cellReference: { value: string }) {
210+
const { sheetName, xc } = splitReference(cellReference.value);
211+
const { col, row } = toCartesian(xc);
212+
const sheetId: UID =
213+
(sheetName && this.getters.getSheetIdByName(sheetName)) ?? this.__originSheetId;
214+
const cell = this.getters.getCell({ sheetId, col, row });
215+
return cell?.isFormula ?? false;
216+
},
217+
isExported: true,
218+
} satisfies AddFunctionDescription;

tests/functions/module_info.test.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Model } from "../../src";
22
import { createSheet, setCellContent, setFormat } from "../test_helpers/commands_helpers";
33
import { getCellContent } from "../test_helpers/getters_helpers";
4-
import { createModelFromGrid, evaluateCell, setGrid } from "../test_helpers/helpers";
4+
import { createModelFromGrid, evaluateCell, evaluateGrid, setGrid } from "../test_helpers/helpers";
55

66
describe("CELL formula", () => {
77
test("CELL takes 2 arguments", () => {
@@ -395,3 +395,29 @@ describe("NA formula", () => {
395395
expect(evaluateCell("A1", { A1: "=NA()" })).toBe("#N/A");
396396
});
397397
});
398+
399+
describe("ISFORMULA formula", () => {
400+
test("test All different types", () => {
401+
//prettier-ignore
402+
const grid = {
403+
A1: "=SUM(B1:B2)", B1: "=ISFORMULA(A1)",
404+
A2: "", B2: "=ISFORMULA(A2)",
405+
A3: "just text", B3: "=ISFORMULA(A3)",
406+
A4: "42", B4: "=ISFORMULA(A4)",
407+
A5: "=1/0", B5: "=ISFORMULA(A5)",
408+
A6: "=#REF", B6: "=ISFORMULA(A6)",
409+
B7: "=ISFORMULA(A7, 1)",
410+
B8: "=ISFORMULA(sum(1,2))",
411+
412+
};
413+
const gridResult = evaluateGrid(grid);
414+
expect(gridResult.B1).toBe(true);
415+
expect(gridResult.B2).toBe(false);
416+
expect(gridResult.B3).toBe(false);
417+
expect(gridResult.B4).toBe(false);
418+
expect(gridResult.B5).toBe(true);
419+
expect(gridResult.B6).toBe(true);
420+
expect(gridResult.B7).toBe("#BAD_EXPR"); // @compatibility: on google sheets, return #N/A
421+
expect(gridResult.B8).toBe("#BAD_EXPR"); // @compatibility: on google sheets, return #N/A
422+
});
423+
});

0 commit comments

Comments
 (0)