OpenXLSX 1.12.1
Modern C++ Excel Library (Fork of OpenXLSX by Kenneth Troldal Balslev)
Loading...
Searching...
No Matches
XLCell.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_XLCELL_HPP
12#define OPENXLSX_XLCELL_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#include <iostream> // std::ostream
21#include <memory>
22#include <ostream> // std::basic_ostream
23
24// ===== OpenXLSX Includes ===== //
25#include "OpenXLSX-Exports.hpp"
26#include "XLCellReference.hpp"
27#include "XLCellValue.hpp"
28#include "XLFormula.hpp"
29#include "XLSharedStrings.hpp"
30#include "XLStyle.hpp" // XLStyle; pulls XLStyles for format APIs
31#include "XLStyles_Common.hpp" // XLStyleIndex (explicit for callers of cellFormat)
32
33namespace OpenXLSX
34{
35 // ===== Flags that can be passed to XLCell::clear as parameter keep, flags can be combined with bitwise OR
36 // // Do not clear the cell's:
37 constexpr const uint32_t XLKeepCellStyle = 1; // style (attribute s)
38 constexpr const uint32_t XLKeepCellType = 2; // type (attribute t)
39 constexpr const uint32_t XLKeepCellValue = 4; // value (child node v)
40 constexpr const uint32_t XLKeepCellFormula = 8; // formula (child node f)
41
42 class XLCellRange;
43
44 class XLWorksheet;
46
50 class OPENXLSX_EXPORT XLCell
51 {
52 friend class XLCellIterator;
53 friend class XLCellValueProxy;
54 friend class XLFormulaProxy;
55 friend class XLRowDataIterator;
56 friend bool operator==(const XLCell& lhs, const XLCell& rhs);
57 friend bool operator!=(const XLCell& lhs, const XLCell& rhs);
58
59 public:
63 XLCell();
64
71 XLCell(const XMLNode& cellNode, const XLSharedStrings& sharedStrings, XLWorksheet* wks = nullptr);
72
79 XLCell(const XLCell& other);
80
86 XLCell(XLCell&& other) noexcept;
87
92 virtual ~XLCell();
93
100 virtual XLCell& operator=(const XLCell& other);
101
108 virtual XLCell& operator=(XLCell&& other) noexcept;
109
114 void copyFrom(XLCell const& other);
115
120 bool empty() const;
121
126 explicit operator bool() const;
127
135 void clear(uint32_t keep);
136
142
147 const XLCellValueProxy& value() const;
148
154
159 XLCell offset(uint16_t rowOffset, uint16_t colOffset) const;
160
165 bool hasFormula() const;
166
172
177 const XLFormulaProxy& formula() const;
178
181 std::string getString() const { return value().getString(); }
182
188 XLStyleIndex cellFormat() const;
189
195 XLCell& setCellFormat(XLStyleIndex cellFormatIndex);
196
202 XLCell& setStyle(const XLStyle& style);
203
207 XLThreadedComment addComment(std::string_view text, std::string_view author = "");
208
212 XLCell& addNote(std::string_view text, std::string_view author = "");
213
217 void print(std::basic_ostream<char>& ostr) const;
218
219 private:
225 static bool isEqual(const XLCell& lhs, const XLCell& rhs);
226 XMLNode m_cellNode;
227 XLSharedStringsRef m_sharedStrings;
228 XLCellValueProxy m_valueProxy;
229 XLFormulaProxy m_formulaProxy;
230 XLWorksheet* m_wks{nullptr};
231 };
232
233 class OPENXLSX_EXPORT XLCellAssignable : public XLCell
234 {
235 public:
240
245 XLCellAssignable(XLCell const& other);
247
252 XLCellAssignable(XLCell&& other);
253 XLCellAssignable(XLCellAssignable&& other) noexcept;
254
261 XLCellAssignable& operator=(const XLCell& other) override;
262 XLCellAssignable& operator=(const XLCellAssignable& other);
263
270 XLCellAssignable& operator=(XLCell&& other) noexcept override;
271 XLCellAssignable& operator=(XLCellAssignable&& other) noexcept;
272
273 XLCellAssignable& addNote(std::string_view text, std::string_view author = "") {
274 XLCell::addNote(text, author);
275 return *this;
276 }
277
279 XLCell::setCellFormat(cellFormatIndex);
280 return *this;
281 }
282
284 XLCell::setStyle(style);
285 return *this;
286 }
287
294 template<typename T,
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, XLDateTime>>>
300 {
301 XLCell::value() = value; // forward implementation to templated XLCellValue& XLCellValue::operator=(T value)
302 return *this;
303 }
304 };
305} // namespace OpenXLSX
306
307namespace OpenXLSX
308{
314 inline bool operator==(const XLCell& lhs, const XLCell& rhs) { return XLCell::isEqual(lhs, rhs); }
315
321 inline bool operator!=(const XLCell& lhs, const XLCell& rhs) { return !XLCell::isEqual(lhs, rhs); }
322
329 inline std::ostream& operator<<(std::ostream& os, const XLCell& c)
330 {
331 XLCellValue val = c.value();
332 switch (val.type()) {
334 os << val.get<int64_t>();
335 break;
337 os << val.get<double>();
338 break;
340 os << (val.get<bool>() ? "true" : "false");
341 break;
343 break;
344 default:
345 os << c.getString();
346 break;
347 }
348 return os;
349 }
350
357 inline std::ostream& operator<<(std::ostream& os, const XLCellAssignable& c)
358 {
359 XLCellValue val = c.value();
360 switch (val.type()) {
362 os << val.get<int64_t>();
363 break;
365 os << val.get<double>();
366 break;
368 os << (val.get<bool>() ? "true" : "false");
369 break;
371 break;
372 default:
373 os << c.getString();
374 break;
375 }
376 return os;
377 }
378} // namespace OpenXLSX
379
380#ifdef _MSC_VER // conditionally enable MSVC specific pragmas to avoid other compilers warning about unknown pragmas
381# pragma warning(pop)
382#endif // _MSC_VER
383
384#endif // OPENXLSX_XLCELL_HPP
Definition XLCell.hpp:234
XLCellAssignable & setStyle(const XLStyle &style)
Definition XLCell.hpp:283
XLCellAssignable()
Default constructor. Constructs a null object.
Definition XLCell.hpp:239
XLCellAssignable & operator=(T value)
Templated assignment operator.
Definition XLCell.hpp:299
XLCellAssignable & addNote(std::string_view text, std::string_view author="")
Definition XLCell.hpp:273
XLCellAssignable & setCellFormat(XLStyleIndex cellFormatIndex)
Definition XLCell.hpp:278
Represents a rectangular area of cells within a worksheet.
Definition XLCellRange.hpp:40
Definition XLCellReference.hpp:44
The XLCellValueProxy class is used for proxy (or placeholder) objects for XLCellValue objects.
Definition XLCellValue.hpp:418
Class encapsulating a cell value.
Definition XLCellValue.hpp:89
XLValueType type() const
Get the value type of the current object.
Definition XLCellValue.cpp:121
T get() const
Templated getter.
Definition XLCellValue.hpp:275
An implementation class encapsulating the properties and behaviours of a spreadsheet cell.
Definition XLCell.hpp:51
bool empty() const
test if cell object has no (valid) content
Definition XLCell.cpp:125
XLCellReference cellReference() const
get the XLCellReference object for the cell.
Definition XLCell.cpp:129
friend class XLCellValueProxy
Definition XLCell.hpp:53
XLCellValueProxy & value()
Retrieves the mutable value proxy for the cell.
Definition XLCell.cpp:253
XLCell()
Default constructor. Constructs a null object.
Definition XLCell.cpp:26
std::string getString() const
Definition XLCell.hpp:181
XLCell & setStyle(const XLStyle &style)
Apply a high-level XLStyle to this cell. Automatically creates or reuses the required underlying XML ...
Definition XLCell.cpp:286
friend bool operator!=(const XLCell &lhs, const XLCell &rhs)
Definition XLCell.hpp:321
XLCell & setCellFormat(XLStyleIndex cellFormatIndex)
Set the cell style (attribute s) with a reference to the array index of xl/styles....
Definition XLCell.cpp:166
virtual XLCell & operator=(const XLCell &other)
Copy assignment operator.
Definition XLCell.cpp:66
friend bool operator==(const XLCell &lhs, const XLCell &rhs)
Definition XLCell.hpp:314
XLFormulaProxy & formula()
Retrieves the mutable formula proxy for the cell.
Definition XLCell.cpp:150
friend class XLFormulaProxy
Definition XLCell.hpp:54
XLCell offset(uint16_t rowOffset, uint16_t colOffset) const
get the XLCell object from the current cell offset
Definition XLCell.cpp:135
XLCell & addNote(std::string_view text, std::string_view author="")
Add a legacy yellow note to this cell fluently.
Definition XLCell.cpp:270
virtual ~XLCell()
Destructor.
void clear(uint32_t keep)
clear all cell content and attributes except for the cell reference (attribute r)
Definition XLCell.cpp:219
void copyFrom(XLCell const &other)
Copy contents of a cell, value & formula.
Definition XLCell.cpp:89
friend class XLRowDataIterator
Definition XLCell.hpp:55
friend class XLCellIterator
Definition XLCell.hpp:52
bool hasFormula() const
test if cell has a formula (XML) node, even if it is an empty string
Definition XLCell.cpp:144
The XLFormulaProxy serves as a placeholder for XLFormula objects. This enable getting and setting for...
Definition XLFormula.hpp:167
This class encapsulate the Excel concept of Shared Strings. In Excel, instead of havig individual str...
Definition XLSharedStrings.hpp:78
A proxy class encapsulating a single modern threaded comment.
Definition XLThreadedComments.hpp:27
A class encapsulating an Excel worksheet. Access to XLWorksheet objects should be via the workbook ob...
Definition XLWorksheet.hpp:132
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 const uint32_t XLKeepCellType
Definition XLCell.hpp:38
constexpr const uint32_t XLKeepCellValue
Definition XLCell.hpp:39
constexpr const uint32_t XLKeepCellFormula
Definition XLCell.hpp:40
@ Float
Definition XLCellValue.hpp:47
@ Boolean
Definition XLCellValue.hpp:47
@ Integer
Definition XLCellValue.hpp:47
@ Empty
Definition XLCellValue.hpp:47
OpenXLSX_xml_node XMLNode
Definition XLXmlParser.hpp:64
size_t XLStyleIndex
Definition XLStyles_Common.hpp:36
std::ostream & operator<<(std::ostream &os, const XLCell &c)
ostream output of XLCell content
Definition XLCell.hpp:329
constexpr const uint32_t XLKeepCellStyle
Definition XLCell.hpp:37
std::reference_wrapper< const XLSharedStrings > XLSharedStringsRef
Definition XLSharedStrings.hpp:67
A high-level, human-ergonomic structure representing the styling of a cell or range....
Definition XLStyle.hpp:29