OpenXLSX 1.12.1
Modern C++ Excel Library (Fork of OpenXLSX by Kenneth Troldal Balslev)
Loading...
Searching...
No Matches
OpenXLSX::XLCalculationEngine Class Reference

Sheet- or workbook-scoped formula calculation engine (Phase C). More...

#include <XLCalculationEngine.hpp>

Public Types

using FormulaMap = std::unordered_map<std::string, std::string>
 "A1" or "Sheet2!A1" -> formula
using ValueMap = std::unordered_map<std::string, XLCellValue>

Public Member Functions

 XLCalculationEngine (XLWorksheet &worksheet, XLCalculationOptions options={})
 XLCalculationEngine (XLDocument &document, XLCalculationOptions options={})
 All worksheets in a document (multi-sheet graph; keys always carry sheet name).
 XLCalculationEngine (FormulaMap formulas, ValueMap values, XLCalculationOptions options={}, std::string defaultSheet={})
 In-memory model for unit tests.
 ~XLCalculationEngine ()
 XLCalculationEngine (const XLCalculationEngine &)=delete
XLCalculationEngine & operator= (const XLCalculationEngine &)=delete
 XLCalculationEngine (XLCalculationEngine &&)=delete
XLCalculationEngine & operator= (XLCalculationEngine &&)=delete
const XLCalculationOptions & options () const noexcept
void setOptions (XLCalculationOptions options)
bool isMultiSheet () const noexcept
void rebuild ()
size_t formulaCount () const noexcept
size_t dirtyCount () const noexcept
std::vector< XLCellKey > dependencies (const XLCellKey &cell) const
std::vector< XLCellKey > dependencies (std::string_view a1) const
std::vector< XLCellKey > dependents (const XLCellKey &cell) const
std::vector< XLCellKey > dependents (std::string_view a1) const
XLCellValue calcCellValue (const XLCellKey &cell)
 Evaluate one cell, recursively calculating precedent formula cells.
XLCellValue calcCellValue (std::string_view a1)
XLCalcStatus lastStatus () const noexcept
size_t recalculate ()
 Recalculate only dirty formula cells.
size_t recalculateAll ()
 Mark all formulas dirty, then recalculate.
void markDirty (const XLCellKey &cell, bool propagate)
 Mark a cell dirty; with propagateDirty, also all transitive dependents.
void markDirty (const XLCellKey &cell)
void markDirty (std::string_view a1, bool propagate)
void markDirty (std::string_view a1)
void markAllDirty ()
void setInputValue (const XLCellKey &cell, const XLCellValue &value)
 Update an input value and dirty all dependent formulas.
void setInputValue (std::string_view a1, const XLCellValue &value)
void notifyChanged (const XLCellKey &cell)
 Notify that a cell changed externally (same as markDirty with propagation).
void notifyChanged (std::string_view a1)
void clearCache ()
void setDefinedNames (std::unordered_map< std::string, std::string > names)
 Inject defined names for map-mode engines (name → refersTo formula, e.g. "Sales" → "=$B$2").
void reloadDefinedNames ()
 Reload defined names from the bound workbook (sheet/document engines).
bool updateFormulaCell (const XLCellKey &cell)
 Locally update one formula cell in the graph without full rebuild.
bool updateFormulaCell (std::string_view a1)

Static Public Member Functions

static std::vector< XLCellKey > extractDependencies (std::string_view formula, std::string_view defaultSheet={}, size_t maxExpanded=4096, bool forceDefaultSheet=false, const std::unordered_map< std::string, std::string > *definedNames=nullptr)
 Extract cell/range dependencies from a formula string (public for tests).

Detailed Description

Sheet- or workbook-scoped formula calculation engine (Phase C).

Builds a dependency graph over formula cells, evaluates with caching and circular detection, propagates dirty flags to dependents, and can recalculate only dirty cells or the full graph.

calc.recalculateAll();
calc.setInputValue("A1", 99); // dirties B1, C1, …
calc.recalculate(); // only dirty formulas
XLCalculationEngine book(doc); // all worksheets
book.recalculateAll();
XLCalculationEngine(XLWorksheet &worksheet, XLCalculationOptions options={})
Definition XLCalculationEngine.cpp:273

Member Typedef Documentation

◆ FormulaMap

using OpenXLSX::XLCalculationEngine::FormulaMap = std::unordered_map<std::string, std::string>

"A1" or "Sheet2!A1" -> formula

◆ ValueMap

using OpenXLSX::XLCalculationEngine::ValueMap = std::unordered_map<std::string, XLCellValue>

Constructor & Destructor Documentation

◆ XLCalculationEngine() [1/5]

OpenXLSX::XLCalculationEngine::XLCalculationEngine ( XLWorksheet & worksheet,
XLCalculationOptions options = {} )
explicit

Single worksheet (sheet-local graph; empty sheet keys).

◆ XLCalculationEngine() [2/5]

OpenXLSX::XLCalculationEngine::XLCalculationEngine ( XLDocument & document,
XLCalculationOptions options = {} )
explicit

All worksheets in a document (multi-sheet graph; keys always carry sheet name).

Note
The document must outlive this engine.

◆ XLCalculationEngine() [3/5]

OpenXLSX::XLCalculationEngine::XLCalculationEngine ( FormulaMap formulas,
ValueMap values,
XLCalculationOptions options = {},
std::string defaultSheet = {} )

In-memory model for unit tests.

Parameters
formulasMap of address -> formula. Use "Sheet!A1" for multi-sheet graphs.
valuesConstant / seed values (same key conventions).
defaultSheetOptional default sheet name for unqualified addresses.

◆ ~XLCalculationEngine()

OpenXLSX::XLCalculationEngine::~XLCalculationEngine ( )

◆ XLCalculationEngine() [4/5]

OpenXLSX::XLCalculationEngine::XLCalculationEngine ( const XLCalculationEngine & )
delete

◆ XLCalculationEngine() [5/5]

OpenXLSX::XLCalculationEngine::XLCalculationEngine ( XLCalculationEngine && )
delete

Member Function Documentation

◆ calcCellValue() [1/2]

XLCellValue OpenXLSX::XLCalculationEngine::calcCellValue ( const XLCellKey & cell)
nodiscard

Evaluate one cell, recursively calculating precedent formula cells.

Returns
Computed value; circular refs return Error with circularErrorToken.

◆ calcCellValue() [2/2]

XLCellValue OpenXLSX::XLCalculationEngine::calcCellValue ( std::string_view a1)
nodiscard

◆ clearCache()

void OpenXLSX::XLCalculationEngine::clearCache ( )

Clear evaluation caches and visit colours (keeps formula text / deps).

◆ dependencies() [1/2]

std::vector< XLCellKey > OpenXLSX::XLCalculationEngine::dependencies ( const XLCellKey & cell) const
nodiscard

Direct precedents (cells this formula reads).

◆ dependencies() [2/2]

std::vector< XLCellKey > OpenXLSX::XLCalculationEngine::dependencies ( std::string_view a1) const
nodiscard

◆ dependents() [1/2]

std::vector< XLCellKey > OpenXLSX::XLCalculationEngine::dependents ( const XLCellKey & cell) const
nodiscard

Direct dependents (formulas that read this cell). Includes non-formula seeds as keys.

◆ dependents() [2/2]

std::vector< XLCellKey > OpenXLSX::XLCalculationEngine::dependents ( std::string_view a1) const
nodiscard

◆ dirtyCount()

size_t OpenXLSX::XLCalculationEngine::dirtyCount ( ) const
nodiscardnoexcept

Number of formula cells currently marked dirty.

◆ extractDependencies()

std::vector< XLCellKey > OpenXLSX::XLCalculationEngine::extractDependencies ( std::string_view formula,
std::string_view defaultSheet = {},
size_t maxExpanded = 4096,
bool forceDefaultSheet = false,
const std::unordered_map< std::string, std::string > * definedNames = nullptr )
staticnodiscard

Extract cell/range dependencies from a formula string (public for tests).

Parameters
forceDefaultSheetWhen true, unqualified refs get sheet=defaultSheet.
definedNamesOptional uppercase-name → refersTo map for expanding named ranges.

◆ formulaCount()

size_t OpenXLSX::XLCalculationEngine::formulaCount ( ) const
inlinenodiscardnoexcept

◆ isMultiSheet()

bool OpenXLSX::XLCalculationEngine::isMultiSheet ( ) const
inlinenodiscardnoexcept

◆ lastStatus()

XLCalcStatus OpenXLSX::XLCalculationEngine::lastStatus ( ) const
inlinenodiscardnoexcept

◆ markAllDirty()

void OpenXLSX::XLCalculationEngine::markAllDirty ( )

◆ markDirty() [1/4]

void OpenXLSX::XLCalculationEngine::markDirty ( const XLCellKey & cell)

◆ markDirty() [2/4]

void OpenXLSX::XLCalculationEngine::markDirty ( const XLCellKey & cell,
bool propagate )

Mark a cell dirty; with propagateDirty, also all transitive dependents.

Parameters
cellAny cell (input seed or formula).
propagateOverride options.propagateDirty when set.

◆ markDirty() [3/4]

void OpenXLSX::XLCalculationEngine::markDirty ( std::string_view a1)

◆ markDirty() [4/4]

void OpenXLSX::XLCalculationEngine::markDirty ( std::string_view a1,
bool propagate )

◆ notifyChanged() [1/2]

void OpenXLSX::XLCalculationEngine::notifyChanged ( const XLCellKey & cell)

Notify that a cell changed externally (same as markDirty with propagation).

◆ notifyChanged() [2/2]

void OpenXLSX::XLCalculationEngine::notifyChanged ( std::string_view a1)

◆ operator=() [1/2]

XLCalculationEngine & OpenXLSX::XLCalculationEngine::operator= ( const XLCalculationEngine & )
delete

◆ operator=() [2/2]

XLCalculationEngine & OpenXLSX::XLCalculationEngine::operator= ( XLCalculationEngine && )
delete

◆ options()

const XLCalculationOptions & OpenXLSX::XLCalculationEngine::options ( ) const
inlinenodiscardnoexcept

◆ rebuild()

void OpenXLSX::XLCalculationEngine::rebuild ( )

(Re)scan formula cells and rebuild dependency + reverse-dependency graphs.

◆ recalculate()

size_t OpenXLSX::XLCalculationEngine::recalculate ( )

Recalculate only dirty formula cells.

Returns
Number of formula cells successfully evaluated (non-circular).

◆ recalculateAll()

size_t OpenXLSX::XLCalculationEngine::recalculateAll ( )

Mark all formulas dirty, then recalculate.

◆ reloadDefinedNames()

void OpenXLSX::XLCalculationEngine::reloadDefinedNames ( )

Reload defined names from the bound workbook (sheet/document engines).

◆ setDefinedNames()

void OpenXLSX::XLCalculationEngine::setDefinedNames ( std::unordered_map< std::string, std::string > names)

Inject defined names for map-mode engines (name → refersTo formula, e.g. "Sales" → "=$B$2").

Names are matched case-insensitively. Call before rebuild() or rebuild after set.

◆ setInputValue() [1/2]

void OpenXLSX::XLCalculationEngine::setInputValue ( const XLCellKey & cell,
const XLCellValue & value )

Update an input value and dirty all dependent formulas.

Writes to the value map or worksheet cell, then markDirty(propagate).

◆ setInputValue() [2/2]

void OpenXLSX::XLCalculationEngine::setInputValue ( std::string_view a1,
const XLCellValue & value )

◆ setOptions()

void OpenXLSX::XLCalculationEngine::setOptions ( XLCalculationOptions options)
inline

◆ updateFormulaCell() [1/2]

bool OpenXLSX::XLCalculationEngine::updateFormulaCell ( const XLCellKey & cell)

Locally update one formula cell in the graph without full rebuild.

Reads the live formula from the worksheet/map; removes the node if no formula.

Returns
true if the node exists after the update (still a formula cell).

◆ updateFormulaCell() [2/2]

bool OpenXLSX::XLCalculationEngine::updateFormulaCell ( std::string_view a1)

The documentation for this class was generated from the following files: