Skip to content

Commit

Permalink
[FIX] XLSX: Export values along the formula string
Browse files Browse the repository at this point in the history
While it seems redundant to export the evaluated final value of a formula
in the xlsx file, that information is acutally relevant for parsers that
want to process "raw" data from the xlsx file.

Among those parsers lie Odoo's import process...

closes #4944

Task: 4141855
X-original-commit: ecdc7a5
Signed-off-by: Lucas Lefèvre (lul) <[email protected]>
Signed-off-by: Rémi Rahir (rar) <[email protected]>
  • Loading branch information
rrahir committed Sep 4, 2024
1 parent 27ff4ae commit fe643b7
Show file tree
Hide file tree
Showing 4 changed files with 1,430 additions and 357 deletions.
20 changes: 8 additions & 12 deletions src/xlsx/functions/cells.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ import { functionRegistry } from "../../functions";
import { formatValue, isNumber } from "../../helpers";
import { mdyDateRegexp, parseDateTime, timeRegexp, ymdDateRegexp } from "../../helpers/dates";
import { ExcelCellData, Format } from "../../types";
import { CellErrorType } from "../../types/errors";
import { XMLAttributes, XMLString } from "../../types/xlsx";
import { FORCE_DEFAULT_ARGS_FUNCTIONS, NON_RETROCOMPATIBLE_FUNCTIONS } from "../constants";
import { pushElement } from "../helpers/content_helpers";
import { getCellType, pushElement } from "../helpers/content_helpers";
import { escapeXml } from "../helpers/xml_helpers";
import { DEFAULT_LOCALE } from "./../../types/locale";

Expand All @@ -26,18 +25,15 @@ export function addFormula(cell: ExcelCellData): {
return { attrs: [], node: escapeXml`` };
}

const attrs: XMLAttributes = [];
let node = escapeXml``;
const type = getCellType(cell.value);
if (type === undefined) {
return { attrs: [], node: escapeXml`` };
}

let cycle = escapeXml``;
const attrs: XMLAttributes = [["t", type]];
const XlsxFormula = adaptFormulaToExcel(formula);
// hack for cycles : if we don't set a value (be it 0 or #VALUE!), it will appear as invisible on excel,
// Making it very hard for the client to find where the recursion is.
if (cell.value === CellErrorType.CircularDependency) {
attrs.push(["t", "str"]);
cycle = escapeXml/*xml*/ `<v>${cell.value}</v>`;
}
node = escapeXml/*xml*/ `<f>${XlsxFormula}</f>${cycle}`;

const node = escapeXml/*xml*/ `<f>${XlsxFormula}</f><v>${cell.value}</v>`;
return { attrs, node };
}

Expand Down
13 changes: 13 additions & 0 deletions src/xlsx/helpers/content_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@ export function convertOperator(operator: ConditionalFormattingOperatorValues):
// WORKSHEET HELPERS
// -------------------------------------

export function getCellType(value: number | string | boolean | null): string | undefined {
switch (typeof value) {
case "boolean":
return "b";
case "string":
return "str";
case "number":
return "n";
default:
return undefined;
}
}

export function convertHeightToExcel(height: number): number {
return Math.round(HEIGHT_FACTOR * height * 100) / 100;
}
Expand Down
Loading

0 comments on commit fe643b7

Please sign in to comment.