xlnt
range.hpp
1 // Copyright (c) 2014-2022 Thomas Fussell
2 // Copyright (c) 2024 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 <memory>
30 #include <string>
31 #include <vector>
32 
33 #include <xlnt/xlnt_config.hpp>
34 #include <xlnt/styles/alignment.hpp>
35 #include <xlnt/styles/border.hpp>
36 #include <xlnt/styles/conditional_format.hpp>
37 #include <xlnt/styles/fill.hpp>
38 #include <xlnt/styles/font.hpp>
39 #include <xlnt/styles/number_format.hpp>
40 #include <xlnt/styles/protection.hpp>
41 #include <xlnt/worksheet/cell_vector.hpp>
42 #include <xlnt/worksheet/major_order.hpp>
43 #include <xlnt/worksheet/range_iterator.hpp>
44 #include <xlnt/worksheet/range_reference.hpp>
45 #include <xlnt/worksheet/worksheet.hpp>
46 
47 namespace xlnt {
48 
49 class const_range_iterator;
50 class range_iterator;
51 
55 class XLNT_API range
56 {
57 public:
62 
67 
71  using reverse_iterator = std::reverse_iterator<iterator>;
72 
76  using const_reverse_iterator = std::reverse_iterator<const_iterator>;
77 
81  range(worksheet ws, const range_reference &reference,
82  major_order order = major_order::row, bool skip_null = false);
83 
87  ~range();
88 
92  range(const range &) = default;
93 
97  void clear_cells();
98 
103  cell_vector vector(std::size_t n);
104 
109  const cell_vector vector(std::size_t n) const;
110 
114  class cell cell(const cell_reference &ref);
115 
119  const class cell cell(const cell_reference &ref) const;
120 
124  const worksheet &target_worksheet() const;
125 
129  range_reference reference() const;
130 
134  std::size_t length() const;
135 
139  bool contains(const cell_reference &ref);
140 
144  range alignment(const xlnt::alignment &new_alignment);
145 
149  range border(const xlnt::border &new_border);
150 
154  range fill(const xlnt::fill &new_fill);
155 
159  range font(const xlnt::font &new_font);
160 
165  range number_format(const xlnt::number_format &new_number_format);
166 
170  range protection(const xlnt::protection &new_protection);
171 
175  range style(const class style &new_style);
176 
182  range style(const std::string &style_name);
183 
188 
192  cell_vector front();
193 
197  const cell_vector front() const;
198 
202  cell_vector back();
203 
207  const cell_vector back() const;
208 
212  iterator begin();
213 
217  iterator end();
218 
222  const_iterator begin() const;
223 
227  const_iterator end() const;
228 
232  const_iterator cbegin() const;
233 
237  const_iterator cend() const;
238 
242  reverse_iterator rbegin();
243 
247  reverse_iterator rend();
248 
252  const_reverse_iterator rbegin() const;
253 
257  const_reverse_iterator rend() const;
258 
262  const_reverse_iterator crbegin() const;
263 
267  const_reverse_iterator crend() const;
268 
272  void apply(std::function<void(class cell)> f);
273 
277  cell_vector operator[](std::size_t n);
278 
282  const cell_vector operator[](std::size_t n) const;
283 
287  bool operator==(const range &comparand) const;
288 
292  bool operator!=(const range &comparand) const;
293 
294 private:
298  class worksheet ws_;
299 
303  range_reference ref_;
304 
308  major_order order_;
309 
313  bool skip_null_;
314 };
315 
316 } // namespace xlnt
A range is a 2D collection of cells with defined extens that can be iterated upon.
Definition: range.hpp:55
A const version of range_iterator which does not allow modification to the dereferenced cell_vector...
Definition: range_iterator.hpp:163
Enumerates the possible types a cell can be determined by it&#39;s current value.
Definition: cell.hpp:37
Describes a conditional format that will be applied to all cells in the associated range that satisfy...
Definition: conditional_format.hpp:86
Describes the font style of a particular cell.
Definition: font.hpp:41
Definition: conditional_format.hpp:49
Describes the fill style of a particular cell.
Definition: fill.hpp:299
A worksheet is a 2D array of cells starting with cell A1 in the top-left corner and extending indefin...
Definition: worksheet.hpp:77
major_order
Defines whether iterating a range returns columns or rows sequentially.
Definition: major_order.hpp:34
Describes the border style of a particular cell.
Definition: border.hpp:94
bool operator==(std::nullptr_t, const cell &cell)
Returns true if this cell is uninitialized.
An object used to refer to a cell. References have two parts, the column and the row. In Excel, the reference string A1 refers to the top-left-most cell. A cell_reference can be initialized from a string of this form or a 1-indexed ordered pair of the form column, row.
Definition: cell_reference.hpp:60
Describes a unit of data in a worksheet at a specific coordinate and its associated properties...
Definition: cell.hpp:84
std::reverse_iterator< const_iterator > const_reverse_iterator
Alias for the const reverse iterator type
Definition: range.hpp:76
Describes the number formatting applied to text and numbers within a certain cell.
Definition: number_format.hpp:41
An iterator used by worksheet and range for traversing a 2D grid of cells by row/column then across t...
Definition: range_iterator.hpp:43
Alignment options that determine how text should be displayed within a cell.
Definition: alignment.hpp:63
A cell vector is a linear (1D) range of cells, either vertical or horizontal depending on the major o...
Definition: cell_vector.hpp:47
bool operator!=(const std::string &reference_string, const range_reference &ref)
Returns true if the string representation of the range is not equivalent to ref.
A range_reference describes a rectangular area of a worksheet with positive width and height defined ...
Definition: range_reference.hpp:36
std::reverse_iterator< iterator > reverse_iterator
Alias for the reverse iterator type
Definition: range.hpp:71
Describes the protection style of a particular cell.
Definition: protection.hpp:38
Describes a style which has a name and can be applied to multiple individual formats. In Excel this is a "Cell Style".
Definition: style.hpp:56