Skip to content

Read formula cells by evaluating, not just the cached result#1120

Merged
iroqueta merged 1 commit into
masterfrom
fix/xls-formula-cells-return-zero
Jun 8, 2026
Merged

Read formula cells by evaluating, not just the cached result#1120
iroqueta merged 1 commit into
masterfrom
fix/xls-formula-cells-return-zero

Conversation

@iroqueta

@iroqueta iroqueta commented Jun 4, 2026

Copy link
Copy Markdown
Collaborator

Problem

Reading a numeric formula cell from an Excel file returned 0 when the file did not have the formula's cached result stored. This is common in legacy .xls files produced by tools/systems that write the formula but leave the cached value at 0.

POI does not recalculate formulas on read: getNumericCellValue() returns the cached result, so the cell reads as 0. The same logical content saved as .xlsx worked only because Excel had stored the computed cache.

Evidence (real customer files, POI 5.4.1)

Cell Formula .xls cached .xlsx cached Evaluated
B2 =D2 0.0 140.0 140.0
B3 =D3 0.0 120.0 120.0

It is not an .xls-vs-.xlsx format issue: both behave the same when the cache is missing. Evaluating the formula returns the correct value for both.

Change

For FORMULA cells, resolve the numeric value with a FormulaEvaluator, falling back to the cached value when evaluation is not supported by POI (NotImplementedException). evaluate() does not mutate the cell or the cached value, so it is safe on read.

Applied in getNumber() (which also feeds getValue()) of the HSSF and XSSF ExcelCells in the gxoffice (ExcelDocument) API:

  • com/genexus/gxoffice/poi/hssf/ExcelCells.java
  • com/genexus/gxoffice/poi/xssf/ExcelCells.java

Notes

  • Read-only and backward compatible: files that already carry cached results behave exactly as before; unsupported functions fall back to the cached value.
  • Scope limited to the numeric path (the reported issue). The date path is unchanged.

Reading a numeric formula cell returned 0 for files whose cached formula
result was not stored (e.g. legacy .xls produced by tools that write the
formula but leave the cached value at 0). POI does not recalculate on read:
getNumericCellValue() returns the cached result, so the cell read as 0.
The same content saved as .xlsx worked only because Excel had stored the
computed cache.

Resolve the value with a FormulaEvaluator for FORMULA cells, falling back
to the cached value if evaluation is not supported by POI
(NotImplementedException). evaluate() does not mutate the cell/cache, so it
is safe on read. Applied to getNumber() (which also feeds getValue()) in the
HSSF and XSSF ExcelCells of the gxoffice (ExcelDocument) API.
@genexusbot

Copy link
Copy Markdown
Collaborator
Cherry pick to beta success

@iroqueta iroqueta merged commit baa2633 into master Jun 8, 2026
10 checks passed
@iroqueta iroqueta deleted the fix/xls-formula-cells-return-zero branch June 8, 2026 19:29
iroqueta added a commit that referenced this pull request Jun 8, 2026
Reading a numeric formula cell returned 0 for files whose cached formula
result was not stored (e.g. legacy .xls produced by tools that write the
formula but leave the cached value at 0). POI does not recalculate on read:
getNumericCellValue() returns the cached result, so the cell read as 0.
The same content saved as .xlsx worked only because Excel had stored the
computed cache.

Resolve the value with a FormulaEvaluator for FORMULA cells, falling back
to the cached value if evaluation is not supported by POI
(NotImplementedException). evaluate() does not mutate the cell/cache, so it
is safe on read. Applied to getNumber() (which also feeds getValue()) in the
HSSF and XSSF ExcelCells of the gxoffice (ExcelDocument) API.

(cherry picked from commit baa2633)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants