diff --git a/src/xlsx/functions/cells.ts b/src/xlsx/functions/cells.ts
index d3717fee20..67494e5c4e 100644
--- a/src/xlsx/functions/cells.ts
+++ b/src/xlsx/functions/cells.ts
@@ -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";
@@ -33,7 +34,8 @@ export function addFormula(cell: ExcelCellData): {
const attrs: XMLAttributes = [["t", type]];
const XlsxFormula = adaptFormulaToExcel(formula);
- const node = escapeXml/*xml*/ `${XlsxFormula}${cell.value}`;
+ const exportedValue = adaptFormulaValueToExcel(cell.value);
+ const node = escapeXml/*xml*/ `${XlsxFormula}${exportedValue}`;
return { attrs, node };
}
@@ -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.
diff --git a/tests/xlsx/__snapshots__/xlsx_export.test.ts.snap b/tests/xlsx/__snapshots__/xlsx_export.test.ts.snap
index 34255ac654..6dd76c92a4 100644
--- a/tests/xlsx/__snapshots__/xlsx_export.test.ts.snap
+++ b/tests/xlsx/__snapshots__/xlsx_export.test.ts.snap
@@ -10825,6 +10825,16 @@ exports[`Test XLSX export Generic sheets (style, hidden, size, cf) Simple model
+
+
+
+ #REF!+5
+
+
+ #REF!
+
+
+
",
"contentType": "sheet",
diff --git a/tests/xlsx/xlsx_export.test.ts b/tests/xlsx/xlsx_export.test.ts
index ab2e9e1f69..15ff03123e 100644
--- a/tests/xlsx/xlsx_export.test.ts
+++ b/tests/xlsx/xlsx_export.test.ts
@@ -89,6 +89,7 @@ const simpleData = {
A38: { content: `=''!B2` },
A39: { content: `=A39` },
A40: { content: `=(1+2)/3` },
+ A41: { content: "=#REF + 5" },
K3: { border: 5 },
K4: { border: 4 },
K5: { border: 4 },