xlnt - community edition
selection.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 <vector>
28 
29 #include <xlnt/xlnt_config.hpp>
30 #include <xlnt/cell/cell_reference.hpp>
31 #include <xlnt/worksheet/pane.hpp>
32 #include <xlnt/worksheet/range_reference.hpp>
33 #include <xlnt/utils/exceptions.hpp>
34 #include <xlnt/utils/optional.hpp>
35 
36 namespace xlnt {
37 
41 class XLNT_API selection
42 {
43 public:
47  explicit selection() = default;
48 
53  explicit selection(pane_corner quadrant, cell_reference active_cell)
54  : active_cell_(active_cell), sqref_({range_reference(active_cell, active_cell)}), pane_(quadrant)
55  {
56  }
57 
62  explicit selection(pane_corner quadrant, cell_reference active_cell, range_reference selected)
63  : active_cell_(active_cell), sqref_({selected}), pane_(quadrant)
64  {
65  }
66 
70  bool has_active_cell() const
71  {
72  return active_cell_.is_set();
73  }
74 
81  {
82  if (!active_cell_.is_set())
83  {
84  throw xlnt::invalid_attribute("selection has no active cell");
85  }
86  return active_cell_.get();
87  }
88 
92  void active_cell(const cell_reference &ref)
93  {
94  active_cell_ = ref;
95  }
96 
101  {
102  active_cell_.clear();
103  }
104 
108  bool has_sqref() const
109  {
110  return !sqref_.empty();
111  }
112 
123  XLNT_DEPRECATED range_reference sqref() const
124  {
125  if (!has_sqref())
126  throw invalid_attribute("the selection has no sqref");
127 
128  return sqref_.front();
129  }
130 
134  const std::vector<range_reference>& sqrefs() const
135  {
136  return sqref_;
137  }
138 
142  void sqref(const range_reference &ref)
143  {
144  sqref_ = {ref};
145  }
146 
150  void sqref(const std::vector<range_reference> &ref)
151  {
152  sqref_ = ref;
153  }
154 
160  void sqref(const std::string &ref);
161 
166  {
167  return pane_;
168  }
169 
173  void pane(pane_corner corner)
174  {
175  pane_ = corner;
176  }
177 
182  bool operator==(const selection &rhs) const
183  {
184  return active_cell_ == rhs.active_cell_
185  && sqref_ == rhs.sqref_
186  && pane_ == rhs.pane_;
187  }
188 
189  // <summary>
192  bool operator!=(const selection &rhs) const
193  {
194  return !(*this == rhs);
195  }
196 
197 private:
201  optional<cell_reference> active_cell_;
202 
207  std::vector<range_reference> sqref_;
208 
212  pane_corner pane_ = pane_corner::top_left;
213 };
214 
215 } // namespace xlnt
void sqref(const range_reference &ref)
Sets the range encompassed by this selection.
Definition: selection.hpp:142
bool has_sqref() const
Returns true if this selection has a defined sqref.
Definition: selection.hpp:108
pane_corner pane() const
Returns the sheet quadrant of this selection.
Definition: selection.hpp:165
void pane(pane_corner corner)
Sets the sheet quadrant of this selection to corner.
Definition: selection.hpp:173
The selected area of a worksheet.
Definition: selection.hpp:41
Enumerates the possible types a cell can be determined by it&#39;s current value.
Definition: cell.hpp:36
const std::vector< range_reference > & sqrefs() const
Returns the range encompassed by this selection.
Definition: selection.hpp:134
selection(pane_corner quadrant, cell_reference active_cell, range_reference selected)
ctor with selected range sqref must contain active_cell
Definition: selection.hpp:62
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
Exception when getting a class&#39;s attribute before being set/initialized, or when setting a class&#39;s at...
Definition: exceptions.hpp:171
void active_cell(const cell_reference &ref)
Sets the active cell to that pointed to by ref.
Definition: selection.hpp:92
void clear_active_cell()
Clears the active cell.
Definition: selection.hpp:100
pane_corner
Enumeration of the four quadrants of a worksheet
Definition: pane.hpp:48
Many settings in xlnt are allowed to not have a value set. This class encapsulates a value which may ...
Definition: format.hpp:44
bool has_active_cell() const
Returns true if this selection has a defined active cell.
Definition: selection.hpp:70
bool operator==(const selection &rhs) const
Returns true if this selection is equal to rhs based on its active cell, sqref, and pane...
Definition: selection.hpp:182
selection(pane_corner quadrant, cell_reference active_cell)
ctor when no range selected sqref == active_cell
Definition: selection.hpp:53
bool operator!=(const selection &rhs) const
Definition: selection.hpp:192
A range_reference describes a rectangular area of a worksheet with positive width and height defined ...
Definition: range_reference.hpp:36
cell_reference active_cell() const
Returns the cell reference of the active cell. Assumes that this selection has an active cell (please...
Definition: selection.hpp:80
void sqref(const std::vector< range_reference > &ref)
Sets the range encompassed by this selection.
Definition: selection.hpp:150
XLNT_DEPRECATED range_reference sqref() const
Returns the range encompassed by this selection. If the range contains multiple (non-contiguous) regi...
Definition: selection.hpp:123