xlnt - community edition
sheet_view.hpp
1 // Copyright (c) 2014-2022 Thomas Fussell
2 // Copyright (c) 2010-2015 openpyxl
3 // Copyright (c) 2024-2026 xlnt-community
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 // THE SOFTWARE
22 //
23 // @license: http://www.opensource.org/licenses/mit-license.php
24 // @author: see AUTHORS file
25 
26 #pragma once
27 
28 #include <xlnt/xlnt_config.hpp>
29 #include <xlnt/utils/optional.hpp>
30 #include <xlnt/worksheet/pane.hpp>
31 #include <xlnt/worksheet/selection.hpp>
32 
33 namespace xlnt {
34 
38 enum class sheet_view_type
39 {
40  normal,
41  page_break_preview,
42  page_layout
43 };
44 
49 class XLNT_API sheet_view
50 {
51 public:
55  void id(std::size_t new_id)
56  {
57  id_ = new_id;
58  }
59 
63  std::size_t id() const
64  {
65  return id_;
66  }
67 
71  bool has_pane() const
72  {
73  return pane_.is_set();
74  }
75 
81  struct pane &pane()
82  {
83  if (!pane_.is_set())
84  {
85  throw xlnt::invalid_attribute("sheet view has no pane");
86  }
87  return pane_.get();
88  }
89 
95  const struct pane &pane() const
96  {
97  if (!pane_.is_set())
98  {
99  throw xlnt::invalid_attribute("sheet view has no pane");
100  }
101  return pane_.get();
102  }
103 
107  void clear_pane()
108  {
109  pane_.clear();
110  }
111 
115  void pane(const struct pane &new_pane)
116  {
117  pane_ = new_pane;
118  }
119 
123  bool has_selections() const
124  {
125  return !selections_.empty();
126  }
127 
131  void add_selection(const class selection &new_selection)
132  {
133  selections_.push_back(new_selection);
134  }
135 
140  {
141  selections_.clear();
142  }
143 
147  std::vector<xlnt::selection> selections() const
148  {
149  return selections_;
150  }
151 
156  class xlnt::selection &selection(std::size_t index)
157  {
158  if (index >= selections_.size())
159  {
160  throw xlnt::invalid_parameter("sheet view selection at index " + std::to_string(index) + " does not exist (" + std::to_string(selections_.size()) + " selections available)");
161  }
162 
163  return selections_.at(index);
164  }
165 
169  void show_grid_lines(bool show)
170  {
171  show_grid_lines_ = show;
172  }
173 
177  bool show_grid_lines() const
178  {
179  return show_grid_lines_;
180  }
181 
185  void default_grid_color(bool is_default)
186  {
187  default_grid_color_ = is_default;
188  }
189 
193  bool default_grid_color() const
194  {
195  return default_grid_color_;
196  }
197 
201  void type(sheet_view_type new_type)
202  {
203  type_ = new_type;
204  }
205 
210  {
211  return type_;
212  }
213 
217  bool has_top_left_cell() const
218  {
219  return top_left_cell_.is_set();
220  }
221 
225  void top_left_cell(const cell_reference &ref)
226  {
227  top_left_cell_.set(ref);
228  }
229 
236  {
237  if (!top_left_cell_.is_set())
238  {
239  throw xlnt::invalid_attribute("sheet view has no top left cell");
240  }
241  return top_left_cell_.get();
242  }
243 
247  void zoom_scale(int scale)
248  {
249  zoom_scale_ = scale;
250  }
251 
255  int zoom_scale() const
256  {
257  return zoom_scale_;
258  }
259 
264  bool operator==(const sheet_view &rhs) const
265  {
266  return id_ == rhs.id_
267  && show_grid_lines_ == rhs.show_grid_lines_
268  && default_grid_color_ == rhs.default_grid_color_
269  && pane_ == rhs.pane_
270  && selections_ == rhs.selections_
271  && top_left_cell_ == rhs.top_left_cell_
272  && zoom_scale_ == rhs.zoom_scale_;
273  }
274 
278  bool operator!=(const sheet_view &rhs) const
279  {
280  return !(*this == rhs);
281  }
282 
283 private:
287  std::size_t id_ = 0;
288 
292  bool show_grid_lines_ = true;
293 
297  bool default_grid_color_ = true;
298 
302  sheet_view_type type_ = sheet_view_type::normal;
303 
307  optional<xlnt::pane> pane_;
308 
312  optional<cell_reference> top_left_cell_;
313 
317  std::vector<xlnt::selection> selections_;
318 
322  int zoom_scale_ = 100;
323 };
324 
325 } // namespace xlnt
void clear_selections()
Removes all selections.
Definition: sheet_view.hpp:139
void top_left_cell(const cell_reference &ref)
Sets the top left cell of this view.
Definition: sheet_view.hpp:225
void zoom_scale(int scale)
Sets the zoom scale (percentage) for this view.
Definition: sheet_view.hpp:247
bool show_grid_lines() const
Returns true if grid lines will be shown for sheets using this view.
Definition: sheet_view.hpp:177
class xlnt::selection & selection(std::size_t index)
Returns the selection at the given index (please call selections().size() if the number of selections...
Definition: sheet_view.hpp:156
struct pane & pane()
Returns a reference to this view&#39;s pane. Assumes that this view has a pane (please call has_pane() to...
Definition: sheet_view.hpp:81
std::size_t id() const
Returns the ID of this view.
Definition: sheet_view.hpp:63
void add_selection(const class selection &new_selection)
Adds the given selection to the collection of selections.
Definition: sheet_view.hpp:131
bool has_top_left_cell() const
has a top left cell?
Definition: sheet_view.hpp:217
void pane(const struct pane &new_pane)
Sets the pane of this view to new_pane.
Definition: sheet_view.hpp:115
cell_reference top_left_cell() const
Returns the top left cell of this view. Assumes that this view has a top left (please call has_top_le...
Definition: sheet_view.hpp:235
sheet_view_type type() const
Returns the type of this view.
Definition: sheet_view.hpp:209
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
bool operator!=(const sheet_view &rhs) const
Returns the negation of the equality operator.
Definition: sheet_view.hpp:278
void show_grid_lines(bool show)
If show is true, grid lines will be shown for sheets using this view.
Definition: sheet_view.hpp:169
sheet_view_type
Enumeration of possible types of sheet views
Definition: sheet_view.hpp:38
Exception for a bad parameter value
Definition: exceptions.hpp:69
bool has_selections() const
Returns true if this view has any selections.
Definition: sheet_view.hpp:123
bool operator==(const sheet_view &rhs) const
Returns true if this view is equal to rhs based on its id, grid lines setting, default grid color...
Definition: sheet_view.hpp:264
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
std::vector< xlnt::selection > selections() const
Returns the collection of selections as a vector.
Definition: sheet_view.hpp:147
Exception when getting a class&#39;s attribute before being set/initialized, or when setting a class&#39;s at...
Definition: exceptions.hpp:171
Describes a view of a worksheet. Worksheets can have multiple views which show the data differently...
Definition: sheet_view.hpp:49
void default_grid_color(bool is_default)
If is_default is true, the default grid color will be used.
Definition: sheet_view.hpp:185
bool has_pane() const
Returns true if this view has a pane defined.
Definition: sheet_view.hpp:71
void type(sheet_view_type new_type)
Sets the type of this view.
Definition: sheet_view.hpp:201
A fixed portion of a worksheet.
Definition: pane.hpp:59
void clear_pane()
Removes the defined pane from this view.
Definition: sheet_view.hpp:107
int zoom_scale() const
Returns the zoom scale (percentage).
Definition: sheet_view.hpp:255
void id(std::size_t new_id)
Sets the ID of this view to new_id.
Definition: sheet_view.hpp:55
const struct pane & pane() const
Returns a reference to this view&#39;s pane. Assumes that this view has a pane (please call has_pane() to...
Definition: sheet_view.hpp:95
bool default_grid_color() const
Returns true if the default grid color will be used.
Definition: sheet_view.hpp:193