xlnt - community edition
cell_reference.hpp
1 // Copyright (c) 2014-2022 Thomas Fussell
2 // Copyright (c) 2024-2026 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 <xlnt/xlnt_config.hpp>
28 #include <xlnt/cell/index_types.hpp>
29 
30 #include <cstdint>
31 #include <functional>
32 #include <string>
33 #include <utility>
34 
35 namespace xlnt {
36 
37 class cell_reference;
38 class range_reference;
39 
44 struct XLNT_API cell_reference_hash
45 {
49  std::size_t operator()(const cell_reference &k) const;
50 };
51 
62 class XLNT_API cell_reference
63 {
64 public:
68  static std::pair<std::string, row_t> split_reference(const std::string &reference_string);
69 
76  static std::pair<std::string, row_t> split_reference(
77  const std::string &reference_string, bool &absolute_column, bool &absolute_row);
78 
79  // constructors
80 
85 
86  // TODO: should these be explicit? The implicit conversion is nice sometimes.
87 
97  cell_reference(const char *reference_string);
98 
108  cell_reference(const std::string &reference_string);
109 
116  cell_reference(column_t column, row_t row);
117 
118  // absoluteness
119 
133  cell_reference &make_absolute(bool absolute_column = true, bool absolute_row = true);
134 
138  bool column_absolute() const;
139 
144  void column_absolute(bool absolute_column);
145 
149  bool row_absolute() const;
150 
155  void row_absolute(bool absolute_row);
156 
157  // getters/setters
158 
163  column_t column() const;
164 
171  void column(const std::string &column_string);
172 
176  column_t::index_t column_index() const;
177 
181  void column_index(column_t column);
182 
186  row_t row() const;
187 
191  void row(row_t row);
192 
199  cell_reference make_offset(int column_offset, int row_offset) const;
200 
204  std::string to_string() const;
205 
209  range_reference to_range() const;
210 
211  // operators
212 
218  range_reference operator,(const cell_reference &other) const;
219 
224  bool operator==(const cell_reference &comparand) const;
225 
230  bool operator==(const std::string &reference_string) const;
231 
236  bool operator==(const char *reference_string) const;
237 
242  bool operator!=(const cell_reference &comparand) const;
243 
248  bool operator!=(const std::string &reference_string) const;
249 
254  bool operator!=(const char *reference_string) const;
255 
256 private:
264  column_t column_;
265 
273  row_t row_;
274 
278  bool absolute_row_ = false;
279 
283  bool absolute_column_ = false;
284 };
285 
286 } // namespace xlnt
287 
288 namespace std {
289 template <>
290 struct hash<xlnt::cell_reference>
291 {
292  size_t operator()(const xlnt::cell_reference &x) const
293  {
294  static_assert(std::is_same<decltype(x.row()), std::uint32_t>::value, "this hash function expects both row and column to be 32-bit numbers");
295  static_assert(std::is_same<decltype(x.column_index()), std::uint32_t>::value, "this hash function expects both row and column to be 32-bit numbers");
296  return hash<std::uint64_t>{}(x.row() | static_cast<std::uint64_t>(x.column_index()) << 32);
297  }
298 };
299 } // namespace std
std::uint32_t index_t
Alias declaration for the internal numeric type of this column.
Definition: index_types.hpp:59
std::uint32_t row_t
All rows should be referred to by an instance of this type. By default, row references range from 1 t...
Definition: index_types.hpp:43
Definition: cell_reference.hpp:288
Enumerates the possible types a cell can be determined by it&#39;s current value.
Definition: cell.hpp:36
bool operator!=(std::nullptr_t, const cell &cell)
Returns true if this cell is initialized.
Functor for hashing a cell reference. Allows for use of std::unordered_set<cell_reference, cel_reference_hash> and similar.
Definition: cell_reference.hpp:44
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. By default, cell references range from column A1–A1048576 (column A:A) to column XFD1–XFD1048576 (column XFD:XFD, column index 16384). The OOXML specification allows to extend this range, and XLNT allows both rows and columns between 1 and 4294967295. Please note that not all applications might support these extended ranges.
Definition: cell_reference.hpp:62
column_t::index_t column_index() const
Returns a 1-indexed numeric index of the column of this reference.
Columns can be referred to as a string A,B,...Z,AA,AB,..,ZZ,AAA,...,XFD or as a 1-indexed index (indi...
Definition: index_types.hpp:53
A range_reference describes a rectangular area of a worksheet with positive width and height defined ...
Definition: range_reference.hpp:36
row_t row() const
Returns a 1-indexed numeric index of the row of this reference.