How to Get Computed Cell Values from Excel Using OfficeCLI
OfficeCLI evaluates Excel formulas on-the-fly through its embedded calculation engine and returns computed values via the get and view commands without requiring Microsoft Excel installation.
OfficeCLI, maintained by iOfficeAI, is an open-source command-line tool for manipulating Excel workbooks stored as OpenXML (.xlsx) files. When you need to extract calculated results rather than formula strings, the tool automatically invokes its built-in formula engine to compute current values during standard query operations.
How Formula Evaluation Works in OfficeCLI
The evaluation process centers on two core components: the cell value resolution logic and the formula calculation engine.
The GetCellDisplayValue Method
In ExcelHandler.Helpers.Cell.cs, the GetCellDisplayValue method (lines 41-96) serves as the entry point for cell value retrieval. This method examines cell contents and applies specific formatting rules: inline strings return unchanged, boolean values normalize to "TRUE" or "FALSE" (lines 60-66), and formula cells trigger evaluation when no cached value exists.
When evaluation is required, the method instantiates a FormulaEvaluator and executes evaluator.EvaluateForReport. Successful evaluations pass through ToCellValueText() for formatting (lines 84-88), while failures—such as references to missing sheets—return the sentinel value #OCLI_NOTEVAL! (lines 80-96).
The FormulaEvaluator Engine
The calculation capabilities reside in the Core/Formula/FormulaEvaluator.* files, including FormulaEvaluator.Helpers.cs and FormulaEvaluator.Functions.cs. This engine implements over 350 Excel functions, supporting dynamic array spilling, financial calculations, and statistical operations entirely within the binary. Because the engine operates independently, computed values become available immediately after any set or add operation without external dependencies.
Retrieving Single Computed Values
To obtain a computed value from a specific cell, use the get command with the target cell path. OfficeCLI loads the worksheet, invokes GetCellDisplayValue with formula evaluation if necessary, and returns the current result.
# 1. Create a new workbook and add a formula
officecli create grades.xlsx
officecli set grades.xlsx /Sheet1/A1 --prop formula="=SUM(B1:C1)"
officecli set grades.xlsx /Sheet1/B1 --prop number=5
officecli set grades.xlsx /Sheet1/C1 --prop number=7
# 2. Retrieve the computed value of the formula cell (A1)
officecli get grades.xlsx /Sheet1/A1 --json
# → {"tag":"cell","path":"/Sheet1/A1","attributes":{"value":"12"}}
# 3. Retrieve the value as plain text (no JSON)
officecli get grades.xlsx /Sheet1/A1
# → 12
# 4. View a range with all computed values (text mode)
officecli view grades.xlsx /Sheet1 --text --cols A,B,C
# A1 12
# B1 5
# C1 7
# 5. Force re‑evaluation after editing a referenced cell
officecli set grades.xlsx /Sheet1/B1 --prop number=10
officecli get grades.xlsx /Sheet1/A1 # now returns 17
The set command writes values or formulas, while the subsequent get operation transparently fetches the computed result. No manual recalculation step is required because the engine evaluates formulas dynamically during the fetch operation.
Viewing Ranges of Computed Values
For batch retrieval across multiple cells, the view command displays computed values in tabular format. This approach evaluates all formulas within the specified range before rendering output.
Use the --json flag instead of --text to receive structured data suitable for programmatic parsing in CI pipelines or automation scripts.
Handling Evaluation Errors and Edge Cases
When the formula engine encounters unsupported references or circular dependencies, GetCellDisplayValue returns #OCLI_NOTEVAL! as a sentinel value. This explicit error indicator allows automation scripts to detect evaluation failures without parsing exception messages.
Boolean cells receive special handling: the method normalizes True/False values to uppercase string representations "TRUE" and "FALSE" according to Excel conventions (lines 60-66 in ExcelHandler.Helpers.Cell.cs).
Summary
- OfficeCLI embeds a full-featured formula engine supporting 350+ Excel functions without external Office installations.
- The
GetCellDisplayValuemethod inExcelHandler.Helpers.Cell.csorchestrates value retrieval and formula evaluation at lines 41-96. - Use
officecli get <file> <path>to retrieve automatically computed values in plain text or JSON format. - The
viewcommand evaluates ranges of formulas for batch reporting and inspection. - Failed evaluations return
#OCLI_NOTEVAL!to indicate computation errors in the source data.
Frequently Asked Questions
Do I need Microsoft Excel installed to get computed values?
No. OfficeCLI operates entirely through its embedded OpenXML handling and formula evaluation logic. The FormulaEvaluator class implements all calculation capabilities within the binary itself, making the tool suitable for headless server environments, CI pipelines, and Docker containers where installing Microsoft Office is impractical.
What happens when a formula references a missing sheet or contains an error?
The GetCellDisplayValue method returns the sentinel value #OCLI_NOTEVAL! when evaluation fails due to missing references, unsupported functions, or circular dependencies. This occurs in the error handling block at lines 80-96 of ExcelHandler.Helpers.Cell.cs, allowing your scripts to detect computation failures explicitly.
How does OfficeCLI handle boolean values in cells?
Boolean cells are normalized to uppercase string literals. According to lines 60-66 in ExcelHandler.Helpers.Cell.cs, true values render as "TRUE" and false values as "FALSE", maintaining compatibility with Excel's display conventions.
Can I force re-evaluation after updating dependent cells?
Re-evaluation happens automatically. When you update a referenced cell using officecli set and subsequently query the dependent cell with get, OfficeCLI invokes the formula engine to compute the fresh value based on current data. No explicit refresh or recalculation command is required.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →