11#ifndef OPENXLSX_XLCALCULATIONENGINE_HPP
12#define OPENXLSX_XLCALCULATIONENGINE_HPP
16# pragma warning(disable : 4251)
17# pragma warning(disable : 4275)
20#include "OpenXLSX-Exports.hpp"
29#include <unordered_map>
30#include <unordered_set>
51 [[nodiscard]]
bool valid() const noexcept {
return row > 0 &&
col > 0; }
55 return row == other.row &&
col == other.col &&
sheet == other.sheet;
58 [[nodiscard]] std::string address()
const;
59 [[nodiscard]] std::string qualifiedAddress()
const;
67 [[nodiscard]]
static XLCellKey parse(std::string_view ref,
68 std::string_view defaultSheet = {},
69 bool forceDefaultSheet =
false);
153 using FormulaMap = std::unordered_map<std::string, std::string>;
154 using ValueMap = std::unordered_map<std::string, XLCellValue>;
171 XLCalculationEngine(FormulaMap formulas,
173 XLCalculationOptions options = {},
174 std::string defaultSheet = {});
176 ~XLCalculationEngine();
186 [[nodiscard]]
bool isMultiSheet() const noexcept {
return m_multiSheet; }
191 [[nodiscard]]
size_t formulaCount() const noexcept {
return m_nodes.size(); }
194 [[nodiscard]]
size_t dirtyCount() const noexcept;
198 [[nodiscard]]
std::vector<
XLCellKey> dependencies(
std::string_view a1) const;
202 [[nodiscard]]
std::vector<
XLCellKey> dependents(
std::string_view a1) const;
217 size_t recalculate();
222 size_t recalculateAll();
229 void markDirty(
const XLCellKey& cell,
bool propagate);
231 void markDirty(std::string_view a1,
bool propagate);
232 void markDirty(std::string_view a1);
241 void setInputValue(std::string_view a1,
const XLCellValue& value);
246 void notifyChanged(
const XLCellKey& cell);
247 void notifyChanged(std::string_view a1);
256 void setDefinedNames(std::unordered_map<std::string, std::string> names);
261 void reloadDefinedNames();
268 bool updateFormulaCell(
const XLCellKey& cell);
269 bool updateFormulaCell(std::string_view a1);
276 [[nodiscard]]
static std::vector<XLCellKey> extractDependencies(
278 std::string_view defaultSheet = {},
279 size_t maxExpanded = 4096,
280 bool forceDefaultSheet =
false,
281 const std::unordered_map<std::string, std::string>* definedNames =
nullptr);
284 enum class VisitColor : uint8_t { White = 0, Gray, Black };
289 std::vector<XLCellKey> deps;
291 bool hasCache{
false};
293 VisitColor color{VisitColor::White};
296 XLCellValue calcCellValueImpl(
const XLCellKey& cell,
int depth);
297 XLCellValue readInputValue(
const XLCellKey& cell)
const;
298 void writeBackValue(
const XLCellKey& cell,
const XLCellValue& value);
299 void rebuildFromWorksheet();
300 void rebuildFromDocument();
301 void rebuildFromMaps();
302 void rebuildDependentsIndex();
303 void collectFormulasFromSheet(XLWorksheet& wks,
const std::string& sheetName);
304 XLCellKey makeKey(uint32_t row, uint16_t col, std::string_view sheet = {})
const;
305 XLCellKey normalizeLookupKey(
const XLCellKey& key)
const;
306 XLCellValue circularErrorValue()
const;
307 void maybeSetFullCalcOnLoad();
308 size_t recalculateKeys(
const std::vector<XLCellKey>& keys);
309 void attachDocumentListener();
310 void detachDocumentListener();
311 void onExternalCellChange(std::string_view sheet, uint32_t row, uint16_t col,
bool formulaChanged);
312 std::vector<XLCellKey> extractDepsForContext(std::string_view formula, std::string_view homeSheet)
const;
313 std::string readLiveFormula(
const XLCellKey& key)
const;
315 XLWorksheet* m_worksheet{
nullptr};
316 XLDocument* m_document{
nullptr};
317 std::string m_defaultSheet;
318 FormulaMap m_mapFormulas;
319 ValueMap m_mapValues;
320 bool m_useMaps{
false};
321 bool m_multiSheet{
false};
322 bool m_listenerAttached{
false};
323 XLCalculationOptions m_options{};
324 XLFormulaEngine m_engine{};
325 std::unordered_map<std::string, std::string> m_definedNames;
327 std::unordered_map<XLCellKey, Node, XLCellKeyHash> m_nodes;
328 std::unordered_map<XLCellKey, std::vector<XLCellKey>, XLCellKeyHash> m_dependents;
330 bool m_circularSeen{
false};
331 bool m_suppressExternalDirty{
false};
Sheet- or workbook-scoped formula calculation engine (Phase C).
Definition XLCalculationEngine.hpp:151
const XLCalculationOptions & options() const noexcept
Definition XLCalculationEngine.hpp:183
std::unordered_map< std::string, std::string > FormulaMap
"A1" or "Sheet2!A1" -> formula
Definition XLCalculationEngine.hpp:153
std::unordered_map< std::string, XLCellValue > ValueMap
Definition XLCalculationEngine.hpp:154
void setOptions(XLCalculationOptions options)
Definition XLCalculationEngine.hpp:184
size_t formulaCount() const noexcept
Definition XLCalculationEngine.hpp:191
bool isMultiSheet() const noexcept
Definition XLCalculationEngine.hpp:186
XLCalcStatus lastStatus() const noexcept
Definition XLCalculationEngine.hpp:211
XLCalculationEngine & operator=(const XLCalculationEngine &)=delete
XLCalculationEngine(XLWorksheet &worksheet, XLCalculationOptions options={})
Definition XLCalculationEngine.cpp:273
XLCalculationEngine & operator=(XLCalculationEngine &&)=delete
XLCalculationEngine(XLCalculationEngine &&)=delete
XLCalculationEngine(const XLCalculationEngine &)=delete
Class encapsulating a cell value.
Definition XLCellValue.hpp:89
This class encapsulates the concept of an excel file. It is different from the XLWorkbook,...
Definition XLDocument.hpp:97
A class encapsulating an Excel worksheet. Access to XLWorksheet objects should be via the workbook ob...
Definition XLWorksheet.hpp:132
Definition IXLCellProvider.hpp:24
XLCalcStatus
Result status for a single calcCellValue / recalculate step.
Definition XLCalculationEngine.hpp:126
@ Error
Definition XLCalculationEngine.hpp:129
@ Ok
Definition XLCalculationEngine.hpp:127
@ Circular
Definition XLCalculationEngine.hpp:128
@ Empty
Definition XLCalculationEngine.hpp:130
Definition XLCellIterator.hpp:131
Options for workbook/sheet formula recalculation (Phase C / C-deep).
Definition XLCalculationEngine.hpp:81
size_t maxExpandedDeps
Cap on expanded range dependencies per formula.
Definition XLCalculationEngine.hpp:93
std::string circularErrorToken
Error token returned for circular references (default Excel-ish CIRCREF!).
Definition XLCalculationEngine.hpp:110
bool autoTrackChanges
When true (default), register with XLDocument to auto-dirty on cell value/formula writes.
Definition XLCalculationEngine.hpp:115
bool useDefinedNames
Load workbook defined names into the dependency extractor (default true for sheet/doc engines).
Definition XLCalculationEngine.hpp:120
int maxDepth
Definition XLCalculationEngine.hpp:86
bool writeBack
Definition XLCalculationEngine.hpp:83
bool setFullCalcOnLoad
After recalculate / recalculateAll on a document-backed engine, set the workbook fullCalcOnLoad flag ...
Definition XLCalculationEngine.hpp:104
bool propagateDirty
When markDirty is called, also dirty all transitive dependents.
Definition XLCalculationEngine.hpp:98
Definition XLCalculationEngine.hpp:73
size_t operator()(const XLCellKey &k) const noexcept
Definition XLCalculationEngine.cpp:111
Normalized cell identity used by the calculation graph.
Definition XLCalculationEngine.hpp:46
uint32_t row
Definition XLCalculationEngine.hpp:48
std::string sheet
Definition XLCalculationEngine.hpp:47
bool valid() const noexcept
Definition XLCalculationEngine.hpp:51
bool operator==(const XLCellKey &other) const noexcept
Definition XLCalculationEngine.hpp:53
uint16_t col
Definition XLCalculationEngine.hpp:49