OpenXLSX 1.9.1
Loading...
Searching...
No Matches
XLCell.hpp
Go to the documentation of this file.
1#ifndef OPENXLSX_XLCELL_HPP
2#define OPENXLSX_XLCELL_HPP
3
4#ifdef _MSC_VER // conditionally enable MSVC specific pragmas to avoid other compilers warning about unknown pragmas
5# pragma warning(push)
6# pragma warning(disable : 4251)
7# pragma warning(disable : 4275)
8#endif // _MSC_VER
9
10#include <iostream> // std::ostream
11#include <memory>
12#include <ostream> // std::basic_ostream
13
14// ===== OpenXLSX Includes ===== //
15#include "OpenXLSX-Exports.hpp"
16#include "XLCellReference.hpp"
17#include "XLCellValue.hpp"
18#include "XLFormula.hpp"
19#include "XLSharedStrings.hpp"
20#include "XLStyle.hpp"
21#include "XLStyles.hpp" // XLStyleIndex
22
23namespace OpenXLSX
24{
25 // ===== Flags that can be passed to XLCell::clear as parameter keep, flags can be combined with bitwise OR
26 // // Do not clear the cell's:
27 constexpr const uint32_t XLKeepCellStyle = 1; // style (attribute s)
28 constexpr const uint32_t XLKeepCellType = 2; // type (attribute t)
29 constexpr const uint32_t XLKeepCellValue = 4; // value (child node v)
30 constexpr const uint32_t XLKeepCellFormula = 8; // formula (child node f)
31
32 class XLCellRange;
33
34 class XLWorksheet;
36
40 class OPENXLSX_EXPORT XLCell
41 {
42 friend class XLCellIterator;
43 friend class XLCellValueProxy;
44 friend class XLFormulaProxy;
45 friend class XLRowDataIterator;
46 friend bool operator==(const XLCell& lhs, const XLCell& rhs);
47 friend bool operator!=(const XLCell& lhs, const XLCell& rhs);
48
49 public:
53 XLCell();
54
61 XLCell(const XMLNode& cellNode, const XLSharedStrings& sharedStrings, XLWorksheet* wks = nullptr);
62
69 XLCell(const XLCell& other);
70
76 XLCell(XLCell&& other) noexcept;
77
82 virtual ~XLCell();
83
90 virtual XLCell& operator=(const XLCell& other);
91
98 virtual XLCell& operator=(XLCell&& other) noexcept;
99
104 void copyFrom(XLCell const& other);
105
110 bool empty() const;
111
116 explicit operator bool() const;
117
125 void clear(uint32_t keep);
126
131 XLCellValueProxy& value();
132
137 const XLCellValueProxy& value() const;
138
143 XLCellReference cellReference() const;
144
149 XLCell offset(uint16_t rowOffset, uint16_t colOffset) const;
150
155 bool hasFormula() const;
156
161 XLFormulaProxy& formula();
162
167 const XLFormulaProxy& formula() const;
168
171 std::string getString() const { return value().getString(); }
172
178 XLStyleIndex cellFormat() const;
179
185 XLCell& setCellFormat(XLStyleIndex cellFormatIndex);
186
192 XLCell& setStyle(const XLStyle& style);
193
197 XLThreadedComment addComment(std::string_view text, std::string_view author = "");
198
202 XLCell& addNote(std::string_view text, std::string_view author = "");
203
207 void print(std::basic_ostream<char>& ostr) const;
208
209 private:
215 static bool isEqual(const XLCell& lhs, const XLCell& rhs);
216 XMLNode m_cellNode;
217 XLSharedStringsRef m_sharedStrings;
218 XLCellValueProxy m_valueProxy;
219 XLFormulaProxy m_formulaProxy;
220 XLWorksheet* m_wks{nullptr};
221 };
222
223 class OPENXLSX_EXPORT XLCellAssignable : public XLCell
224 {
225 public:
230
235 XLCellAssignable(XLCell const& other);
237
242 XLCellAssignable(XLCell&& other);
243 XLCellAssignable(XLCellAssignable&& other) noexcept;
244
251 XLCellAssignable& operator=(const XLCell& other) override;
252 XLCellAssignable& operator=(const XLCellAssignable& other);
253
260 XLCellAssignable& operator=(XLCell&& other) noexcept override;
261 XLCellAssignable& operator=(XLCellAssignable&& other) noexcept;
262
263 XLCellAssignable& addNote(std::string_view text, std::string_view author = "") {
264 XLCell::addNote(text, author);
265 return *this;
266 }
267
269 XLCell::setCellFormat(cellFormatIndex);
270 return *this;
271 }
272
274 XLCell::setStyle(style);
275 return *this;
276 }
277
284 template<typename T,
285 typename = std::enable_if_t<
286 std::is_integral_v<T> or std::is_floating_point_v<T> or std::is_same_v<std::decay_t<T>, std::string> ||
287 std::is_same_v<std::decay_t<T>, std::string_view> or std::is_same_v<std::decay_t<T>, const char*> ||
288 std::is_same_v<std::decay_t<T>, char*> or std::is_same_v<T, XLDateTime>>>
290 {
291 XLCell::value() = value; // forward implementation to templated XLCellValue& XLCellValue::operator=(T value)
292 return *this;
293 }
294 };
295} // namespace OpenXLSX
296
297namespace OpenXLSX
298{
304 inline bool operator==(const XLCell& lhs, const XLCell& rhs) { return XLCell::isEqual(lhs, rhs); }
305
311 inline bool operator!=(const XLCell& lhs, const XLCell& rhs) { return !XLCell::isEqual(lhs, rhs); }
312
319 inline std::ostream& operator<<(std::ostream& os, const XLCell& c)
320 {
321 XLCellValue val = c.value();
322 switch (val.type()) {
324 os << val.get<int64_t>();
325 break;
327 os << val.get<double>();
328 break;
330 os << (val.get<bool>() ? "true" : "false");
331 break;
333 break;
334 default:
335 os << c.getString();
336 break;
337 }
338 return os;
339 }
340
347 inline std::ostream& operator<<(std::ostream& os, const XLCellAssignable& c)
348 {
349 XLCellValue val = c.value();
350 switch (val.type()) {
352 os << val.get<int64_t>();
353 break;
355 os << val.get<double>();
356 break;
358 os << (val.get<bool>() ? "true" : "false");
359 break;
361 break;
362 default:
363 os << c.getString();
364 break;
365 }
366 return os;
367 }
368} // namespace OpenXLSX
369
370#ifdef _MSC_VER // conditionally enable MSVC specific pragmas to avoid other compilers warning about unknown pragmas
371# pragma warning(pop)
372#endif // _MSC_VER
373
374#endif // OPENXLSX_XLCELL_HPP
Definition XLXmlParser.hpp:84
Definition XLCell.hpp:224
XLCellAssignable & setStyle(const XLStyle &style)
Definition XLCell.hpp:273
XLCellAssignable()
Default constructor. Constructs a null object.
Definition XLCell.hpp:229
XLCellAssignable & operator=(T value)
Templated assignment operator.
Definition XLCell.hpp:289
XLCellAssignable & addNote(std::string_view text, std::string_view author="")
Definition XLCell.hpp:263
XLCellAssignable & setCellFormat(XLStyleIndex cellFormatIndex)
Definition XLCell.hpp:268
A forward iterator for iterating over a range of cells.
Definition XLCellIterator.hpp:37
Represents a rectangular area of cells within a worksheet.
Definition XLCellRange.hpp:30
Definition XLCellReference.hpp:34
The XLCellValueProxy class is used for proxy (or placeholder) objects for XLCellValue objects.
Definition XLCellValue.hpp:408
Class encapsulating a cell value.
Definition XLCellValue.hpp:79
XLValueType type() const
Get the value type of the current object.
Definition XLCellValue.cpp:94
T get() const
Templated getter.
Definition XLCellValue.hpp:265
An implementation class encapsulating the properties and behaviours of a spreadsheet cell.
Definition XLCell.hpp:41
XLCellValueProxy & value()
Retrieves the mutable value proxy for the cell.
Definition XLCell.cpp:242
std::string getString() const
Definition XLCell.hpp:171
virtual ~XLCell()
Destructor.
The XLFormulaProxy serves as a placeholder for XLFormula objects. This enable getting and setting for...
Definition XLFormula.hpp:157
This class encapsulates a (non-const) iterator, for iterating over the cells in a row.
Definition XLRowData.hpp:35
This class encapsulate the Excel concept of Shared Strings. In Excel, instead of havig individual str...
Definition XLSharedStrings.hpp:67
A proxy class encapsulating a single modern threaded comment.
Definition XLThreadedComments.hpp:17
A class encapsulating an Excel worksheet. Access to XLWorksheet objects should be via the workbook ob...
Definition XLWorksheet.hpp:118
Definition IZipArchive.hpp:18
bool operator==(const XLCell &lhs, const XLCell &rhs)
Definition XLCell.hpp:304
bool operator!=(const XLCell &lhs, const XLCell &rhs)
Definition XLCell.hpp:311
constexpr const uint32_t XLKeepCellType
Definition XLCell.hpp:28
constexpr const uint32_t XLKeepCellValue
Definition XLCell.hpp:29
size_t XLStyleIndex
Definition XLStyles.hpp:31
constexpr const uint32_t XLKeepCellFormula
Definition XLCell.hpp:30
std::ostream & operator<<(std::ostream &os, const XLCell &c)
ostream output of XLCell content
Definition XLCell.hpp:319
constexpr const uint32_t XLKeepCellStyle
Definition XLCell.hpp:27
std::reference_wrapper< const XLSharedStrings > XLSharedStringsRef
Definition XLSharedStrings.hpp:56
A high-level, human-ergonomic structure representing the styling of a cell or range....
Definition XLStyle.hpp:19