XLSX Formulas and Cached Values: Why Libraries Return Old Results
Explain formula XML, cached results, calculation mode and reliable recalculation workflows.
How Formula Cells Store Two Values in sheet.xml
Every formula cell in an XLSX file carries two pieces of data inside the worksheet XML:
<f>element — the formula string (for example,=SUM(A1:A10)).<v>element — the last computed result that Excel saved when the workbook was last opened with calculation enabled. This is the cached value.
A library that reads the file without a calculation engine returns the content of <v>. A library that does calculate may still return <v> if it obeys the file’s calculation mode flag, which instructs applications to skip recalculation.
Inspect both values directly
- Rename the
.xlsxfile to.zipand extract it. - Open
xl/worksheets/sheet1.xml. - Locate a
<c>element with an<f>child:
<c r="C5" s="1">
<f>SUM(A1:A10)</f>
<v>7</v>
</c>
Here 7 is the cached value. If a library returns 7, it read <v> and did not execute <f>.
How calcMode in workbook.xml Controls Recalculation
The xl/workbook.xml file may contain a <calcPr> element with a calcMode attribute. The XLSX specification defines three values:
auto— recalculate all cells on open.autoNoTable— recalculate cells outside data tables.manual— do not recalculate; display cached values.
A file saved with calcMode="manual" and no fullCalcOnLoad="true" attribute tells conforming libraries to skip recalculation and return the stale <v>.
Check the mode in your file
Extract xl/workbook.xml and search for <calcPr>. A manual-mode declaration looks like:
<calcPr calcMode="manual" calcCompleted="true"/>
A library that respects this flag will not touch formulas. A library that ignores it may recalculate anyway, producing results that differ from Excel’s behavior.
When the Cached Value Is Wrong by Design
Cached values are not always merely stale; they can be intentionally different from a fresh formula result. Excel saves them to speed up the next open. If the formula references external files, volatile functions, or data connections, the cached value is correct only for the moment the file was saved.
Specific failure modes
- External references (
=[Source.xlsx]Sheet1!A1): The cache holds the value from the last time the external file was available. Without that file, a library may return the cache or raise an error. - Volatile functions (
RAND(),TODAY()): The cache is a single frozen point. Every recalculation would differ. Libraries that skip calculation return the same frozen number indefinitely. - Array formulas (
{=SUM(IF(...))}): The cached value for the top-left cell may be present, but the rest of the array range may have no cached values. Libraries that do not handle array formulas can return#VALUE!or empty strings.
Why Most Libraries Return Cached Values
Popular XLSX libraries (openpyxl, Apache POI, Node-XLSX) are read/write containers, not calculation engines. They preserve the file structure, including the cached value, exactly as saved. They do not execute formulas because:
- Formula execution requires implementing hundreds of functions, many with Excel-specific edge cases.
- Circular references and iterative calculation are difficult to replicate outside Excel.
- The library cannot determine whether to respect
calcMode="manual"or override it.
A library that does calculate (for example, LibreOffice’s UNO API) typically ignores the cached value and computes from the formula. Even then, the result may differ from Excel due to floating-point precision, date serial number differences, or unimplemented functions.
Three Workflows for Reliable Recalculated Values
Each workflow produces a file where the cached value matches the live formula result.
1. Recalculate with a headless spreadsheet application
Use LibreOffice or Excel’s command-line interface to open, recalculate, and save the file.
soffice --headless --calc --convert-to xlsx --infilter=Calc\ XLSX myfile.xlsx
This forces a full recalculation and writes new cached values. The output file will have calcMode="auto" or no <calcPr> element. This is the most reliable method because the same engine that wrote the format performs the calculation.
2. Patch the XML to force recalculation on next open
This works only for deterministic formulas (no volatile functions, no external references) and requires the file to be opened later in Excel or LibreOffice.
- Extract the XLSX.
- In
xl/workbook.xml, remove the<calcPr>element or setcalcMode="auto"andfullCalcOnLoad="true". - In each
sheet.xml, delete all<v>elements from formula cells. - Re-zip the file.
When opened in Excel or LibreOffice, the application recalculates every cell because the cache is empty and the mode requests it. This does not work for libraries that lack a calculation engine — they return empty strings for the missing <v>.
3. Pair a read/write library with a formula evaluator
Libraries like openpyxl with data_only=True return cached values. To get calculated values, pair the library with a formula evaluator such as formulas (Python).
from openpyxl import load_workbook
from formulas import ExcelModel
wb = load_workbook('file.xlsx', data_only=False) # keep formulas
model = ExcelModel()
model.read_xlsx('file.xlsx')
result = model.calculate()
This handles basic arithmetic, SUM, IF, and lookup functions. It fails for INDIRECT, OFFSET, and anything referencing external files. Results may differ from Excel on edge cases.
What Conversion Cannot Recover for XLSX Formulas and Cached Values
When converting XLSX to CSV, HTML, or JSON without recalculating, the cached value is the only data that survives. Specific losses include:
- Circular references: The cache may hold a value from the last iteration, but the formula logic is discarded. The converter sees only the number.
- Multi-sheet references: The cache is per-cell; formula logic that spans sheets is lost.
- Volatile results: The output is permanently frozen at the cached value. Every conversion from the same file produces the same number, even if the original would change daily.
To preserve dynamic behavior in a converted format, recalculate first (workflow 1), then convert. The result is a snapshot of the most recent calculation, truthful to the file’s state at that moment.
Verify the Fix
After any recalculation workflow, confirm success:
- Open the file in Excel or LibreOffice and inspect a known formula cell.
- Re-extract
sheet.xmland compare the<v>value to a manual calculation of the formula. - Check
xl/workbook.xmlfor<calcPr calcMode="auto"/>or the absence of the element.
If the cached value still differs from the formula result, the tool you used did not recalculate — it only rewrote the file structure. Repeat with a headless application recalculation.