11#ifndef OPENXLSX_XLCELLVALUE_HPP
12#define OPENXLSX_XLCELLVALUE_HPP
16# pragma warning(disable : 4251)
17# pragma warning(disable : 4275)
23#include <fast_float/fast_float.h>
25#include <fmt/format.h>
31#include "OpenXLSX-Exports.hpp"
37typedef std::variant<std::string, int64_t, double, bool, OpenXLSX::XLRichText>
XLCellValueType;
41 class XLCellValueProxy;
53 double operator()(int64_t v)
const {
return static_cast<double>(v); }
65 "string is not convertible to double.");
67 auto [ptr, ec] = fast_float::from_chars(v.data(), v.data() + v.size(), val);
68 if (ec != std::errc())
throw XLValueTypeError(
"string is not convertible to double.");
77 std::string
operator()(int64_t v)
const {
return fmt::format(
"{}", v); }
78 std::string
operator()(
double v)
const {
return fmt::format(
"{}", v); }
79 std::string
operator()(
bool v)
const {
return v ?
"true" :
"false"; }
82 std::string
operator()(std::string v)
const {
return v; }
100 friend std::hash<OpenXLSX::XLCellValue>;
102#define OPENXLSX_XLCELLVALUE_FRIEND_STRING_OP(OP, FUNCTOR) \
103 friend bool operator OP(const XLCellValue& lhs, std::string_view rhs) \
105 if (std::holds_alternative<std::string>(lhs.m_value)) \
106 return FUNCTOR<>{}(std::string_view(std::get<std::string>(lhs.m_value)), rhs); \
109 friend bool operator OP(std::string_view lhs, const XLCellValue& rhs) \
111 if (std::holds_alternative<std::string>(rhs.m_value)) \
112 return FUNCTOR<>{}(lhs, std::string_view(std::get<std::string>(rhs.m_value))); \
115 friend bool operator OP(const XLCellValue& lhs, const char* rhs) { return lhs OP std::string_view(rhs); } \
116 friend bool operator OP(const char* lhs, const XLCellValue& rhs) { return std::string_view(lhs) OP rhs; } \
117 friend bool operator OP(const XLCellValue& lhs, const std::string& rhs) { return lhs OP std::string_view(rhs); } \
118 friend bool operator OP(const std::string& lhs, const XLCellValue& rhs) { return std::string_view(lhs) OP rhs; }
127#undef OPENXLSX_XLCELLVALUE_FRIEND_STRING_OP
142 typename = std::enable_if_t<
143 std::is_integral_v<T> or std::is_floating_point_v<T> or std::is_same_v<std::decay_t<T>, std::string> ||
144 std::is_same_v<std::decay_t<T>, std::string_view> or std::is_same_v<std::decay_t<T>,
const char*> ||
145 std::is_same_v<std::decay_t<T>,
char*> or std::is_same_v<T, OpenXLSX::XLRichText> or std::is_same_v<T, XLDateTime>>>
149 if constexpr (std::is_integral_v<T> and std::is_same_v<T, bool>) {
155 else if constexpr (std::is_integral_v<T> and !std::is_same_v<T, bool>) {
157 m_value =
static_cast<int64_t
>(value);
162 else if constexpr (std::is_same_v<std::decay_t<T>, std::string> or std::is_same_v<std::decay_t<T>, std::string_view> ||
163 std::is_same_v<std::decay_t<T>,
const char*> ||
164 (std::is_same_v<std::decay_t<T>,
char*> and !std::is_same_v<T, bool>))
167 m_value = std::string(value);
171 else if constexpr (std::is_same_v<T, XLDateTime>) {
173 m_value = value.serial();
177 else if constexpr (std::is_same_v<T, XLRichText>) {
185 static_assert(std::is_floating_point_v<T>,
"Invalid argument for constructing XLCellValue object");
186 if (std::isfinite(value)) {
188 m_value =
static_cast<double>(value);
192 m_value = std::string(
"#NUM!");
235 typename = std::enable_if_t<
236 std::is_integral_v<T> or std::is_floating_point_v<T> or std::is_same_v<std::decay_t<T>, std::string> ||
237 std::is_same_v<std::decay_t<T>, std::string_view> or std::is_same_v<std::decay_t<T>,
const char*> ||
238 std::is_same_v<std::decay_t<T>,
char*> or std::is_same_v<T, OpenXLSX::XLRichText> or std::is_same_v<T, XLDateTime>>>
243 std::swap(*
this, temp);
254 typename = std::enable_if_t<std::is_same_v<T, XLCellValue> or std::is_integral_v<T> or std::is_floating_point_v<T> ||
255 std::is_same_v<std::decay_t<T>, std::string> or std::is_same_v<std::decay_t<T>, std::string_view> ||
256 std::is_same_v<std::decay_t<T>,
const char*> or std::is_same_v<std::decay_t<T>,
char*> ||
257 std::is_same_v<T, OpenXLSX::XLRichText> or std::is_same_v<T, XLDateTime>>>
271 typename = std::enable_if_t<
272 std::is_integral_v<T> or std::is_floating_point_v<T> or std::is_same_v<std::decay_t<T>, std::string> ||
273 std::is_same_v<std::decay_t<T>, std::string_view> or std::is_same_v<std::decay_t<T>,
const char*> ||
274 std::is_same_v<std::decay_t<T>,
char*> or std::is_same_v<T, XLDateTime> or std::is_same_v<T, OpenXLSX::XLRichText>>>
278 return privateGet<T>();
281 catch (
const std::bad_variant_access&) {
282 throw XLValueTypeError(
"XLCellValue object does not contain the requested type.");
295 typename = std::enable_if_t<
296 std::is_integral_v<T> or std::is_floating_point_v<T> or std::is_same_v<std::decay_t<T>, std::string> ||
297 std::is_same_v<std::decay_t<T>, std::string_view> or std::is_same_v<std::decay_t<T>,
const char*> ||
298 std::is_same_v<std::decay_t<T>,
char*> or std::is_same_v<T, OpenXLSX::XLRichText> or std::is_same_v<T, XLDateTime>>>
302 if constexpr (std::is_integral_v<T> and std::is_same_v<T, bool>)
return std::get<bool>(m_value);
304 if constexpr (std::is_integral_v<T> and !std::is_same_v<T, bool>)
return static_cast<T
>(std::get<int64_t>(m_value));
306 if constexpr (std::is_floating_point_v<T>) {
307 return static_cast<T
>(getDouble());
313 if constexpr (std::is_same_v<std::decay_t<T>, std::string> or std::is_same_v<std::decay_t<T>, std::string_view> ||
314 std::is_same_v<std::decay_t<T>,
const char*> ||
315 (std::is_same_v<std::decay_t<T>,
char*> and !std::is_same_v<T, bool>))
316 return std::get<std::string>(m_value).c_str();
318 if constexpr (std::is_same_v<T, OpenXLSX::XLRichText>)
return std::get<OpenXLSX::XLRichText>(m_value);
320 if constexpr (std::is_same_v<T, XLDateTime>)
321 return XLDateTime(getDouble());
325 catch (
const std::bad_variant_access&) {
326 throw XLValueTypeError(
"XLCellValue object does not contain the requested type.");
376 typename = std::enable_if_t<
377 std::is_integral_v<T> or std::is_floating_point_v<T> or std::is_same_v<std::decay_t<T>, std::string> ||
378 std::is_same_v<std::decay_t<T>, std::string_view> or std::is_same_v<std::decay_t<T>,
const char*> ||
379 std::is_same_v<std::decay_t<T>,
char*> or std::is_same_v<T, OpenXLSX::XLRichText> or std::is_same_v<T, XLDateTime>>>
381 {
return this->
get<T>(); }
405 const char* typeAsString()
const;
409 XLValueType m_type{XLValueType::Empty};
417 class OPENXLSX_EXPORT XLCellValueProxy
434 XLCellValueProxy&
operator=(
const XLCellValueProxy& other);
443 typename = std::enable_if_t<
444 std::is_integral_v<T> or std::is_floating_point_v<T> or std::is_same_v<std::decay_t<T>, std::string> ||
445 std::is_same_v<std::decay_t<T>, std::string_view> or std::is_same_v<std::decay_t<T>,
const char*> ||
446 std::is_same_v<std::decay_t<T>,
char*> or std::is_same_v<T, XLCellValue> or std::is_same_v<T, XLDateTime> or
447 std::is_same_v<T, OpenXLSX::XLRichText>>>
451 if constexpr (std::is_integral_v<T> and std::is_same_v<T, bool>)
454 else if constexpr (std::is_integral_v<T> and !std::is_same_v<T, bool>)
457 else if constexpr (std::is_floating_point_v<T>)
460 else if constexpr (std::is_same_v<T, XLDateTime>)
461 setFloat(value.serial());
463 else if constexpr (std::is_same_v<T, OpenXLSX::XLRichText>)
466 else if constexpr (std::is_same_v<std::decay_t<T>, std::string> or std::is_same_v<std::decay_t<T>, std::string_view> ||
467 std::is_same_v<std::decay_t<T>,
const char*> ||
468 (std::is_same_v<std::decay_t<T>,
char*> and !std::is_same_v<T, bool> and !std::is_same_v<T, XLCellValue>))
473 if constexpr (std::is_same_v<T, XLCellValue>) {
474 switch (value.type()) {
485 setString(value.template privateGet<const char*>());
508 typename = std::enable_if_t<
509 std::is_integral_v<T> or std::is_floating_point_v<T> or std::is_same_v<std::decay_t<T>, std::string> ||
510 std::is_same_v<std::decay_t<T>, std::string_view> or std::is_same_v<std::decay_t<T>,
const char*> ||
511 std::is_same_v<std::decay_t<T>,
char*> or std::is_same_v<T, XLCellValue> or std::is_same_v<T, XLDateTime> or
512 std::is_same_v<T, OpenXLSX::XLRichText>>>
521 typename = std::enable_if_t<
522 std::is_integral_v<T> or std::is_floating_point_v<T> or std::is_same_v<std::decay_t<T>, std::string> ||
523 std::is_same_v<std::decay_t<T>, std::string_view> or std::is_same_v<std::decay_t<T>,
const char*> ||
524 std::is_same_v<std::decay_t<T>,
char*> or std::is_same_v<T, OpenXLSX::XLRichText> or std::is_same_v<T, XLDateTime>>>
527 if constexpr (std::is_same_v<std::decay_t<T>, std::string_view> || std::is_same_v<std::decay_t<T>,
const char*>) {
529 if constexpr (std::is_same_v<std::decay_t<T>,
const char*>)
return view.data();
532 else if constexpr (std::is_same_v<std::decay_t<T>, std::string>) {
536 return getValue().get<T>();
562 const char* typeAsString()
const;
576 typename = std::enable_if_t<
577 std::is_integral_v<T> or std::is_floating_point_v<T> or std::is_same_v<std::decay_t<T>, std::string> ||
578 std::is_same_v<std::decay_t<T>, std::string_view> or std::is_same_v<std::decay_t<T>,
const char*> ||
579 std::is_same_v<std::decay_t<T>,
char*> or std::is_same_v<T, OpenXLSX::XLRichText> or std::is_same_v<T, XLDateTime>>>
581 {
return getValue().get<T>(); }
593 std::string_view getStringView()
const;
634 void notifyDocumentOfChange();
640 void setInteger(int64_t numberValue);
646 void setBoolean(
bool numberValue);
652 void setFloat(
double numberValue);
658 void setString(std::string_view stringValue);
664 void setRichText(
const XLRichText& richTextValue);
677 int32_t stringIndex()
const;
684 bool setStringIndex(int32_t newIndex);
696 template<
typename Op>
699 template<
typename T,
typename U>
702 if constexpr (std::is_arithmetic_v<T> && std::is_arithmetic_v<U> && !std::is_same_v<T, bool> && !std::is_same_v<U, bool>) {
703 return Op{}(lhs, rhs);
705 else if constexpr (std::is_same_v<T, bool> && std::is_same_v<U, bool>) {
706 return Op{}(lhs, rhs);
708 else if constexpr (std::is_same_v<T, std::string> && std::is_same_v<U, std::string>) {
709 return Op{}(lhs, rhs);
722 {
return std::visit(
XLCompareVisitor<std::equal_to<>>{}, lhs.m_value, rhs.m_value); }
729 {
return std::visit(
XLCompareVisitor<std::not_equal_to<>>{}, lhs.m_value, rhs.m_value); }
736 {
return std::visit(
XLCompareVisitor<std::less<>>{}, lhs.m_value, rhs.m_value); }
743 {
return std::visit(
XLCompareVisitor<std::greater<>>{}, lhs.m_value, rhs.m_value); }
750 {
return std::visit(
XLCompareVisitor<std::less_equal<>>{}, lhs.m_value, rhs.m_value); }
757 {
return std::visit(
XLCompareVisitor<std::greater_equal<>>{}, lhs.m_value, rhs.m_value); }
765 switch (value.
type()) {
769 return os << value.
get<
bool>();
771 return os << value.
get<int64_t>();
773 return os << value.
get<
double>();
775 return os << value.
get<std::string>();
786 switch (value.
type()) {
790 return os << value.
get<
bool>();
792 return os << value.
get<int64_t>();
794 return os << value.
get<
double>();
796 return os << value.
get<std::string>();
814 std::size_t h = std::hash<std::string>{}(run.text());
815 if (run.bold()) h ^= std::hash<bool>{}(*run.bold()) + 0x9e3779b9 + (h << 6) + (h >> 2);
816 if (run.italic()) h ^= std::hash<bool>{}(*run.italic()) + 0x9e3779b9 + (h << 6) + (h >> 2);
827 for (
const auto& run : rt.runs()) { h ^= std::hash<OpenXLSX::XLRichTextRun>{}(run) + 0x9e3779b9 + (h << 6) + (h >> 2); }
837 return std::visit([](
const auto& v) {
return std::hash<std::decay_t<
decltype(v)>>{}(v); }, value.getVariant());
#define OPENXLSX_XLCELLVALUE_FRIEND_STRING_OP(OP, FUNCTOR)
Definition XLCellValue.hpp:102
std::variant< std::string, int64_t, double, bool, OpenXLSX::XLRichText > XLCellValueType
Definition XLCellValue.hpp:37
The XLCellValueProxy class is used for proxy (or placeholder) objects for XLCellValue objects.
Definition XLCellValue.hpp:418
XLCellValueProxy & clear()
Clear the contents of the cell.
Definition XLCellValue.cpp:214
XLCellValueProxy & operator=(const T &value)
Templated assignment operator.
Definition XLCellValue.hpp:448
XLCellValueProxy & operator=(const XLCellValueProxy &other)
Copy assignment operator.
Definition XLCellValue.cpp:188
friend class XLDocument
Definition XLCellValue.hpp:421
friend class XLCell
Definition XLCellValue.hpp:419
XLCellValueProxy & setError(const std::string &error)
Set the cell value to a error state.
Definition XLCellValue.cpp:241
friend class XLCellValue
Definition XLCellValue.hpp:420
std::string getString() const
Definition XLCellValue.hpp:594
T get() const
Definition XLCellValue.hpp:525
std::string_view getStringView() const
get the cell value as a std::string, regardless of value type
Definition XLCellValue.cpp:704
~XLCellValueProxy()
Destructor.
void set(const T &value)
Sets the cell value using an automatically deduced type proxy.
Definition XLCellValue.hpp:513
XLValueType type() const
Get the value type for the cell.
Definition XLCellValue.cpp:273
Class encapsulating a cell value.
Definition XLCellValue.hpp:89
void set(T numberValue)
Templated setter for integral and bool types.
Definition XLCellValue.hpp:258
XLCellValue(XLCellValue &&other) noexcept
Move constructor.
double getDouble() const
get the cell value as a double, regardless of value type
Definition XLCellValue.hpp:336
XLCellValue & operator=(T value)
Templated assignment operator.
Definition XLCellValue.hpp:239
friend bool operator<(const XLCellValue &lhs, const XLCellValue &rhs)
Definition XLCellValue.hpp:735
friend class XLCellValueProxy
Definition XLCellValue.hpp:90
const XLCellValueType & getVariant() const
get the cell value as a std::variant of XLCellValueType
Definition XLCellValue.hpp:366
friend bool operator==(const XLCellValue &lhs, const XLCellValue &rhs)
Definition XLCellValue.hpp:721
friend std::ostream & operator<<(std::ostream &os, const XLCellValue &value)
Definition XLCellValue.hpp:763
std::string getString()
get the cell value as a std::string, regardless of value type
Definition XLCellValue.hpp:352
friend bool operator>(const XLCellValue &lhs, const XLCellValue &rhs)
Definition XLCellValue.hpp:742
~XLCellValue()
Destructor.
friend bool operator<=(const XLCellValue &lhs, const XLCellValue &rhs)
Definition XLCellValue.hpp:749
XLCellValue(const XLCellValue &other)
Copy constructor.
XLCellValue & operator=(const XLCellValue &other)
Copy assignment operator.
XLValueType type() const
Get the value type of the current object.
Definition XLCellValue.cpp:121
T get() const
Templated getter.
Definition XLCellValue.hpp:275
friend bool operator!=(const XLCellValue &lhs, const XLCellValue &rhs)
Definition XLCellValue.hpp:728
XLCellValue(const T &value)
A templated constructor. Any value convertible to a valid cell value can be used as argument.
Definition XLCellValue.hpp:146
XLCellValue & operator=(XLCellValue &&other) noexcept
Move assignment operator.
friend bool operator>=(const XLCellValue &lhs, const XLCellValue &rhs)
Definition XLCellValue.hpp:756
XLCellValue()
Default constructor.
An implementation class encapsulating the properties and behaviours of a spreadsheet cell.
Definition XLCell.hpp:51
A class representing a single run of rich text.
Definition XLRichText.hpp:30
A class representing rich text in a cell.
Definition XLRichText.hpp:139
std::string plainText() const
Get the plain text representation of the rich text.
Definition XLRichText.hpp:160
Definition XLException.hpp:51
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
bool operator<=(const XLCellReference &lhs, const XLCellReference &rhs) noexcept
Asserts whether a cell sequentially precedes or occupies the exact same coordinate as another.
Definition XLCellReference.hpp:254
bool operator<(const XLCellReference &lhs, const XLCellReference &rhs) noexcept
Evaluates precedence primarily by row, then by column, allowing cell ranges to be sorted efficiently ...
Definition XLCellReference.hpp:243
XLValueType
Enum defining the valid value types for a an Excel spreadsheet cell.
Definition XLCellValue.hpp:47
@ Float
Definition XLCellValue.hpp:47
@ RichText
Definition XLCellValue.hpp:47
@ String
Definition XLCellValue.hpp:47
@ Boolean
Definition XLCellValue.hpp:47
@ Error
Definition XLCellValue.hpp:47
@ Integer
Definition XLCellValue.hpp:47
@ Empty
Definition XLCellValue.hpp:47
@ Error
Definition XLCalculationEngine.hpp:129
@ Empty
Definition XLCalculationEngine.hpp:130
OpenXLSX_xml_node XMLNode
Definition XLXmlParser.hpp:64
bool operator>=(const XLCellReference &lhs, const XLCellReference &rhs) noexcept
Asserts whether a cell sequentially follows or occupies the exact same coordinate as another.
Definition XLCellReference.hpp:259
std::ostream & operator<<(std::ostream &os, const XLCell &c)
ostream output of XLCell content
Definition XLCell.hpp:329
bool operator>(const XLCellReference &lhs, const XLCellReference &rhs) noexcept
Inverts the less-than operator logic to verify strict left-to-right, top-to-bottom traversal dominanc...
Definition XLCellReference.hpp:249
Definition XLCellIterator.hpp:131
Definition XLCellValue.hpp:51
std::string packageName
Definition XLCellValue.hpp:52
double operator()(std::string v) const
Definition XLCellValue.hpp:62
double operator()(int64_t v) const
Definition XLCellValue.hpp:53
double operator()(double v) const
Definition XLCellValue.hpp:54
double operator()(bool v) const
Definition XLCellValue.hpp:55
double operator()(const OpenXLSX::XLRichText &v) const
Definition XLCellValue.hpp:57
Definition XLCellValue.hpp:75
std::string operator()(bool v) const
Definition XLCellValue.hpp:79
std::string operator()(int64_t v) const
Definition XLCellValue.hpp:77
std::string operator()(double v) const
Definition XLCellValue.hpp:78
std::string operator()(std::string v) const
Definition XLCellValue.hpp:82
std::string packageName
Definition XLCellValue.hpp:76
std::string operator()(const XLRichText &v) const
Definition XLCellValue.hpp:81
Definition XLCellValue.hpp:698
bool operator()(const T &lhs, const U &rhs) const
Definition XLCellValue.hpp:700
std::size_t operator()(const OpenXLSX::XLCellValue &value) const noexcept
Definition XLCellValue.hpp:835
std::size_t operator()(const OpenXLSX::XLRichTextRun &run) const noexcept
Definition XLCellValue.hpp:812
std::size_t operator()(const OpenXLSX::XLRichText &rt) const noexcept
Definition XLCellValue.hpp:824