Skip to content

Commit 14a6da6

Browse files
committed
[FIX] xlsx: cannot import CF with formulas
Importing a conditional formatting rule with formulas was not working because the formulas are not prefixed by "=" in the xlsx file. closes #6957 Task: 4945945 X-original-commit: 4b9a566 Signed-off-by: Rémi Rahir (rar) <rar@odoo.com> Signed-off-by: Adrien Minne (adrm) <adrm@odoo.com>
1 parent 97ccf4c commit 14a6da6

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

src/xlsx/conversion/cf_conversion.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { ICON_SETS } from "../../components/icons/icons";
2+
import { tokenize } from "../../formulas";
23
import {
34
ColorScaleMidPointThreshold,
45
ColorScaleThreshold,
@@ -75,9 +76,9 @@ export function convertConditionalFormats(
7576
case "cellIs":
7677
if (!rule.operator || !rule.formula || rule.formula.length === 0) continue;
7778
operator = convertCFCellIsOperator(rule.operator);
78-
values.push(rule.formula[0]);
79+
values.push(prefixFormula(rule.formula[0]));
7980
if (rule.formula.length === 2) {
80-
values.push(rule.formula[1]);
81+
values.push(prefixFormula(rule.formula[1]));
8182
}
8283
break;
8384
}
@@ -243,6 +244,12 @@ function convertIcons(xlsxIconSet: ExcelIconSet, index: number): string {
243244
: ICON_SETS[iconSet].good;
244245
}
245246

247+
/** Prefix the string by "=" if the string looks like a formula */
248+
function prefixFormula(formula: string): string {
249+
const tokens = tokenize(formula);
250+
return tokens.length === 1 && tokens[0].type !== "REFERENCE" ? formula : "=" + formula;
251+
}
252+
246253
// ---------------------------------------------------------------------------
247254
// Warnings
248255
// ---------------------------------------------------------------------------

tests/__xlsx__/xlsx_demo_data.xlsx

878 Bytes
Binary file not shown.

tests/xlsx/xlsx_import.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,15 @@ describe("Import xlsx data", () => {
320320
expect((cf.rule as CellIsRule).values).toEqual(values);
321321
});
322322

323+
test("Can import CF with formulas", () => {
324+
const testSheet = getWorkbookSheet("jestCfs", convertedData)!;
325+
const cf = getCFBeginningAt("B29", testSheet)!;
326+
327+
expect(cf.rule.type).toEqual("CellIsRule");
328+
expect((cf.rule as CellIsRule).operator).toEqual("Between");
329+
expect((cf.rule as CellIsRule).values).toEqual(["=$B$23", "=2+2"]);
330+
});
331+
323332
test.each([
324333
["A2"], // number
325334
["F2"], // time

0 commit comments

Comments
 (0)