OpenXLSX 1.12.1
Modern C++ Excel Library (Fork of OpenXLSX by Kenneth Troldal Balslev)
Loading...
Searching...
No Matches
XLFormula.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_XLFORMULA_HPP
12#define OPENXLSX_XLFORMULA_HPP
13
14#ifdef _MSC_VER // conditionally enable MSVC specific pragmas to avoid other compilers warning about unknown pragmas
15# pragma warning(push)
16# pragma warning(disable : 4251)
17# pragma warning(disable : 4275)
18#endif // _MSC_VER
19
20// ===== External Includes ===== //
21#include <iostream>
22#include <string>
23#include <variant>
24
25// ===== OpenXLSX Includes ===== //
26#include "OpenXLSX-Exports.hpp"
27#include "XLXmlParser.hpp"
28
29namespace OpenXLSX
30{
31 constexpr bool XLResetValue = true;
32 constexpr bool XLPreserveValue = false;
33
34 //---------- Forward Declarations ----------//
35 class XLFormulaProxy;
36 class XLCell;
37
46 class OPENXLSX_EXPORT XLFormula
47 {
48 //---------- Friend Declarations ----------//
49
50 friend bool operator==(const XLFormula& lhs, const XLFormula& rhs);
51 friend bool operator!=(const XLFormula& lhs, const XLFormula& rhs);
52 friend std::ostream& operator<<(std::ostream& os, const XLFormula& value);
53
54 public:
59
65 template<
66 typename T,
67 typename = std::enable_if_t<std::is_same_v<std::decay_t<T>, std::string> or std::is_same_v<std::decay_t<T>, std::string_view> ||
68 std::is_same_v<std::decay_t<T>, const char*> or std::is_same_v<std::decay_t<T>, char*>>>
69 explicit XLFormula(T formula)
70 {
71 // ===== If the argument is a const char *, use the argument directly; otherwise, assume it has a .c_str() function.
72 if constexpr (std::is_same_v<std::decay_t<T>, const char*> or std::is_same_v<std::decay_t<T>, char*>)
73 m_formulaString = formula;
74 else if constexpr (std::is_same_v<std::decay_t<T>, std::string_view>)
75 m_formulaString = std::string(formula);
76 else
77 m_formulaString = formula.c_str();
78 }
79
84 XLFormula(const XLFormula& other);
85
90 XLFormula(XLFormula&& other) noexcept;
91
96
103
109 XLFormula& operator=(XLFormula&& other) noexcept;
110
117 template<
118 typename T,
119 typename = std::enable_if_t<std::is_same_v<std::decay_t<T>, std::string> or std::is_same_v<std::decay_t<T>, std::string_view> ||
120 std::is_same_v<std::decay_t<T>, const char*> or std::is_same_v<std::decay_t<T>, char*>>>
122 {
123 XLFormula temp(formula);
124 std::swap(*this, temp);
125 return *this;
126 }
127
133 template<
134 typename T,
135 typename = std::enable_if_t<std::is_same_v<std::decay_t<T>, std::string> or std::is_same_v<std::decay_t<T>, std::string_view> ||
136 std::is_same_v<std::decay_t<T>, const char*> or std::is_same_v<std::decay_t<T>, char*>>>
137 void set(T formula)
138 { *this = formula; }
139
144 std::string get() const;
145
150 operator std::string() const; // NOLINT
151
156 XLFormula& clear();
157
158 private:
159 std::string m_formulaString;
160 };
161
166 class OPENXLSX_EXPORT XLFormulaProxy
167 {
168 friend class XLCell;
169 friend class XLFormula;
170
171 public:
176
182 XLFormulaProxy& operator=(const XLFormulaProxy& other);
183
190 template<typename T,
191 typename = std::enable_if_t<std::is_same_v<std::decay_t<T>, XLFormula> or std::is_same_v<std::decay_t<T>, std::string> ||
192 std::is_same_v<std::decay_t<T>, std::string_view> ||
193 std::is_same_v<std::decay_t<T>, const char*> or std::is_same_v<std::decay_t<T>, char*>>>
194 XLFormulaProxy& operator=(T formula)
195 {
196 if constexpr (std::is_same_v<std::decay_t<T>, XLFormula>)
197 setFormulaString(formula.get().c_str());
198 else if constexpr (std::is_same_v<std::decay_t<T>, std::string>)
199 setFormulaString(formula.c_str());
200 else if constexpr (std::is_same_v<std::decay_t<T>, std::string_view>)
201 setFormulaString(std::string(formula).c_str());
202 else
203 setFormulaString(formula);
204
205 return *this;
206 }
207
213 template<
214 typename T,
215 typename = std::enable_if_t<std::is_same_v<std::decay_t<T>, std::string> or std::is_same_v<std::decay_t<T>, std::string_view> ||
216 std::is_same_v<std::decay_t<T>, const char*> or std::is_same_v<std::decay_t<T>, char*>>>
217 void set(T formula)
218 { *this = formula; }
219
224 std::string get() const;
225
230 XLFormulaProxy& clear();
231
236 operator std::string() const; // NOLINT
237
242 operator XLFormula() const; // NOLINT
243
244 private:
250 XLFormulaProxy(XLCell* cell, XMLNode* cellNode);
251
256 XLFormulaProxy(const XLFormulaProxy& other);
257
262 XLFormulaProxy(XLFormulaProxy&& other) noexcept;
263
269 XLFormulaProxy& operator=(XLFormulaProxy&& other) noexcept;
270
276 void setFormulaString(const char* formulaString, bool resetValue = XLResetValue);
277
283 XLFormula getFormula() const;
284
285 //---------- Private Member Variables ---------- //
286 XLCell* m_cell;
287 XMLNode* m_cellNode;
288 };
289} // namespace OpenXLSX
290
291namespace OpenXLSX
292{
299 inline bool operator==(const XLFormula& lhs, const XLFormula& rhs) { return lhs.m_formulaString == rhs.m_formulaString; }
300
307 inline bool operator!=(const XLFormula& lhs, const XLFormula& rhs) { return lhs.m_formulaString != rhs.m_formulaString; }
308
315 inline std::ostream& operator<<(std::ostream& os, const XLFormula& value) { return os << value.m_formulaString; }
316
323 inline std::ostream& operator<<(std::ostream& os, const XLFormulaProxy& value) { return os << value.get(); }
324} // namespace OpenXLSX
325
326#ifdef _MSC_VER // conditionally enable MSVC specific pragmas to avoid other compilers warning about unknown pragmas
327# pragma warning(pop)
328#endif // _MSC_VER
329
330#endif // OPENXLSX_XLFORMULA_HPP
An implementation class encapsulating the properties and behaviours of a spreadsheet cell.
Definition XLCell.hpp:51
The XLFormulaProxy serves as a placeholder for XLFormula objects. This enable getting and setting for...
Definition XLFormula.hpp:167
std::string get() const
Get the formula as a std::string.
Definition XLFormula.cpp:246
friend class XLFormula
Definition XLFormula.hpp:169
XLFormulaProxy & operator=(const XLFormulaProxy &other)
Copy assignment operator.
Definition XLFormula.cpp:221
~XLFormulaProxy()
Destructor.
friend class XLCell
Definition XLFormula.hpp:168
XLFormulaProxy & operator=(T formula)
Templated assignment operator, taking a string-type argument.
Definition XLFormula.hpp:194
void set(T formula)
Templated setter, taking a string-type argument.
Definition XLFormula.hpp:217
The XLFormula class encapsulates the concept of an Excel formula. The class is essentially a wrapper ...
Definition XLFormula.hpp:47
XLFormula(T formula)
Constructor, taking a string-type argument.
Definition XLFormula.hpp:69
XLFormula(XLFormula &&other) noexcept
Move constructor.
friend bool operator==(const XLFormula &lhs, const XLFormula &rhs)
Definition XLFormula.hpp:299
friend bool operator!=(const XLFormula &lhs, const XLFormula &rhs)
Definition XLFormula.hpp:307
XLFormula & operator=(T formula)
Templated assignment operator, taking a string-type object as an argument.
Definition XLFormula.hpp:121
~XLFormula()
Destructor.
friend std::ostream & operator<<(std::ostream &os, const XLFormula &value)
send a formula string to an ostream
Definition XLFormula.hpp:315
XLFormula()
Constructor.
void set(T formula)
Templated setter function, taking a string-type object as an argument.
Definition XLFormula.hpp:137
XLFormula & operator=(const XLFormula &other)
Copy assignment operator.
XLFormula & operator=(XLFormula &&other) noexcept
Move assignment operator.
XLFormula(const XLFormula &other)
Copy constructor.
Definition XLFormulaUtils.hpp:37
Definition IXLCellProvider.hpp:24
bool operator==(const XLCell &lhs, const XLCell &rhs)
Definition XLCell.hpp:314
bool operator!=(const XLCell &lhs, const XLCell &rhs)
Definition XLCell.hpp:321
constexpr bool XLResetValue
Definition XLFormula.hpp:31
constexpr bool XLPreserveValue
Definition XLFormula.hpp:32
OpenXLSX_xml_node XMLNode
Definition XLXmlParser.hpp:64
std::ostream & operator<<(std::ostream &os, const XLCell &c)
ostream output of XLCell content
Definition XLCell.hpp:329