OpenXLSX 1.10.0
Loading...
Searching...
No Matches
XLFormulaEngine.hpp
Go to the documentation of this file.
1#ifndef OPENXLSX_XLFORMULAENGINE_HPP
2#define OPENXLSX_XLFORMULAENGINE_HPP
3
4#ifdef _MSC_VER
5# pragma warning(push)
6# pragma warning(disable : 4251)
7# pragma warning(disable : 4275)
8#endif
9
10// ===== Standard Library ===== //
11#include <functional>
12#include <memory>
13#include <string>
14#include <string_view>
15#include <unordered_map>
16#include <vector>
17
18// ===== GSL ===== //
19#include <gsl/gsl>
20
21// ===== OpenXLSX Includes ===== //
22#include "OpenXLSX-Exports.hpp"
23#include "XLCellReference.hpp"
24#include "XLCellValue.hpp"
25
26// Forward declare XLWorksheet so callers can use makeResolver without pulling the full header.
27namespace OpenXLSX
28{
29 class XLWorksheet;
30}
31
32namespace OpenXLSX
33{
34 // =========================================================================
35 // Lexer
36 // =========================================================================
37
41 enum class XLTokenKind : uint8_t {
42 Number,
43 String,
44 Bool,
45 CellRef,
46 Ident,
47 Plus,
48 Minus,
49 Star,
50 Slash,
51 Caret,
52 Percent,
53 Amp,
54 Eq,
55 NEq,
56 Lt,
57 Le,
58 Gt,
59 Ge,
60 LParen,
61 RParen,
62 Comma,
63 Semicolon,
64 Colon,
65 End,
66 Error
67 };
68
72 struct OPENXLSX_EXPORT XLToken
73 {
74 XLTokenKind kind{XLTokenKind::Error};
75 std::string text;
76 double number{0.0};
77 bool boolean{false};
78 };
79
86 class OPENXLSX_EXPORT XLFormulaLexer
87 {
88 public:
94 static std::vector<XLToken> tokenize(std::string_view formula);
95
96 private:
97 // Stateless helper – everything runs in tokenize().
98 };
99
100 // =========================================================================
101 // AST
102 // =========================================================================
103
105 enum class XLNodeKind : uint8_t {
106 Number,
107 StringLit,
108 BoolLit,
109 CellRef,
110 Range,
111 BinOp,
112 UnaryOp,
113 FuncCall,
114 ErrorLit
115 };
116
121 struct OPENXLSX_EXPORT XLASTNode
122 {
124
125 // ---- Leaf payloads ----
126 double number{0.0};
127 std::string text;
128 bool boolean{false};
129 XLTokenKind op{XLTokenKind::Error};
130
131 // ---- Children ----
132 std::vector<std::unique_ptr<XLASTNode>> children;
133
134 explicit XLASTNode(XLNodeKind k) : kind(k) {}
135
136 // Non-copyable due to unique_ptr children; movable.
137 XLASTNode(const XLASTNode&) = delete;
138 XLASTNode& operator=(const XLASTNode&) = delete;
139 XLASTNode(XLASTNode&&) = default;
141 };
142
143 // =========================================================================
144 // Parser
145 // =========================================================================
146
150 class OPENXLSX_EXPORT XLFormulaParser
151 {
152 public:
159 static std::unique_ptr<XLASTNode> parse(gsl::span<const XLToken> tokens);
160
161 private:
162 // All state is local to the recursive parse calls below.
163 struct ParseContext
164 {
165 gsl::span<const XLToken> tokens;
166 std::size_t pos{0};
167
168 [[nodiscard]] const XLToken& current() const;
169 [[nodiscard]] const XLToken& peek(std::size_t offset = 1) const;
170 const XLToken& consume();
171 bool matchKind(XLTokenKind k);
172 };
173
174 static std::unique_ptr<XLASTNode> parseExpr(ParseContext& ctx, int minPrec = 0);
175 static std::unique_ptr<XLASTNode> parseUnary(ParseContext& ctx);
176 static std::unique_ptr<XLASTNode> parsePrimary(ParseContext& ctx);
177 static std::unique_ptr<XLASTNode> parseFuncCall(std::string name, ParseContext& ctx);
178
179 static int precedence(XLTokenKind k);
180 static bool isRightAssoc(XLTokenKind k);
181 };
182
183 // =========================================================================
184 // Evaluator / Engine
185 // =========================================================================
186
198 using XLCellResolver = std::function<XLCellValue(std::string_view ref)>;
199
214 class OPENXLSX_EXPORT XLFormulaArg
215 {
216 public:
217 enum class Type { Empty, Scalar, Array, LazyRange };
218
219 private:
220 Type m_type{Type::Empty};
221 XLCellValue m_scalar;
222 std::vector<XLCellValue> m_array;
223 uint32_t m_r1{0}, m_r2{0};
224 uint16_t m_c1{0}, m_c2{0};
225 std::string m_sheetName;
226 const std::function<XLCellValue(std::string_view)>* m_resolver{nullptr};
227
228 public:
229 XLFormulaArg() = default;
230 XLFormulaArg(XLCellValue v) : m_type(Type::Scalar), m_scalar(std::move(v)) {}
231 XLFormulaArg(std::vector<XLCellValue> arr) : m_type(Type::Array), m_array(std::move(arr)) {}
232 XLFormulaArg(uint32_t r1,
233 uint32_t r2,
234 uint16_t c1,
235 uint16_t c2,
236 std::string sheetName,
237 const std::function<XLCellValue(std::string_view)>* resolver)
238 : m_type(Type::LazyRange),
239 m_r1(r1),
240 m_r2(r2),
241 m_c1(c1),
242 m_c2(c2),
243 m_sheetName(std::move(sheetName)),
244 m_resolver(resolver)
245 {}
246
247 Type type() const { return m_type; }
248
249 size_t rows() const
250 {
251 if (m_type == Type::LazyRange) return static_cast<size_t>(m_r2 - m_r1 + 1);
252 if (m_type == Type::Array) return m_array.size(); // simplified, arrays might be 2d but we treat as 1d column
253 return m_type == Type::Scalar ? 1 : 0;
254 }
255
256 size_t cols() const
257 {
258 if (m_type == Type::LazyRange) return static_cast<size_t>(m_c2 - m_c1 + 1);
259 return m_type == Type::Scalar || m_type == Type::Array ? 1 : 0;
260 }
261
263 uint32_t firstRow() const noexcept { return m_type == Type::LazyRange ? m_r1 : 0; }
264
266 uint16_t firstCol() const noexcept { return m_type == Type::LazyRange ? m_c1 : 0; }
267
268 bool empty() const
269 {
270 if (m_type == Type::Empty) return true;
271 if (m_type == Type::Scalar) return m_scalar.type() == XLValueType::Empty;
272 if (m_type == Type::Array) return m_array.empty();
273 return m_r1 > m_r2 || m_c1 > m_c2;
274 }
275 size_t size() const
276 {
277 if (m_type == Type::Empty) return 0;
278 if (m_type == Type::Scalar) return 1;
279 if (m_type == Type::Array) return m_array.size();
280 return static_cast<size_t>(m_r2 - m_r1 + 1) * static_cast<size_t>(m_c2 - m_c1 + 1);
281 }
282
283 XLCellValue operator[](size_t index) const
284 {
285 if (m_type == Type::Scalar) return index == 0 ? m_scalar : XLCellValue();
286 if (m_type == Type::Array) return index < m_array.size() ? m_array[index] : XLCellValue();
287 if (m_type == Type::LazyRange) {
288 uint16_t w = m_c2 - m_c1 + 1;
289 uint32_t row = m_r1 + static_cast<uint32_t>(index / w);
290 uint16_t col = m_c1 + static_cast<uint16_t>(index % w);
291 std::string ref = m_sheetName;
292 if (!ref.empty()) ref += "!";
293 ref += XLCellReference(row, col).address();
294 if (m_resolver && *m_resolver) return (*m_resolver)(ref);
295 }
296 return XLCellValue();
297 }
298
300 {
301 const XLFormulaArg* arg;
302 size_t index;
303
304 public:
305 Iterator(const XLFormulaArg* a, size_t i) : arg(a), index(i) {}
306 XLCellValue operator*() const { return (*arg)[index]; }
308 {
309 ++index;
310 return *this;
311 }
312 bool operator!=(const Iterator& other) const { return index != other.index; }
313 };
314 Iterator begin() const { return Iterator(this, 0); }
315 Iterator end() const { return Iterator(this, size()); }
316 };
317
318 class OPENXLSX_EXPORT XLFormulaEngine
319 {
320 public:
322 ~XLFormulaEngine() = default;
323
328
336 [[nodiscard]] XLCellValue evaluate(std::string_view formula, const XLCellResolver& resolver = {}) const;
337
344 [[nodiscard]] static XLCellResolver makeResolver(const XLWorksheet& wks);
345
346 private:
347 // ---- Internal evaluation helpers ----
348
352 static XLFormulaArg expandRange(std::string_view rangeRef, const XLCellResolver& resolver);
353
357 static std::vector<double> collectNumbers(const std::vector<XLCellValue>& flat, bool countBlanks = false);
358
363 [[nodiscard]] XLCellValue evalNode(const XLASTNode& node, const XLCellResolver& resolver) const;
364
369 [[nodiscard]] XLFormulaArg expandArg(const XLASTNode& argNode, const XLCellResolver& resolver) const;
370
371 // ---- Built-in function table ----
372 // Each entry maps an uppercase function name to its implementation.
373 using FuncArgs = std::vector<XLCellValue>;
374 using FuncImpl = std::function<XLCellValue(const std::vector<XLFormulaArg>&)>;
375 static const std::unordered_map<std::string, FuncImpl>& getBuiltins();
376
377 // ---- Helpers registered as lambdas in getBuiltins() ----
378 static XLCellValue fnSum(const std::vector<XLFormulaArg>& args);
379 static XLCellValue fnAverage(const std::vector<XLFormulaArg>& args);
380 static XLCellValue fnMin(const std::vector<XLFormulaArg>& args);
381 static XLCellValue fnMax(const std::vector<XLFormulaArg>& args);
382 static XLCellValue fnCount(const std::vector<XLFormulaArg>& args);
383 static XLCellValue fnCounta(const std::vector<XLFormulaArg>& args);
384 static XLCellValue fnIf(const std::vector<XLFormulaArg>& args);
385 static XLCellValue fnIfs(const std::vector<XLFormulaArg>& args);
386 static XLCellValue fnSwitch(const std::vector<XLFormulaArg>& args);
387 static XLCellValue fnAnd(const std::vector<XLFormulaArg>& args);
388 static XLCellValue fnOr(const std::vector<XLFormulaArg>& args);
389 static XLCellValue fnNot(const std::vector<XLFormulaArg>& args);
390 static XLCellValue fnIferror(const std::vector<XLFormulaArg>& args);
391 static XLCellValue fnAbs(const std::vector<XLFormulaArg>& args);
392 static XLCellValue fnRound(const std::vector<XLFormulaArg>& args);
393 static XLCellValue fnRoundup(const std::vector<XLFormulaArg>& args);
394 static XLCellValue fnRounddown(const std::vector<XLFormulaArg>& args);
395 static XLCellValue fnSqrt(const std::vector<XLFormulaArg>& args);
396 static XLCellValue fnPi(const std::vector<XLFormulaArg>& args);
397 static XLCellValue fnSin(const std::vector<XLFormulaArg>& args);
398 static XLCellValue fnCos(const std::vector<XLFormulaArg>& args);
399 static XLCellValue fnTan(const std::vector<XLFormulaArg>& args);
400 static XLCellValue fnAsin(const std::vector<XLFormulaArg>& args);
401 static XLCellValue fnAcos(const std::vector<XLFormulaArg>& args);
402 static XLCellValue fnDegrees(const std::vector<XLFormulaArg>& args);
403 static XLCellValue fnRadians(const std::vector<XLFormulaArg>& args);
404 static XLCellValue fnRand(const std::vector<XLFormulaArg>& args);
405 static XLCellValue fnRandbetween(const std::vector<XLFormulaArg>& args);
406 static XLCellValue fnInt(const std::vector<XLFormulaArg>& args);
407 static XLCellValue fnMod(const std::vector<XLFormulaArg>& args);
408 static XLCellValue fnPower(const std::vector<XLFormulaArg>& args);
409 static XLCellValue fnVlookup(const std::vector<XLFormulaArg>& args);
410 static XLCellValue fnHlookup(const std::vector<XLFormulaArg>& args);
411 static XLCellValue fnXlookup(const std::vector<XLFormulaArg>& args);
412 static XLCellValue fnIndex(const std::vector<XLFormulaArg>& args);
413 static XLCellValue fnMatch(const std::vector<XLFormulaArg>& args);
414 static XLCellValue fnConcatenate(const std::vector<XLFormulaArg>& args);
415 static XLCellValue fnLen(const std::vector<XLFormulaArg>& args);
416 static XLCellValue fnLeft(const std::vector<XLFormulaArg>& args);
417 static XLCellValue fnRight(const std::vector<XLFormulaArg>& args);
418 static XLCellValue fnMid(const std::vector<XLFormulaArg>& args);
419 static XLCellValue fnUpper(const std::vector<XLFormulaArg>& args);
420 static XLCellValue fnLower(const std::vector<XLFormulaArg>& args);
421 static XLCellValue fnTrim(const std::vector<XLFormulaArg>& args);
422 static XLCellValue fnText(const std::vector<XLFormulaArg>& args);
423 static XLCellValue fnIsnumber(const std::vector<XLFormulaArg>& args);
424 static XLCellValue fnIsblank(const std::vector<XLFormulaArg>& args);
425 static XLCellValue fnIserror(const std::vector<XLFormulaArg>& args);
426 static XLCellValue fnIstext(const std::vector<XLFormulaArg>& args);
427
428 // ---- Date / Time ----
429 static XLCellValue fnToday(const std::vector<XLFormulaArg>& args);
430 static XLCellValue fnNow(const std::vector<XLFormulaArg>& args);
431 static XLCellValue fnDate(const std::vector<XLFormulaArg>& args);
432 static XLCellValue fnTime(const std::vector<XLFormulaArg>& args);
433 static XLCellValue fnYear(const std::vector<XLFormulaArg>& args);
434 static XLCellValue fnMonth(const std::vector<XLFormulaArg>& args);
435 static XLCellValue fnDay(const std::vector<XLFormulaArg>& args);
436 static XLCellValue fnHour(const std::vector<XLFormulaArg>& args);
437 static XLCellValue fnMinute(const std::vector<XLFormulaArg>& args);
438 static XLCellValue fnSecond(const std::vector<XLFormulaArg>& args);
439 static XLCellValue fnDays(const std::vector<XLFormulaArg>& args);
440 static XLCellValue fnWeekday(const std::vector<XLFormulaArg>& args);
441 static XLCellValue fnEdate(const std::vector<XLFormulaArg>& args);
442 static XLCellValue fnEomonth(const std::vector<XLFormulaArg>& args);
443 static XLCellValue fnWorkday(const std::vector<XLFormulaArg>& args);
444 static XLCellValue fnNetworkdays(const std::vector<XLFormulaArg>& args);
445
446 // ---- Financial ----
447 static XLCellValue fnPmt(const std::vector<XLFormulaArg>& args);
448 static XLCellValue fnFv(const std::vector<XLFormulaArg>& args);
449 static XLCellValue fnPv(const std::vector<XLFormulaArg>& args);
450 static XLCellValue fnNpv(const std::vector<XLFormulaArg>& args);
451
452 // ---- Math extended ----
453 static XLCellValue fnSumproduct(const std::vector<XLFormulaArg>& args);
454 static XLCellValue fnCeil(const std::vector<XLFormulaArg>& args);
455 static XLCellValue fnFloor(const std::vector<XLFormulaArg>& args);
456 static XLCellValue fnLog(const std::vector<XLFormulaArg>& args);
457 static XLCellValue fnLog10(const std::vector<XLFormulaArg>& args);
458 static XLCellValue fnExp(const std::vector<XLFormulaArg>& args);
459 static XLCellValue fnSign(const std::vector<XLFormulaArg>& args);
460
461 // ---- Text extended ----
462 static XLCellValue fnFind(const std::vector<XLFormulaArg>& args);
463 static XLCellValue fnSearch(const std::vector<XLFormulaArg>& args);
464 static XLCellValue fnSubstitute(const std::vector<XLFormulaArg>& args);
465 static XLCellValue fnReplace(const std::vector<XLFormulaArg>& args);
466 static XLCellValue fnRept(const std::vector<XLFormulaArg>& args);
467 static XLCellValue fnExact(const std::vector<XLFormulaArg>& args);
468 static XLCellValue fnT(const std::vector<XLFormulaArg>& args);
469 static XLCellValue fnValue(const std::vector<XLFormulaArg>& args);
470 static XLCellValue fnTextjoin(const std::vector<XLFormulaArg>& args);
471 static XLCellValue fnClean(const std::vector<XLFormulaArg>& args);
472 static XLCellValue fnProper(const std::vector<XLFormulaArg>& args);
473
474 // ---- Statistical / Conditional ----
475 static XLCellValue fnSumif(const std::vector<XLFormulaArg>& args);
476 static XLCellValue fnCountif(const std::vector<XLFormulaArg>& args);
477 static XLCellValue fnSumifs(const std::vector<XLFormulaArg>& args);
478 static XLCellValue fnCountifs(const std::vector<XLFormulaArg>& args);
479 static XLCellValue fnMaxifs(const std::vector<XLFormulaArg>& args);
480 static XLCellValue fnMinifs(const std::vector<XLFormulaArg>& args);
481 static XLCellValue fnAverageif(const std::vector<XLFormulaArg>& args);
482 static XLCellValue fnRank(const std::vector<XLFormulaArg>& args);
483 static XLCellValue fnLarge(const std::vector<XLFormulaArg>& args);
484 static XLCellValue fnSmall(const std::vector<XLFormulaArg>& args);
485 static XLCellValue fnStdev(const std::vector<XLFormulaArg>& args);
486 static XLCellValue fnVar(const std::vector<XLFormulaArg>& args);
487 static XLCellValue fnMedian(const std::vector<XLFormulaArg>& args);
488 static XLCellValue fnCountblank(const std::vector<XLFormulaArg>& args);
489
490 // ---- Info extended ----
491 static XLCellValue fnIsna(const std::vector<XLFormulaArg>& args);
492 static XLCellValue fnIfna(const std::vector<XLFormulaArg>& args);
493 static XLCellValue fnIslogical(const std::vector<XLFormulaArg>& args);
494
495 // ---- Easy Additions (Logical, Math, Stat, Text, Financial) ----
496 static XLCellValue fnTrue(const std::vector<XLFormulaArg>& args);
497 static XLCellValue fnFalse(const std::vector<XLFormulaArg>& args);
498 static XLCellValue fnIseven(const std::vector<XLFormulaArg>& args);
499 static XLCellValue fnIsodd(const std::vector<XLFormulaArg>& args);
500 static XLCellValue fnMround(const std::vector<XLFormulaArg>& args);
501 static XLCellValue fnCeilingMath(const std::vector<XLFormulaArg>& args);
502 static XLCellValue fnFloorMath(const std::vector<XLFormulaArg>& args);
503 static XLCellValue fnVarp(const std::vector<XLFormulaArg>& args);
504 static XLCellValue fnStdevp(const std::vector<XLFormulaArg>& args);
505 static XLCellValue fnVara(const std::vector<XLFormulaArg>& args);
506 static XLCellValue fnVarpa(const std::vector<XLFormulaArg>& args);
507 static XLCellValue fnStdeva(const std::vector<XLFormulaArg>& args);
508 static XLCellValue fnStdevpa(const std::vector<XLFormulaArg>& args);
509 static XLCellValue fnPermut(const std::vector<XLFormulaArg>& args);
510 static XLCellValue fnPermutationa(const std::vector<XLFormulaArg>& args);
511 static XLCellValue fnFisher(const std::vector<XLFormulaArg>& args);
512 static XLCellValue fnFisherinv(const std::vector<XLFormulaArg>& args);
513 static XLCellValue fnStandardize(const std::vector<XLFormulaArg>& args);
514 static XLCellValue fnPearson(const std::vector<XLFormulaArg>& args);
515 static XLCellValue fnRsq(const std::vector<XLFormulaArg>& args);
516 static XLCellValue fnAverageifs(const std::vector<XLFormulaArg>& args);
517 static XLCellValue fnIsoweeknum(const std::vector<XLFormulaArg>& args);
518 static XLCellValue fnWeeknum(const std::vector<XLFormulaArg>& args);
519 static XLCellValue fnDays360(const std::vector<XLFormulaArg>& args);
520 static XLCellValue fnNper(const std::vector<XLFormulaArg>& args);
521 static XLCellValue fnDb(const std::vector<XLFormulaArg>& args);
522 static XLCellValue fnDdb(const std::vector<XLFormulaArg>& args);
523 static XLCellValue fnIserr(const std::vector<XLFormulaArg>& args);
524 static XLCellValue fnTrunc(const std::vector<XLFormulaArg>& args);
525 static XLCellValue fnSumsq(const std::vector<XLFormulaArg>& args);
526 static XLCellValue fnSumx2my2(const std::vector<XLFormulaArg>& args);
527 static XLCellValue fnSumx2py2(const std::vector<XLFormulaArg>& args);
528 static XLCellValue fnSumxmy2(const std::vector<XLFormulaArg>& args);
529 static XLCellValue fnAvedev(const std::vector<XLFormulaArg>& args);
530 static XLCellValue fnDevsq(const std::vector<XLFormulaArg>& args);
531 static XLCellValue fnAveragea(const std::vector<XLFormulaArg>& args);
532 static XLCellValue fnSln(const std::vector<XLFormulaArg>& args);
533 static XLCellValue fnSyd(const std::vector<XLFormulaArg>& args);
534 static XLCellValue fnChar(const std::vector<XLFormulaArg>& args);
535 static XLCellValue fnUnichar(const std::vector<XLFormulaArg>& args);
536 static XLCellValue fnCode(const std::vector<XLFormulaArg>& args);
537 static XLCellValue fnUnicode(const std::vector<XLFormulaArg>& args);
538 static XLCellValue fnCovarianceP(const std::vector<XLFormulaArg>& args);
539 static XLCellValue fnCovarianceS(const std::vector<XLFormulaArg>& args);
540 static XLCellValue fnTrimmean(const std::vector<XLFormulaArg>& args);
541 static XLCellValue fnSlope(const std::vector<XLFormulaArg>& args);
542 static XLCellValue fnIntercept(const std::vector<XLFormulaArg>& args);
543 static XLCellValue fnPercentileInc(const std::vector<XLFormulaArg>& args);
544 static XLCellValue fnPercentileExc(const std::vector<XLFormulaArg>& args);
545 static XLCellValue fnQuartileInc(const std::vector<XLFormulaArg>& args);
546 static XLCellValue fnQuartileExc(const std::vector<XLFormulaArg>& args);
547 static XLCellValue fnIsnontext(const std::vector<XLFormulaArg>& args);
548
549 // ---- Batch 2 – Math / Trig (pure cmath wrappers) ----
550 static XLCellValue fnLn(const std::vector<XLFormulaArg>& args);
551 static XLCellValue fnAtan(const std::vector<XLFormulaArg>& args);
552 static XLCellValue fnAtan2Fn(const std::vector<XLFormulaArg>& args);
553 static XLCellValue fnSinh(const std::vector<XLFormulaArg>& args);
554 static XLCellValue fnCosh(const std::vector<XLFormulaArg>& args);
555 static XLCellValue fnTanh(const std::vector<XLFormulaArg>& args);
556 static XLCellValue fnAsinh(const std::vector<XLFormulaArg>& args);
557 static XLCellValue fnAcosh(const std::vector<XLFormulaArg>& args);
558 static XLCellValue fnAtanh(const std::vector<XLFormulaArg>& args);
559 static XLCellValue fnSqrtpi(const std::vector<XLFormulaArg>& args);
560 static XLCellValue fnFact(const std::vector<XLFormulaArg>& args);
561 static XLCellValue fnFactdouble(const std::vector<XLFormulaArg>& args);
562 static XLCellValue fnCombin(const std::vector<XLFormulaArg>& args);
563 static XLCellValue fnCombina(const std::vector<XLFormulaArg>& args);
564 static XLCellValue fnProduct(const std::vector<XLFormulaArg>& args);
565 static XLCellValue fnGcd(const std::vector<XLFormulaArg>& args);
566 static XLCellValue fnLcm(const std::vector<XLFormulaArg>& args);
567 static XLCellValue fnEven(const std::vector<XLFormulaArg>& args);
568 static XLCellValue fnOdd(const std::vector<XLFormulaArg>& args);
569 static XLCellValue fnQuotient(const std::vector<XLFormulaArg>& args);
570
571 // ---- Batch 2 – High-frequency utility ----
572 static XLCellValue fnSubtotal(const std::vector<XLFormulaArg>& args);
573 static XLCellValue fnChoose(const std::vector<XLFormulaArg>& args);
574 static XLCellValue fnRow(const std::vector<XLFormulaArg>& args);
575 static XLCellValue fnColumn(const std::vector<XLFormulaArg>& args);
576 static XLCellValue fnDatedif(const std::vector<XLFormulaArg>& args);
577 static XLCellValue fnIrr(const std::vector<XLFormulaArg>& args);
578 static XLCellValue fnMirr(const std::vector<XLFormulaArg>& args);
579 static XLCellValue fnRate(const std::vector<XLFormulaArg>& args);
580 static XLCellValue fnIpmt(const std::vector<XLFormulaArg>& args);
581 static XLCellValue fnPpmt(const std::vector<XLFormulaArg>& args);
582 static XLCellValue fnModeSngl(const std::vector<XLFormulaArg>& args);
583 static XLCellValue fnSkew(const std::vector<XLFormulaArg>& args);
584 static XLCellValue fnKurt(const std::vector<XLFormulaArg>& args);
585 static XLCellValue fnForecastLinear(const std::vector<XLFormulaArg>& args);
586
587 // ---- Batch 3 – Statistical distributions ----
588 static XLCellValue fnNormSDist(const std::vector<XLFormulaArg>& args);
589 static XLCellValue fnNormDist(const std::vector<XLFormulaArg>& args);
590 static XLCellValue fnNormSInv(const std::vector<XLFormulaArg>& args);
591 static XLCellValue fnNormInv(const std::vector<XLFormulaArg>& args);
592 static XLCellValue fnTDist(const std::vector<XLFormulaArg>& args);
593 static XLCellValue fnTDistRT(const std::vector<XLFormulaArg>& args);
594 static XLCellValue fnTDist2T(const std::vector<XLFormulaArg>& args);
595 static XLCellValue fnTInv(const std::vector<XLFormulaArg>& args);
596 static XLCellValue fnTInv2T(const std::vector<XLFormulaArg>& args);
597 static XLCellValue fnChisqDist(const std::vector<XLFormulaArg>& args);
598 static XLCellValue fnChisqDistRT(const std::vector<XLFormulaArg>& args);
599 static XLCellValue fnChisqInv(const std::vector<XLFormulaArg>& args);
600 static XLCellValue fnChisqInvRT(const std::vector<XLFormulaArg>& args);
601 static XLCellValue fnBinomDist(const std::vector<XLFormulaArg>& args);
602 static XLCellValue fnPoissonDist(const std::vector<XLFormulaArg>& args);
603 static XLCellValue fnExponDist(const std::vector<XLFormulaArg>& args);
604 };
605
606} // namespace OpenXLSX
607
608#ifdef _MSC_VER
609# pragma warning(pop)
610#endif
611
612#endif // OPENXLSX_XLFORMULAENGINE_HPP
Definition XLCellReference.hpp:34
std::string address() const
Get the address of the XLCellReference.
Definition XLCellReference.cpp:197
Class encapsulating a cell value.
Definition XLCellValue.hpp:79
Definition XLFormulaEngine.hpp:300
Iterator(const XLFormulaArg *a, size_t i)
Definition XLFormulaEngine.hpp:305
bool operator!=(const Iterator &other) const
Definition XLFormulaEngine.hpp:312
Iterator & operator++()
Definition XLFormulaEngine.hpp:307
XLCellValue operator*() const
Definition XLFormulaEngine.hpp:306
Lightweight formula evaluation engine.
Definition XLFormulaEngine.hpp:215
uint32_t firstRow() const noexcept
Return the 1-based row of the top-left cell (LazyRange only; 0 for other types).
Definition XLFormulaEngine.hpp:263
size_t rows() const
Definition XLFormulaEngine.hpp:249
XLFormulaArg(uint32_t r1, uint32_t r2, uint16_t c1, uint16_t c2, std::string sheetName, const std::function< XLCellValue(std::string_view)> *resolver)
Definition XLFormulaEngine.hpp:232
uint16_t firstCol() const noexcept
Return the 1-based column of the top-left cell (LazyRange only; 0 for other types).
Definition XLFormulaEngine.hpp:266
Iterator end() const
Definition XLFormulaEngine.hpp:315
bool empty() const
Definition XLFormulaEngine.hpp:268
XLFormulaArg(std::vector< XLCellValue > arr)
Definition XLFormulaEngine.hpp:231
XLCellValue operator[](size_t index) const
Definition XLFormulaEngine.hpp:283
size_t cols() const
Definition XLFormulaEngine.hpp:256
XLFormulaArg(XLCellValue v)
Definition XLFormulaEngine.hpp:230
size_t size() const
Definition XLFormulaEngine.hpp:275
Iterator begin() const
Definition XLFormulaEngine.hpp:314
Type type() const
Definition XLFormulaEngine.hpp:247
Type
Definition XLFormulaEngine.hpp:217
Definition XLFormulaEngine.hpp:319
XLFormulaEngine & operator=(const XLFormulaEngine &)=delete
XLFormulaEngine(XLFormulaEngine &&)=default
XLFormulaEngine(const XLFormulaEngine &)=delete
XLFormulaEngine & operator=(XLFormulaEngine &&)=default
Tokenises a raw Excel formula string (with or without leading '=').
Definition XLFormulaEngine.hpp:87
Recursive-descent (Pratt) parser that converts a token stream into an AST.
Definition XLFormulaEngine.hpp:151
A class encapsulating an Excel worksheet. Access to XLWorksheet objects should be via the workbook ob...
Definition XLWorksheet.hpp:120
Definition IZipArchive.hpp:18
XLNodeKind
Kind discriminator for AST nodes.
Definition XLFormulaEngine.hpp:105
@ Range
e.g. "A1:B10" – evaluated lazily by the resolver
@ ErrorLit
#NAME?, etc. – propagated as-is
XLTokenKind
Kinds of tokens produced by the lexer.
Definition XLFormulaEngine.hpp:41
@ Amp
& (string concat)
@ Ident
Function name or named range.
@ Colon
: (used inside range references parsed by lexer)
@ CellRef
Cell reference A1, $B$2, Sheet1!C3.
@ End
Sentinel – end of input.
@ Semicolon
; (alternative argument separator in some locales)
@ Bool
TRUE or FALSE keyword.
std::function< XLCellValue(std::string_view ref)> XLCellResolver
Callback type: resolves a cell reference string to a cell value.
Definition XLFormulaEngine.hpp:198
Definition XLCellIterator.hpp:121
Polymorphic AST node. Uses a tagged-union approach with std::unique_ptr children.
Definition XLFormulaEngine.hpp:122
XLASTNode(const XLASTNode &)=delete
XLASTNode(XLNodeKind k)
Definition XLFormulaEngine.hpp:134
std::vector< std::unique_ptr< XLASTNode > > children
operands or function arguments
Definition XLFormulaEngine.hpp:132
XLASTNode & operator=(XLASTNode &&)=default
std::string text
string literal, cell-ref text, range text, identifier, error text
Definition XLFormulaEngine.hpp:127
XLNodeKind kind
Definition XLFormulaEngine.hpp:123
XLASTNode & operator=(const XLASTNode &)=delete
XLASTNode(XLASTNode &&)=default
A single lexical token from a formula string.
Definition XLFormulaEngine.hpp:73
std::string text
Raw text of the token (number string, identifier, …)
Definition XLFormulaEngine.hpp:75