OpenXLSX 1.12.1
Modern C++ Excel Library (Fork of OpenXLSX by Kenneth Troldal Balslev)
Loading...
Searching...
No Matches
XLCalculationEngine.hpp
Go to the documentation of this file.
1/*
2 * OpenXLSX-NX - Modern High-Performance C++ Excel Library
3 *
4 * Original work Copyright (c) 2018-2025, Kenneth Troldal Balslev and OpenXLSX contributors
5 * Modified work Copyright (c) 2026, Curry Tang and OpenXLSX-NX contributors
6 *
7 * Distributed under the BSD 3-Clause License.
8 * See LICENSE.md in the project root or https://opensource.org/licenses/BSD-3-Clause
9 */
10
11#ifndef OPENXLSX_XLCALCULATIONENGINE_HPP
12#define OPENXLSX_XLCALCULATIONENGINE_HPP
13
14#ifdef _MSC_VER
15# pragma warning(push)
16# pragma warning(disable : 4251)
17# pragma warning(disable : 4275)
18#endif
19
20#include "OpenXLSX-Exports.hpp"
21#include "XLCellValue.hpp"
22#include "XLFormulaEngine.hpp"
23
24#include <cstddef>
25#include <cstdint>
26#include <queue>
27#include <string>
28#include <string_view>
29#include <unordered_map>
30#include <unordered_set>
31#include <utility>
32#include <vector>
33
34namespace OpenXLSX
35{
36 class XLWorksheet;
37 class XLDocument;
38
45 struct OPENXLSX_EXPORT XLCellKey
46 {
47 std::string sheet;
48 uint32_t row{0};
49 uint16_t col{0};
50
51 [[nodiscard]] bool valid() const noexcept { return row > 0 && col > 0; }
52
53 [[nodiscard]] bool operator==(const XLCellKey& other) const noexcept
54 {
55 return row == other.row && col == other.col && sheet == other.sheet;
56 }
57
58 [[nodiscard]] std::string address() const;
59 [[nodiscard]] std::string qualifiedAddress() const;
60
67 [[nodiscard]] static XLCellKey parse(std::string_view ref,
68 std::string_view defaultSheet = {},
69 bool forceDefaultSheet = false);
70 };
71
72 struct OPENXLSX_EXPORT XLCellKeyHash
73 {
74 size_t operator()(const XLCellKey& k) const noexcept;
75 };
76
80 struct OPENXLSX_EXPORT XLCalculationOptions
81 {
83 bool writeBack{true};
84
86 int maxDepth{256};
87
93 size_t maxExpandedDeps{4096};
94
98 bool propagateDirty{true};
99
104 bool setFullCalcOnLoad{false};
105
110 std::string circularErrorToken{"#CIRCREF!"};
111
116
120 bool useDefinedNames{true};
121 };
122
126 enum class XLCalcStatus : uint8_t {
127 Ok = 0,
131 };
132
150 class OPENXLSX_EXPORT XLCalculationEngine
151 {
152 public:
153 using FormulaMap = std::unordered_map<std::string, std::string>;
154 using ValueMap = std::unordered_map<std::string, XLCellValue>;
155
158
163 explicit XLCalculationEngine(XLDocument& document, XLCalculationOptions options = {});
164
171 XLCalculationEngine(FormulaMap formulas,
172 ValueMap values,
173 XLCalculationOptions options = {},
174 std::string defaultSheet = {});
175
176 ~XLCalculationEngine();
177
182
183 [[nodiscard]] const XLCalculationOptions& options() const noexcept { return m_options; }
184 void setOptions(XLCalculationOptions options) { m_options = std::move(options); }
185
186 [[nodiscard]] bool isMultiSheet() const noexcept { return m_multiSheet; }
187
189 void rebuild();
190
191 [[nodiscard]] size_t formulaCount() const noexcept { return m_nodes.size(); }
192
194 [[nodiscard]] size_t dirtyCount() const noexcept;
195
197 [[nodiscard]] std::vector<XLCellKey> dependencies(const XLCellKey& cell) const;
198 [[nodiscard]] std::vector<XLCellKey> dependencies(std::string_view a1) const;
199
201 [[nodiscard]] std::vector<XLCellKey> dependents(const XLCellKey& cell) const;
202 [[nodiscard]] std::vector<XLCellKey> dependents(std::string_view a1) const;
203
208 [[nodiscard]] XLCellValue calcCellValue(const XLCellKey& cell);
209 [[nodiscard]] XLCellValue calcCellValue(std::string_view a1);
210
211 [[nodiscard]] XLCalcStatus lastStatus() const noexcept { return m_lastStatus; }
212
217 size_t recalculate();
218
222 size_t recalculateAll();
223
229 void markDirty(const XLCellKey& cell, bool propagate);
230 void markDirty(const XLCellKey& cell);
231 void markDirty(std::string_view a1, bool propagate);
232 void markDirty(std::string_view a1);
233
234 void markAllDirty();
235
240 void setInputValue(const XLCellKey& cell, const XLCellValue& value);
241 void setInputValue(std::string_view a1, const XLCellValue& value);
242
246 void notifyChanged(const XLCellKey& cell);
247 void notifyChanged(std::string_view a1);
248
250 void clearCache();
251
256 void setDefinedNames(std::unordered_map<std::string, std::string> names);
257
261 void reloadDefinedNames();
262
268 bool updateFormulaCell(const XLCellKey& cell);
269 bool updateFormulaCell(std::string_view a1);
270
276 [[nodiscard]] static std::vector<XLCellKey> extractDependencies(
277 std::string_view formula,
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);
282
283 private:
284 enum class VisitColor : uint8_t { White = 0, Gray, Black };
285
286 struct Node
287 {
288 std::string formula;
289 std::vector<XLCellKey> deps;
290 XLCellValue cached;
291 bool hasCache{false};
292 bool dirty{true};
293 VisitColor color{VisitColor::White};
294 };
295
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;
314
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;
326
327 std::unordered_map<XLCellKey, Node, XLCellKeyHash> m_nodes;
328 std::unordered_map<XLCellKey, std::vector<XLCellKey>, XLCellKeyHash> m_dependents;
329 XLCalcStatus m_lastStatus{XLCalcStatus::Empty};
330 bool m_circularSeen{false};
331 bool m_suppressExternalDirty{false};
332 };
333
334} // namespace OpenXLSX
335
336#ifdef _MSC_VER
337# pragma warning(pop)
338#endif
339
340#endif // OPENXLSX_XLCALCULATIONENGINE_HPP
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 XLFormulaUtils.hpp:37
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