OpenXLSX 1.9.1
Loading...
Searching...
No Matches
XLCellRange.hpp
Go to the documentation of this file.
1#ifndef OPENXLSX_XLCELLRANGE_HPP
2#define OPENXLSX_XLCELLRANGE_HPP
3
4#ifdef _MSC_VER
5# pragma warning(push)
6# pragma warning(disable : 4251)
7# pragma warning(disable : 4275)
8#endif
9
10// ===== External Includes ===== //
11#include <memory>
12
13// ===== OpenXLSX Includes ===== //
14#include "OpenXLSX-Exports.hpp"
15#include "XLCell.hpp"
16#include "XLCellIterator.hpp"
17#include "XLCellReference.hpp"
18#include "XLStyle.hpp"
19#include "XLXmlParser.hpp"
20
21namespace OpenXLSX
22{
29 class OPENXLSX_EXPORT XLCellRange
30 {
31 friend class XLCellIterator;
32
33 public:
38
46 explicit XLCellRange(const XMLNode& dataNode,
47 const XLCellReference& topLeft,
48 const XLCellReference& bottomRight,
49 const XLSharedStrings& sharedStrings);
50
51 ~XLCellRange() = default;
52 XLCellRange(const XLCellRange& other) = default;
53 XLCellRange(XLCellRange&& other) noexcept = default;
54 XLCellRange& operator=(const XLCellRange& other) = default;
55 XLCellRange& operator=(XLCellRange&& other) noexcept = default;
56
62 void fetchColumnStyles();
63
64 [[nodiscard]] XLCellReference topLeft() const;
65 [[nodiscard]] XLCellReference bottomRight() const;
66
70 [[nodiscard]] std::string address() const;
71
72 [[nodiscard]] uint32_t numRows() const;
73 [[nodiscard]] uint16_t numColumns() const;
74
75 [[nodiscard]] XLCellIterator begin() const;
76 [[nodiscard]] XLCellIterator end() const;
77
82 void applyStyle(const XLStyle& style);
83
89 void setBorderOutline(XLLineStyle style, XLColor color);
90
94 template<typename T>
95 std::vector<std::vector<T>> getValue() const;
96
97 template<typename T>
98 void setValue(const std::vector<std::vector<T>>& matrix);
102 [[nodiscard]] bool empty() const;
103
107 void clear();
108
112 template<typename T,
113 typename = std::enable_if_t<
114 std::is_integral_v<T> or std::is_floating_point_v<T> or std::is_same_v<std::decay_t<T>, std::string> ||
115 std::is_same_v<std::decay_t<T>, std::string_view> or std::is_same_v<std::decay_t<T>, const char*> ||
116 std::is_same_v<std::decay_t<T>, char*> or std::is_same_v<T, XLDateTime>>>
118 {
119 for (auto it = begin(); it != end(); ++it) it->value() = value;
120 return *this;
121 }
122
128 XLCellRange& setFormat(XLStyleIndex cellFormatIndex);
129
134 [[nodiscard]] XLCellRange intersect(const XLCellRange& other) const;
135
136 private:
137 XMLNode m_dataNode;
138 XLCellReference m_topLeft;
139 XLCellReference m_bottomRight;
140 XLSharedStringsRef m_sharedStrings;
141 std::vector<XLStyleIndex> m_columnStyles;
142 };
143} // namespace OpenXLSX
144
145#ifdef _MSC_VER
146# pragma warning(pop)
147#endif
148
149// Template implementations
150namespace OpenXLSX
151{
152 template<typename T>
153 std::vector<std::vector<T>> XLCellRange::getValue() const
154 {
155 std::vector<std::vector<T>> matrix;
156 if (numRows() == 0 || numColumns() == 0) return matrix;
157
158 matrix.resize(numRows(), std::vector<T>(numColumns()));
159
160 uint32_t startRow = topLeft().row();
161 uint16_t startCol = topLeft().column();
162
163 for (auto& cell : *this) {
164 uint32_t r = cell.cellReference().row() - startRow;
165 uint16_t c = cell.cellReference().column() - startCol;
166 matrix[r][c] = cell.value().get<T>();
167 }
168 return matrix;
169 }
170
171 template<typename T>
172 void XLCellRange::setValue(const std::vector<std::vector<T>>& matrix)
173 {
174 if (matrix.empty() || matrix[0].empty()) return;
175
176 uint32_t startRow = topLeft().row();
177 uint16_t startCol = topLeft().column();
178
179 for (auto& cell : *this) {
180 uint32_t r = cell.cellReference().row() - startRow;
181 uint16_t c = cell.cellReference().column() - startCol;
182 if (r < matrix.size() && c < matrix[r].size()) { cell.value() = matrix[r][c]; }
183 }
184 }
185} // namespace OpenXLSX
186#endif // OPENXLSX_XLCELLRANGE_HPP
Definition XLXmlParser.hpp:84
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
XLCellReference topLeft() const
Definition XLCellRange.cpp:80
uint32_t numRows() const
Definition XLCellRange.cpp:99
uint16_t numColumns() const
Definition XLCellRange.cpp:108
void setValue(const std::vector< std::vector< T > > &matrix)
Definition XLCellRange.hpp:172
XLCellRange(XLCellRange &&other) noexcept=default
XLCellRange(const XLCellRange &other)=default
std::vector< std::vector< T > > getValue() const
Read the range into a 2D matrix of type T.
Definition XLCellRange.hpp:153
XLCellRange & operator=(XLCellRange &&other) noexcept=default
XLCellRange & operator=(const XLCellRange &other)=default
XLCellRange & operator=(T value)
Assigns a single value to every cell in the range.
Definition XLCellRange.hpp:117
Definition XLCellReference.hpp:34
uint32_t row() const noexcept
Get the row number of the XLCellReference.
Definition XLCellReference.cpp:146
uint16_t column() const noexcept
Get the column number of the XLCellReference.
Definition XLCellReference.cpp:163
Definition XLColor.hpp:22
This class encapsulate the Excel concept of Shared Strings. In Excel, instead of havig individual str...
Definition XLSharedStrings.hpp:67
Definition IZipArchive.hpp:18
size_t XLStyleIndex
Definition XLStyles.hpp:31
XLLineStyle
Definition XLStyles.hpp:149
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