Skip to content

Commit

Permalink
[FIX] xlsx: convert #REF at export to xlsx
Browse files Browse the repository at this point in the history
Reference errors in excel are #REF!, not #REF.

closes #5153

Task: 4207052
X-original-commit: 595963d
Signed-off-by: Lucas Lefèvre (lul) <[email protected]>
Signed-off-by: Adrien Minne (adrm) <[email protected]>
  • Loading branch information
hokolomopo committed Oct 29, 2024
1 parent bebac5f commit c78551c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/xlsx/functions/cells.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
import { functionRegistry } from "../../functions";
import { formatValue, isNumber } from "../../helpers";
import { mdyDateRegexp, parseDateTime, timeRegexp, ymdDateRegexp } from "../../helpers/dates";
import { ExcelCellData, Format } from "../../types";
import { CellValue, 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 { getCellType, pushElement } from "../helpers/content_helpers";
Expand All @@ -33,7 +34,8 @@ export function addFormula(cell: ExcelCellData): {
const attrs: XMLAttributes = [["t", type]];
const XlsxFormula = adaptFormulaToExcel(formula);

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

Expand Down Expand Up @@ -76,9 +78,16 @@ export function adaptFormulaToExcel(formulaText: string): string {
ast = addMissingRequiredArgs(ast);
return ast;
});
ast = convertAstNodes(ast, "REFERENCE", (ast) => {
return ast.value === CellErrorType.InvalidReference ? { ...ast, value: "#REF!" } : ast;
});
return ast ? astToFormula(ast) : formulaText;
}

function adaptFormulaValueToExcel(formulaValue: CellValue): CellValue {
return formulaValue === CellErrorType.InvalidReference ? "#REF!" : formulaValue;
}

/**
* Some Excel function need required args that might not be mandatory in o-spreadsheet.
* This adds those missing args.
Expand Down
10 changes: 10 additions & 0 deletions tests/xlsx/__snapshots__/xlsx_export.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -20082,6 +20082,16 @@ exports[`Test XLSX export Generic sheets (style, hidden, size, cf) Simple model
</v>
</c>
</row>
<row r="41">
<c r="A41" t="str">
<f>
#REF!+5
</f>
<v>
#REF!
</v>
</c>
</row>
</sheetData>
</worksheet>",
"contentType": "sheet",
Expand Down
1 change: 1 addition & 0 deletions tests/xlsx/xlsx_export.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ const simpleData = {
A38: { content: `='<Sheet2>'!B2` },
A39: { content: `=A39` },
A40: { content: `=(1+2)/3` },
A41: { content: "=#REF + 5" },
K3: { border: 5 },
K4: { border: 4 },
K5: { border: 4 },
Expand Down

0 comments on commit c78551c

Please sign in to comment.