xlnt - community edition
worksheet.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 <iterator>
29 #include <string>
30 #include <vector>
31 
32 #include <xlnt/xlnt_config.hpp>
33 #include <xlnt/cell/index_types.hpp>
34 #include <xlnt/packaging/relationship.hpp>
35 #include <xlnt/worksheet/page_margins.hpp>
36 #include <xlnt/worksheet/page_setup.hpp>
37 #include <xlnt/worksheet/sheet_view.hpp>
38 
39 namespace xlnt {
40 
41 class cell;
42 class cell_reference;
43 class cell_vector;
44 class column_properties;
45 class comment;
46 class condition;
47 class conditional_format;
48 class const_range_iterator;
49 class footer;
50 class header;
51 class range;
52 class range_iterator;
53 class range_reference;
54 class relationship;
55 class row_properties;
56 class sheet_format_properties;
57 class workbook;
58 class phonetic_pr;
59 
60 struct date;
61 
62 namespace detail {
63 
64 class xlsx_consumer;
65 class xlsx_producer;
66 
67 struct worksheet_impl;
68 
69 } // namespace detail
70 
75 class XLNT_API worksheet
76 {
77 public:
82 
87 
91  using reverse_iterator = std::reverse_iterator<iterator>;
92 
96  using const_reverse_iterator = std::reverse_iterator<const_iterator>;
97 
101  worksheet();
102 
106  worksheet(const worksheet &rhs);
107 
111  class workbook workbook();
112 
116  const class workbook workbook() const;
117 
122  void garbage_collect();
123 
124  // identification
125 
130  std::size_t id() const;
131 
136  void id(std::size_t id);
137 
141  std::string title() const;
142 
151  void title(const std::string &title);
152 
153  // freeze panes
154 
160  cell_reference frozen_panes() const;
161 
165  void freeze_panes(cell top_left_cell);
166 
170  void freeze_panes(const cell_reference &top_left_coordinate);
171 
175  void unfreeze_panes();
176 
180  bool has_frozen_panes() const;
181 
182  // container
183 
187  bool has_cell(const cell_reference &reference) const;
188 
194  class cell cell(const cell_reference &reference);
195 
201  const class cell cell(const cell_reference &reference) const;
202 
208  class cell cell(column_t column, row_t row);
209 
215  const class cell cell(column_t column, row_t row) const;
216 
221  class range range(const std::string &reference_string);
222 
227  const class range range(const std::string &reference_string) const;
228 
232  class range range(const range_reference &reference);
233 
237  const class range range(const range_reference &reference) const;
238 
245  class range rows(bool skip_null = true);
246 
253  const class range rows(bool skip_null = true) const;
254 
261  class range columns(bool skip_null = true);
262 
269  const class range columns(bool skip_null = true) const;
270 
271  //TODO: finish implementing cell_iterator wrapping before uncommenting
272  //class cell_vector cells(bool skip_null = true);
273 
274  //TODO: finish implementing cell_iterator wrapping before uncommenting
275  //const class cell_vector cells(bool skip_null = true) const;
276 
280  void clear_cell(const cell_reference &ref);
281 
285  void clear_row(row_t row);
286 
292  void insert_rows(row_t row, std::uint32_t amount);
293 
299  void insert_columns(column_t column, std::uint32_t amount);
300 
306  void delete_rows(row_t row, std::uint32_t amount);
307 
313  void delete_columns(column_t column, std::uint32_t amount);
314 
315  // properties
316 
322  xlnt::column_properties &column_properties(column_t column);
323 
329  const xlnt::column_properties &column_properties(column_t column) const;
330 
334  bool has_column_properties(column_t column) const;
335 
339  void add_column_properties(column_t column, const class column_properties &props);
340 
345  double column_width(column_t column) const;
346 
353 
359  const xlnt::row_properties &row_properties(row_t row) const;
360 
364  bool has_row_properties(row_t row) const;
365 
369  void add_row_properties(row_t row, const class row_properties &props);
370 
375  double row_height(row_t row) const;
376 
377  // positioning
378 
382  cell_reference point_pos(int left, int top) const;
383 
384  // named range
385 
389  void create_named_range(const std::string &name, const std::string &reference_string);
390 
394  void create_named_range(const std::string &name, const range_reference &reference);
395 
399  bool has_named_range(const std::string &name) const;
400 
406  class range named_range(const std::string &name);
407 
413  const class range named_range(const std::string &name) const;
414 
420  void remove_named_range(const std::string &name);
421 
422  // extents
423 
427  row_t lowest_row() const;
428 
432  row_t lowest_row_or_props() const;
433 
437  row_t highest_row() const;
438 
442  row_t highest_row_or_props() const;
443 
447  row_t next_row() const;
448 
452  column_t lowest_column() const;
453 
457  column_t lowest_column_or_props() const;
458 
462  column_t highest_column() const;
463 
467  column_t highest_column_or_props() const;
468 
475  range_reference calculate_dimension(bool skip_null=true, bool skip_row_props=false) const;
476 
477  // cell merge
478 
482  void merge_cells(const std::string &reference_string);
483 
487  void merge_cells(const range_reference &reference);
488 
494  void unmerge_cells(const std::string &reference_string);
495 
501  void unmerge_cells(const range_reference &reference);
502 
506  std::vector<range_reference> merged_ranges() const;
507 
508  // operators
509 
513  bool operator==(const worksheet &other) const;
514 
518  bool operator!=(const worksheet &other) const;
519 
523  bool operator==(std::nullptr_t) const;
524 
528  bool operator!=(std::nullptr_t) const;
529 
533  void operator=(const worksheet &other);
534 
541  class cell operator[](const cell_reference &reference);
542 
549  const class cell operator[](const cell_reference &reference) const;
550 
556  bool compare(const worksheet &other, bool compare_by_reference) const;
557 
558  // page
559 
563  bool has_page_setup() const;
564 
571 
575  void page_setup(const struct page_setup &setup);
576 
580  bool has_page_margins() const;
581 
588 
592  void page_margins(const class page_margins &margins);
593 
597  void clear_page_margins();
598 
599  // auto filter
600 
606  range_reference auto_filter() const;
607 
611  void auto_filter(const std::string &range_string);
612 
616  void auto_filter(const xlnt::range &range);
617 
621  void auto_filter(const range_reference &reference);
622 
626  void clear_auto_filter();
627 
631  bool has_auto_filter() const;
632 
637  void reserve(std::size_t n);
638 
642  bool has_phonetic_properties() const;
643 
649  phonetic_pr phonetic_properties() const;
650 
654  void phonetic_properties(const phonetic_pr &phonetic_props);
655 
659  bool has_header_footer() const;
660 
666  class header_footer header_footer() const;
667 
671  void header_footer(const class header_footer &new_header_footer);
672 
677 
681  void sheet_state(xlnt::sheet_state state);
682 
686  iterator begin();
687 
691  iterator end();
692 
696  const_iterator begin() const;
697 
701  const_iterator end() const;
702 
706  const_iterator cbegin() const;
707 
711  const_iterator cend() const;
712 
716  void print_title_rows(row_t start, row_t end);
717 
721  optional<std::pair<row_t, row_t>> print_title_rows() const;
722 
726  void print_title_cols(column_t start, column_t end);
727 
731  optional<std::pair<column_t, column_t>> print_title_cols() const;
732 
736  bool has_print_titles() const;
737 
741  void clear_print_titles();
742 
746  void print_area(const std::string &print_area);
747 
751  void clear_print_area();
752 
758  range_reference print_area() const;
759 
763  bool has_print_area() const;
764 
768  bool has_view() const;
769 
773  const std::vector<sheet_view> & views() const;
774 
780  sheet_view &view(std::size_t index = 0) const;
781 
785  void add_view(const sheet_view &new_view);
786 
792  void remove_view(std::size_t index);
793 
797  void clear_views();
798 
802  void active_cell(const cell_reference &ref);
803 
807  bool has_active_cell() const;
808 
814  cell_reference active_cell() const;
815 
816  // page breaks
817 
822  void clear_page_breaks();
823 
827  const std::vector<row_t> &page_break_rows() const;
828 
832  void page_break_at_row(row_t row);
833 
837  const std::vector<column_t> &page_break_columns() const;
838 
842  void page_break_at_column(column_t column);
843 
847  xlnt::conditional_format conditional_format(const range_reference &ref, const condition &when);
848 
852  xlnt::path path() const;
853 
857  relationship referring_relationship() const;
858 
862  sheet_format_properties format_properties() const;
863 
867  void format_properties(const sheet_format_properties &properties);
868 
872  bool has_drawing() const;
873 
878  bool is_empty() const;
879 
883  int zoom_scale() const;
884 
888  void zoom_scale(int scale);
889 
898  void outline_settings(bool visible, bool symbols_below, bool symbols_right, bool apply_styles);
899 
904  bool show_outline_symbols() const;
905 
911  bool summary_below() const;
912 
918  bool summary_right() const;
919 
924  bool apply_styles() const;
925 
926 private:
927  friend class cell;
928  friend class const_range_iterator;
929  friend class range_iterator;
930  friend class workbook;
931  friend class detail::xlsx_consumer;
932  friend class detail::xlsx_producer;
933 
937  worksheet(detail::worksheet_impl *d);
938 
942  void register_comments_in_manifest();
943 
947  void register_calc_chain_in_manifest();
948 
952  void garbage_collect_formulae();
953 
957  void parent(class workbook &wb);
958 
964  void move_cells(std::uint32_t index, std::uint32_t amount, row_or_col_t row_or_col, bool reverse = false);
965 
969  detail::worksheet_impl *d_ = nullptr;
970 };
971 
972 } // namespace xlnt
A range is a 2D collection of cells with defined extens that can be iterated upon.
Definition: range.hpp:53
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. By default, row references range from 1 t...
Definition: index_types.hpp:43
Definition: cell_reference.hpp:288
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:36
Describes a conditional format that will be applied to all cells in the associated range that satisfy...
Definition: conditional_format.hpp:90
The properties of a row in a worksheet.
Definition: row_properties.hpp:39
A worksheet is a 2D array of cells starting with cell A1 in the top-left corner and extending indefin...
Definition: worksheet.hpp:75
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
Describes a unit of data in a worksheet at a specific coordinate and its associated properties...
Definition: cell.hpp:83
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:82
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:96
value is an ISO 8601 formatted date
Encapsulates a path that points to location in a filesystem.
Definition: path.hpp:43
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,...,XFD or as a 1-indexed index (indi...
Definition: index_types.hpp:53
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:92
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:40
std::reverse_iterator< iterator > reverse_iterator
Iterate in reverse over a non-const worksheet with an iterator of this type.
Definition: worksheet.hpp:91
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:96
Phonetic properties Element provides a collection of properties that affect display of East Asian Lan...
Definition: phonetic_pr.hpp:39