OpenXLSX 1.12.1
Modern C++ Excel Library (Fork of OpenXLSX by Kenneth Troldal Balslev)
Loading...
Searching...
No Matches
XLCellRange.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_XLCELLRANGE_HPP
12#define OPENXLSX_XLCELLRANGE_HPP
13
14#ifdef _MSC_VER
15# pragma warning(push)
16# pragma warning(disable : 4251)
17# pragma warning(disable : 4275)
18#endif
19
20// ===== External Includes ===== //
21#include <memory>
22
23// ===== OpenXLSX Includes ===== //
24#include "OpenXLSX-Exports.hpp"
25#include "XLCell.hpp"
26#include "XLCellIterator.hpp"
27#include "XLCellReference.hpp"
28#include "XLStyle.hpp"
29#include "XLXmlParser.hpp"
30
31namespace OpenXLSX
32{
39 class OPENXLSX_EXPORT XLCellRange
40 {
41 friend class XLCellIterator;
42
43 public:
48
56 explicit XLCellRange(const XMLNode& dataNode,
59 const XLSharedStrings& sharedStrings);
60
61 ~XLCellRange() = default;
62 XLCellRange(const XLCellRange& other) = default;
63 XLCellRange(XLCellRange&& other) noexcept = default;
64 XLCellRange& operator=(const XLCellRange& other) = default;
65 XLCellRange& operator=(XLCellRange&& other) noexcept = default;
66
72 void fetchColumnStyles();
73
74 [[nodiscard]] XLCellReference topLeft() const;
75 [[nodiscard]] XLCellReference bottomRight() const;
76
80 [[nodiscard]] std::string address() const;
81
82 [[nodiscard]] uint32_t numRows() const;
83 [[nodiscard]] uint16_t numColumns() const;
84
85 [[nodiscard]] XLCellIterator begin() const;
86 [[nodiscard]] XLCellIterator end() const;
87
92 void applyStyle(const XLStyle& style);
93
99 void setBorderOutline(XLLineStyle style, XLColor color);
100
104 template<typename T>
105 std::vector<std::vector<T>> getValue() const;
106
107 template<typename T>
108 void setValue(const std::vector<std::vector<T>>& matrix);
112 [[nodiscard]] bool empty() const;
113
117 void clear();
118
122 template<typename T,
123 typename = std::enable_if_t<
124 std::is_integral_v<T> or std::is_floating_point_v<T> or std::is_same_v<std::decay_t<T>, std::string> ||
125 std::is_same_v<std::decay_t<T>, std::string_view> or std::is_same_v<std::decay_t<T>, const char*> ||
126 std::is_same_v<std::decay_t<T>, char*> or std::is_same_v<T, XLDateTime>>>
128 {
129 for (auto it = begin(); it != end(); ++it) it->value() = value;
130 return *this;
131 }
132
138 XLCellRange& setFormat(XLStyleIndex cellFormatIndex);
139
144 [[nodiscard]] XLCellRange intersect(const XLCellRange& other) const;
145
146 private:
147 XMLNode m_dataNode;
148 XLCellReference m_topLeft;
149 XLCellReference m_bottomRight;
150 XLSharedStringsRef m_sharedStrings;
151 std::vector<XLStyleIndex> m_columnStyles;
152 };
153} // namespace OpenXLSX
154
155#ifdef _MSC_VER
156# pragma warning(pop)
157#endif
158
159// Template implementations
160namespace OpenXLSX
161{
162 template<typename T>
163 std::vector<std::vector<T>> XLCellRange::getValue() const
164 {
165 std::vector<std::vector<T>> matrix;
166 if (numRows() == 0 || numColumns() == 0) return matrix;
167
168 matrix.resize(numRows(), std::vector<T>(numColumns()));
169
170 uint32_t startRow = topLeft().row();
171 uint16_t startCol = topLeft().column();
172
173 for (auto& cell : *this) {
174 uint32_t r = cell.cellReference().row() - startRow;
175 uint16_t c = cell.cellReference().column() - startCol;
176 matrix[r][c] = cell.value().get<T>();
177 }
178 return matrix;
179 }
180
181 template<typename T>
182 void XLCellRange::setValue(const std::vector<std::vector<T>>& matrix)
183 {
184 if (matrix.empty() || matrix[0].empty()) return;
185
186 uint32_t startRow = topLeft().row();
187 uint16_t startCol = topLeft().column();
188
189 for (auto& cell : *this) {
190 uint32_t r = cell.cellReference().row() - startRow;
191 uint16_t c = cell.cellReference().column() - startCol;
192 if (r < matrix.size() && c < matrix[r].size()) { cell.value() = matrix[r][c]; }
193 }
194 }
195} // namespace OpenXLSX
196#endif // OPENXLSX_XLCELLRANGE_HPP
Represents a rectangular area of cells within a worksheet.
Definition XLCellRange.hpp:40
XLCellReference topLeft() const
Definition XLCellRange.cpp:90
uint32_t numRows() const
Definition XLCellRange.cpp:109
void clear()
Clears the values of all cells within the range.
Definition XLCellRange.cpp:142
XLCellReference bottomRight() const
Definition XLCellRange.cpp:95
XLCellIterator begin() const
Definition XLCellRange.cpp:127
uint16_t numColumns() const
Definition XLCellRange.cpp:118
XLCellRange()
Constructs an uninitialized range.
Definition XLCellRange.cpp:24
void setValue(const std::vector< std::vector< T > > &matrix)
Definition XLCellRange.hpp:182
void applyStyle(const XLStyle &style)
Apply a high-level style to all cells within this range.
Definition XLCellRange.cpp:174
XLCellRange(XLCellRange &&other) noexcept=default
std::string address() const
Definition XLCellRange.cpp:100
XLCellRange(const XLCellRange &other)=default
void fetchColumnStyles()
Scans the worksheet for column-level styles and caches them.
Definition XLCellRange.cpp:59
std::vector< std::vector< T > > getValue() const
Read the range into a 2D matrix of type T.
Definition XLCellRange.hpp:163
bool empty() const
Returns true if the range is uninitialized or points to an invalid worksheet node.
Definition XLCellRange.cpp:137
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:127
friend class XLCellIterator
Definition XLCellRange.hpp:41
void setBorderOutline(XLLineStyle style, XLColor color)
Set border style specifically for the outer edges of this range.
Definition XLCellRange.cpp:179
XLCellIterator end() const
Definition XLCellRange.cpp:132
Definition XLCellReference.hpp:44
uint32_t row() const noexcept
Get the row number of the XLCellReference.
Definition XLCellReference.cpp:157
uint16_t column() const noexcept
Get the column number of the XLCellReference.
Definition XLCellReference.cpp:175
Definition XLColor.hpp:32
This class encapsulate the Excel concept of Shared Strings. In Excel, instead of havig individual str...
Definition XLSharedStrings.hpp:78
Definition IXLCellProvider.hpp:24
OpenXLSX_xml_node XMLNode
Definition XLXmlParser.hpp:64
size_t XLStyleIndex
Definition XLStyles_Common.hpp:36
XLLineStyle
Definition XLStyles_Common.hpp:157
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