TLA Line data Source code
1 : // Copyright (c) 2014-2022 Thomas Fussell
2 : // Copyright (c) 2024-2025 xlnt-community
3 : //
4 : // Permission is hereby granted, free of charge, to any person obtaining a copy
5 : // of this software and associated documentation files (the "Software"), to deal
6 : // in the Software without restriction, including without limitation the rights
7 : // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 : // copies of the Software, and to permit persons to whom the Software is
9 : // furnished to do so, subject to the following conditions:
10 : //
11 : // The above copyright notice and this permission notice shall be included in
12 : // all copies or substantial portions of the Software.
13 : //
14 : // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 : // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 : // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 : // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 : // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 : // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 : // THE SOFTWARE
21 : //
22 : // @license: http://www.opensource.org/licenses/mit-license.php
23 : // @author: see AUTHORS file
24 :
25 : #pragma once
26 :
27 : #include <functional>
28 : #include <iterator>
29 : #include <string>
30 :
31 : #include <xlnt/xlnt_config.hpp>
32 : #include <xlnt/styles/alignment.hpp>
33 : #include <xlnt/styles/border.hpp>
34 : #include <xlnt/styles/conditional_format.hpp>
35 : #include <xlnt/styles/fill.hpp>
36 : #include <xlnt/styles/font.hpp>
37 : #include <xlnt/styles/number_format.hpp>
38 : #include <xlnt/styles/protection.hpp>
39 : #include <xlnt/worksheet/cell_vector.hpp>
40 : #include <xlnt/worksheet/major_order.hpp>
41 : #include <xlnt/worksheet/range_iterator.hpp>
42 : #include <xlnt/worksheet/range_reference.hpp>
43 : #include <xlnt/worksheet/worksheet.hpp>
44 :
45 : namespace xlnt {
46 :
47 : class const_range_iterator;
48 : class range_iterator;
49 :
50 : /// <summary>
51 : /// A range is a 2D collection of cells with defined extens that can be iterated upon.
52 : /// </summary>
53 : class XLNT_API range
54 : {
55 : public:
56 : /// <summary>
57 : /// Alias for the iterator type
58 : /// </summary>
59 : using iterator = range_iterator;
60 :
61 : /// <summary>
62 : /// Alias for the const iterator type
63 : /// </summary>
64 : using const_iterator = const_range_iterator;
65 :
66 : /// <summary>
67 : /// Alias for the reverse iterator type
68 : /// </summary>
69 : using reverse_iterator = std::reverse_iterator<iterator>;
70 :
71 : /// <summary>
72 : /// Alias for the const reverse iterator type
73 : /// </summary>
74 : using const_reverse_iterator = std::reverse_iterator<const_iterator>;
75 :
76 : /// <summary>
77 : /// Constructs a range on the given worksheet.
78 : /// </summary>
79 : range(worksheet ws, const range_reference &reference,
80 : major_order order = major_order::row, bool skip_null = false);
81 :
82 : /// <summary>
83 : /// Desctructor
84 : /// </summary>
85 : ~range();
86 :
87 : /// <summary>
88 : /// Default copy constructor.
89 : /// </summary>
90 CBC 6 : range(const range &) = default;
91 :
92 : /// <summary>
93 : /// Erases all cell data from the worksheet for cells within this range.
94 : /// </summary>
95 : void clear_cells();
96 :
97 : /// <summary>
98 : /// Returns a vector pointing to the n-th row or column in this range (depending
99 : /// on major order).
100 : /// </summary>
101 : cell_vector vector(std::size_t n);
102 :
103 : /// <summary>
104 : /// Returns a vector pointing to the n-th row or column in this range (depending
105 : /// on major order).
106 : /// </summary>
107 : const cell_vector vector(std::size_t n) const;
108 :
109 : /// <summary>
110 : /// Returns a cell in the range relative to its top left cell.
111 : /// </summary>
112 : class cell cell(const cell_reference &ref);
113 :
114 : /// <summary>
115 : /// Returns a cell in the range relative to its top left cell.
116 : /// </summary>
117 : const class cell cell(const cell_reference &ref) const;
118 :
119 : /// <summary>
120 : /// The worksheet this range targets
121 : /// </summary>
122 : const worksheet &target_worksheet() const;
123 :
124 : /// <summary>
125 : /// Returns the reference defining the bounds of this range.
126 : /// </summary>
127 : range_reference reference() const;
128 :
129 : /// <summary>
130 : /// Returns the number of rows or columns in this range (depending on major order).
131 : /// </summary>
132 : std::size_t length() const;
133 :
134 : /// <summary>
135 : /// Returns true if the given cell exists in the parent worksheet of this range.
136 : /// </summary>
137 : bool contains(const cell_reference &ref);
138 :
139 : /// <summary>
140 : /// Sets the alignment of all cells in the range to new_alignment and returns the range.
141 : /// </summary>
142 : range alignment(const xlnt::alignment &new_alignment);
143 :
144 : /// <summary>
145 : /// Sets the border of all cells in the range to new_border and returns the range.
146 : /// </summary>
147 : range border(const xlnt::border &new_border);
148 :
149 : /// <summary>
150 : /// Sets the fill of all cells in the range to new_fill and returns the range.
151 : /// </summary>
152 : range fill(const xlnt::fill &new_fill);
153 :
154 : /// <summary>
155 : /// Sets the font of all cells in the range to new_font and returns the range.
156 : /// </summary>
157 : range font(const xlnt::font &new_font);
158 :
159 : /// <summary>
160 : /// Sets the number format of all cells in the range to new_number_format and
161 : /// returns the range.
162 : /// </summary>
163 : range number_format(const xlnt::number_format &new_number_format);
164 :
165 : /// <summary>
166 : /// Sets the protection of all cells in the range to new_protection and returns the range.
167 : /// </summary>
168 : range protection(const xlnt::protection &new_protection);
169 :
170 : /// <summary>
171 : /// Sets the named style applied to all cells in this range to a style named style_name.
172 : /// </summary>
173 : range style(const class style &new_style);
174 :
175 : /// <summary>
176 : /// Sets the named style applied to all cells in this range to a style named style_name.
177 : /// If this style has not been previously created in the workbook, a
178 : /// key_not_found exception will be thrown.
179 : /// </summary>
180 : range style(const std::string &style_name);
181 :
182 : /// <summary>
183 : ///
184 : /// </summary>
185 : xlnt::conditional_format conditional_format(const condition &when);
186 :
187 : /// <summary>
188 : /// Returns the first row or column in this range.
189 : /// </summary>
190 : cell_vector front();
191 :
192 : /// <summary>
193 : /// Returns the first row or column in this range.
194 : /// </summary>
195 : const cell_vector front() const;
196 :
197 : /// <summary>
198 : /// Returns the last row or column in this range.
199 : /// </summary>
200 : cell_vector back();
201 :
202 : /// <summary>
203 : /// Returns the last row or column in this range.
204 : /// </summary>
205 : const cell_vector back() const;
206 :
207 : /// <summary>
208 : /// Returns an iterator to the first row or column in this range.
209 : /// </summary>
210 : iterator begin();
211 :
212 : /// <summary>
213 : /// Returns an iterator to one past the last row or column in this range.
214 : /// </summary>
215 : iterator end();
216 :
217 : /// <summary>
218 : /// Returns an iterator to the first row or column in this range.
219 : /// </summary>
220 : const_iterator begin() const;
221 :
222 : /// <summary>
223 : /// Returns an iterator to one past the last row or column in this range.
224 : /// </summary>
225 : const_iterator end() const;
226 :
227 : /// <summary>
228 : /// Returns an iterator to the first row or column in this range.
229 : /// </summary>
230 : const_iterator cbegin() const;
231 :
232 : /// <summary>
233 : /// Returns an iterator to one past the last row or column in this range.
234 : /// </summary>
235 : const_iterator cend() const;
236 :
237 : /// <summary>
238 : /// Returns a reverse iterator to the first row or column in this range.
239 : /// </summary>
240 : reverse_iterator rbegin();
241 :
242 : /// <summary>
243 : /// Returns a reverse iterator to one past the last row or column in this range.
244 : /// </summary>
245 : reverse_iterator rend();
246 :
247 : /// <summary>
248 : /// Returns a reverse iterator to the first row or column in this range.
249 : /// </summary>
250 : const_reverse_iterator rbegin() const;
251 :
252 : /// <summary>
253 : /// Returns a reverse iterator to one past the last row or column in this range.
254 : /// </summary>
255 : const_reverse_iterator rend() const;
256 :
257 : /// <summary>
258 : /// Returns a reverse iterator to the first row or column in this range.
259 : /// </summary>
260 : const_reverse_iterator crbegin() const;
261 :
262 : /// <summary>
263 : /// Returns a reverse iterator to one past the last row or column in this range.
264 : /// </summary>
265 : const_reverse_iterator crend() const;
266 :
267 : /// <summary>
268 : /// Applies function f to all cells in the range
269 : /// </summary>
270 : void apply(std::function<void(class cell)> f);
271 :
272 : /// <summary>
273 : /// Returns the n-th row or column in this range.
274 : /// </summary>
275 : cell_vector operator[](std::size_t n);
276 :
277 : /// <summary>
278 : /// Returns the n-th row or column in this range.
279 : /// </summary>
280 : const cell_vector operator[](std::size_t n) const;
281 :
282 : /// <summary>
283 : /// Returns true if this range is equivalent to comparand.
284 : /// </summary>
285 : bool operator==(const range &comparand) const;
286 :
287 : /// <summary>
288 : /// Returns true if this range is not equivalent to comparand.
289 : /// </summary>
290 : bool operator!=(const range &comparand) const;
291 :
292 : private:
293 : /// <summary>
294 : /// The worksheet this range is within
295 : /// </summary>
296 : class worksheet ws_;
297 :
298 : /// <summary>
299 : /// The reference of this range
300 : /// </summary>
301 : range_reference ref_;
302 :
303 : /// <summary>
304 : /// Whether this range should be iterated by columns or rows first
305 : /// </summary>
306 : major_order order_;
307 :
308 : /// <summary>
309 : /// Whether null rows/columns and cells should be skipped during iteration
310 : /// </summary>
311 : bool skip_null_;
312 : };
313 :
314 : } // namespace xlnt
|