xlnt
worksheet.hpp
1 // Copyright (c) 2014-2022 Thomas Fussell
2 // Copyright (c) 2010-2015 openpyxl
3 // Copyright (c) 2024 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 <iterator>
29 #include <memory>
30 #include <string>
31 #include <unordered_map>
32 #include <vector>
33 
34 #include <xlnt/xlnt_config.hpp>
35 #include <xlnt/cell/index_types.hpp>
36 #include <xlnt/packaging/relationship.hpp>
37 #include <xlnt/worksheet/page_margins.hpp>
38 #include <xlnt/worksheet/page_setup.hpp>
39 #include <xlnt/worksheet/sheet_view.hpp>
40 
41 namespace xlnt {
42 
43 class cell;
44 class cell_reference;
45 class cell_vector;
46 class column_properties;
47 class comment;
48 class condition;
49 class conditional_format;
50 class const_range_iterator;
51 class footer;
52 class header;
53 class range;
54 class range_iterator;
55 class range_reference;
56 class relationship;
57 class row_properties;
58 class sheet_format_properties;
59 class workbook;
60 class phonetic_pr;
61 
62 struct date;
63 
64 namespace detail {
65 
66 class xlsx_consumer;
67 class xlsx_producer;
68 
69 struct worksheet_impl;
70 
71 } // namespace detail
72 
77 class XLNT_API worksheet
78 {
79 public:
84 
89 
93  using reverse_iterator = std::reverse_iterator<iterator>;
94 
98  using const_reverse_iterator = std::reverse_iterator<const_iterator>;
99 
103  worksheet();
104 
108  worksheet(const worksheet &rhs);
109 
113  class workbook &workbook();
114 
118  const class workbook &workbook() const;
119 
124  void garbage_collect();
125 
126  // identification
127 
132  std::size_t id() const;
133 
138  void id(std::size_t id);
139 
143  std::string title() const;
144 
148  void title(const std::string &title);
149 
150  // freeze panes
151 
155  cell_reference frozen_panes() const;
156 
160  void freeze_panes(cell top_left_cell);
161 
165  void freeze_panes(const cell_reference &top_left_coordinate);
166 
170  void unfreeze_panes();
171 
175  bool has_frozen_panes() const;
176 
177  // container
178 
182  bool has_cell(const cell_reference &reference) const;
183 
188  class cell cell(const cell_reference &reference);
189 
194  const class cell cell(const cell_reference &reference) const;
195 
200  class cell cell(column_t column, row_t row);
201 
206  const class cell cell(column_t column, row_t row) const;
207 
212  class range range(const std::string &reference_string);
213 
218  const class range range(const std::string &reference_string) const;
219 
223  class range range(const range_reference &reference);
224 
228  const class range range(const range_reference &reference) const;
229 
236  class range rows(bool skip_null = true);
237 
244  const class range rows(bool skip_null = true) const;
245 
252  class range columns(bool skip_null = true);
253 
260  const class range columns(bool skip_null = true) const;
261 
262  //TODO: finish implementing cell_iterator wrapping before uncommenting
263  //class cell_vector cells(bool skip_null = true);
264 
265  //TODO: finish implementing cell_iterator wrapping before uncommenting
266  //const class cell_vector cells(bool skip_null = true) const;
267 
271  void clear_cell(const cell_reference &ref);
272 
276  void clear_row(row_t row);
277 
281  void insert_rows(row_t row, std::uint32_t amount);
282 
286  void insert_columns(column_t column, std::uint32_t amount);
287 
291  void delete_rows(row_t row, std::uint32_t amount);
292 
296  void delete_columns(column_t column, std::uint32_t amount);
297 
298  // properties
299 
303  xlnt::column_properties &column_properties(column_t column);
304 
308  const xlnt::column_properties &column_properties(column_t column) const;
309 
313  bool has_column_properties(column_t column) const;
314 
318  void add_column_properties(column_t column, const class column_properties &props);
319 
324  double column_width(column_t column) const;
325 
330 
334  const xlnt::row_properties &row_properties(row_t row) const;
335 
339  bool has_row_properties(row_t row) const;
340 
344  void add_row_properties(row_t row, const class row_properties &props);
345 
350  double row_height(row_t row) const;
351 
352  // positioning
353 
357  cell_reference point_pos(int left, int top) const;
358 
359  // named range
360 
364  void create_named_range(const std::string &name, const std::string &reference_string);
365 
369  void create_named_range(const std::string &name, const range_reference &reference);
370 
374  bool has_named_range(const std::string &name) const;
375 
380  class range named_range(const std::string &name);
381 
386  const class range named_range(const std::string &name) const;
387 
391  void remove_named_range(const std::string &name);
392 
393  // extents
394 
398  row_t lowest_row() const;
399 
403  row_t lowest_row_or_props() const;
404 
408  row_t highest_row() const;
409 
413  row_t highest_row_or_props() const;
414 
418  row_t next_row() const;
419 
423  column_t lowest_column() const;
424 
428  column_t lowest_column_or_props() const;
429 
433  column_t highest_column() const;
434 
438  column_t highest_column_or_props() const;
439 
446  range_reference calculate_dimension(bool skip_null=true, bool skip_row_props=false) const;
447 
448  // cell merge
449 
453  void merge_cells(const std::string &reference_string);
454 
458  void merge_cells(const range_reference &reference);
459 
463  void unmerge_cells(const std::string &reference_string);
464 
468  void unmerge_cells(const range_reference &reference);
469 
473  std::vector<range_reference> merged_ranges() const;
474 
475  // operators
476 
480  bool operator==(const worksheet &other) const;
481 
485  bool operator!=(const worksheet &other) const;
486 
490  bool operator==(std::nullptr_t) const;
491 
495  bool operator!=(std::nullptr_t) const;
496 
500  void operator=(const worksheet &other);
501 
505  class cell operator[](const cell_reference &reference);
506 
510  const class cell operator[](const cell_reference &reference) const;
511 
516  bool compare(const worksheet &other, bool reference) const;
517 
518  // page
519 
523  bool has_page_setup() const;
524 
529 
533  void page_setup(const struct page_setup &setup);
534 
538  bool has_page_margins() const;
539 
544 
548  void page_margins(const class page_margins &margins);
549 
550  // auto filter
551 
555  range_reference auto_filter() const;
556 
560  void auto_filter(const std::string &range_string);
561 
565  void auto_filter(const xlnt::range &range);
566 
570  void auto_filter(const range_reference &reference);
571 
575  void clear_auto_filter();
576 
580  bool has_auto_filter() const;
581 
586  void reserve(std::size_t n);
587 
591  bool has_phonetic_properties() const;
592 
596  const phonetic_pr &phonetic_properties() const;
597 
601  void phonetic_properties(const phonetic_pr &phonetic_props);
602 
606  bool has_header_footer() const;
607 
611  class header_footer header_footer() const;
612 
616  void header_footer(const class header_footer &new_header_footer);
617 
622 
626  void sheet_state(xlnt::sheet_state state);
627 
631  iterator begin();
632 
636  iterator end();
637 
641  const_iterator begin() const;
642 
646  const_iterator end() const;
647 
651  const_iterator cbegin() const;
652 
656  const_iterator cend() const;
657 
661  void print_title_rows(row_t start, row_t end);
662 
666  optional<std::pair<row_t, row_t>> print_title_rows() const;
667 
671  void print_title_cols(column_t start, column_t end);
672 
676  optional<std::pair<column_t, column_t>> print_title_cols() const;
677 
681  bool has_print_titles() const;
682 
686  void clear_print_titles();
687 
691  void print_area(const std::string &print_area);
692 
696  void clear_print_area();
697 
701  range_reference print_area() const;
702 
706  bool has_print_area() const;
707 
711  bool has_view() const;
712 
716  sheet_view &view(std::size_t index = 0) const;
717 
721  void add_view(const sheet_view &new_view);
722 
726  void active_cell(const cell_reference &ref);
727 
731  bool has_active_cell() const;
732 
736  cell_reference active_cell() const;
737 
738  // page breaks
739 
744  void clear_page_breaks();
745 
749  const std::vector<row_t> &page_break_rows() const;
750 
754  void page_break_at_row(row_t row);
755 
759  const std::vector<column_t> &page_break_columns() const;
760 
764  void page_break_at_column(column_t column);
765 
769  xlnt::conditional_format conditional_format(const range_reference &ref, const condition &when);
770 
774  xlnt::path path() const;
775 
779  relationship referring_relationship() const;
780 
784  sheet_format_properties format_properties() const;
785 
789  void format_properties(const sheet_format_properties &properties);
790 
794  bool has_drawing() const;
795 
800  bool is_empty() const;
801 
802 private:
803  friend class cell;
804  friend class const_range_iterator;
805  friend class range_iterator;
806  friend class workbook;
807  friend class detail::xlsx_consumer;
808  friend class detail::xlsx_producer;
809 
813  worksheet(detail::worksheet_impl *d);
814 
818  void register_comments_in_manifest();
819 
823  void register_calc_chain_in_manifest();
824 
828  void garbage_collect_formulae();
829 
833  void parent(class workbook &wb);
834 
839  void move_cells(std::uint32_t index, std::uint32_t amount, row_or_col_t row_or_col, bool reverse = false);
840 
844  detail::worksheet_impl *d_;
845 };
846 
847 } // 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
Represents an association between a source Package or part, and a target object which can be a part o...
Definition: relationship.hpp:103
std::uint32_t row_t
All rows should be referred to by an instance of this type.
Definition: index_types.hpp:41
Definition: cell_reference.hpp:262
Describes the margins around a worksheet for printing.
Definition: page_margins.hpp:35
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
The properties of a row in a worksheet.
Definition: row_properties.hpp:35
A worksheet is a 2D array of cells starting with cell A1 in the top-left corner and extending indefin...
Definition: worksheet.hpp:77
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
sheet_state
Defines how a worksheet appears in the workbook. A workbook must have at least one sheet which is vis...
Definition: page_setup.hpp:75
Describes a view of a worksheet. Worksheets can have multiple views which show the data differently...
Definition: sheet_view.hpp:49
std::reverse_iterator< const_iterator > const_reverse_iterator
Iterate in reverse order over a const worksheet with an iterator of this type.
Definition: worksheet.hpp:98
value is an ISO 8601 formatted date
Encapsulates a path that points to location in a filesystem.
Definition: path.hpp:38
Many settings in xlnt are allowed to not have a value set. This class encapsulates a value which may ...
Definition: format.hpp:44
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
Columns can be referred to as a string A,B,...Z,AA,AB,..,ZZ,AAA,...,ZZZ or as a 1-indexed index...
Definition: index_types.hpp:48
A 2D range of cells in a worksheet that is referred to by name. ws->range("A1:B2") could be replaced ...
Definition: named_range.hpp:42
Describes how a worksheet will be converted into a page during printing.
Definition: page_setup.hpp:85
General worksheet formatting properties.
Definition: sheet_format_properties.hpp:36
Properties applied to a column in a worksheet. Columns can have a size and a style.
Definition: column_properties.hpp:36
std::reverse_iterator< iterator > reverse_iterator
Iterate in reverse over a non-const worksheet with an iterator of this type.
Definition: worksheet.hpp:93
A range_reference describes a rectangular area of a worksheet with positive width and height defined ...
Definition: range_reference.hpp:36
workbook is the container for all other parts of the document.
Definition: workbook.hpp:92
Phonetic properties Element provides a collection of properties that affect display of East Asian Lan...
Definition: phonetic_pr.hpp:39