xlnt
workbook.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 <functional>
29 #include <iterator>
30 #include <map>
31 #include <memory>
32 #include <string>
33 #include <unordered_map>
34 #include <utility>
35 #include <vector>
36 
37 #include <xlnt/xlnt_config.hpp>
38 #include <xlnt/cell/rich_text.hpp>
39 
40 namespace xlnt {
41 
42 enum class calendar;
43 enum class core_property;
44 enum class extended_property;
45 enum class relationship_type;
46 
47 class alignment;
48 class border;
49 class calculation_properties;
50 class cell;
51 class cell_style;
52 class color;
53 class const_worksheet_iterator;
54 class fill;
55 class font;
56 class format;
57 class rich_text;
58 class manifest;
59 class metadata_property;
60 class named_range;
61 class number_format;
62 class path;
63 class pattern_fill;
64 class protection;
65 class range;
66 class range_reference;
67 class relationship;
68 class streaming_workbook_reader;
69 class style;
70 class style_serializer;
71 class theme;
72 class variant;
73 class workbook_view;
74 class worksheet;
75 class worksheet_iterator;
76 class zip_file;
77 
78 struct datetime;
79 
80 namespace detail {
81 
82 struct stylesheet;
83 struct workbook_impl;
84 class xlsx_consumer;
85 class xlsx_producer;
86 
87 } // namespace detail
88 
92 class XLNT_API workbook
93 {
94 public:
100 
106 
112  using reverse_iterator = std::reverse_iterator<iterator>;
113 
119  using const_reverse_iterator = std::reverse_iterator<const_iterator>;
120 
125  static workbook empty();
126 
127  // Constructors
128 
133  workbook();
134 
138  workbook(const xlnt::path &file);
139 
143  workbook(const xlnt::path &file, const std::string &password);
144 
148  workbook(std::istream &data);
149 
153  workbook(std::istream &data, const std::string &password);
154 
158  workbook(workbook &&other);
159 
163  workbook(const workbook &other);
164 
170  ~workbook();
171 
172  // Worksheets
173 
177  worksheet create_sheet();
178 
182  worksheet create_sheet(std::size_t index);
183 
187  worksheet create_sheet_with_rel(const std::string &title, const relationship &rel);
188 
193  worksheet copy_sheet(worksheet worksheet);
194 
199  worksheet copy_sheet(worksheet worksheet, std::size_t index);
200 
205  worksheet active_sheet();
206 
211  void active_sheet(std::size_t index);
212 
218  worksheet sheet_by_title(const std::string &title);
219 
225  const worksheet sheet_by_title(const std::string &title) const;
226 
231  worksheet sheet_by_index(std::size_t index);
232 
237  const worksheet sheet_by_index(std::size_t index) const;
238 
243  worksheet sheet_by_id(std::size_t id);
244 
249  const worksheet sheet_by_id(std::size_t id) const;
250 
256  bool sheet_hidden_by_index(std::size_t index) const;
257 
261  bool contains(const std::string &title) const;
262 
266  std::size_t index(worksheet worksheet);
267 
268  // remove worksheets
269 
273  void remove_sheet(worksheet worksheet);
274 
279  void clear();
280 
281  // iterators
282 
286  iterator begin();
287 
293  iterator end();
294 
298  const_iterator begin() const;
299 
305  const_iterator end() const;
306 
310  const_iterator cbegin() const;
311 
317  const_iterator cend() const;
318 
322  void apply_to_cells(std::function<void(cell)> f);
323 
328  std::vector<std::string> sheet_titles() const;
329 
333  std::size_t sheet_count() const;
334 
335  // Metadata Properties
336 
340  bool has_core_property(xlnt::core_property type) const;
341 
346  std::vector<xlnt::core_property> core_properties() const;
347 
352 
356  void core_property(xlnt::core_property type, const variant &value);
357 
361  bool has_extended_property(xlnt::extended_property type) const;
362 
367  std::vector<xlnt::extended_property> extended_properties() const;
368 
373 
377  void extended_property(xlnt::extended_property type, const variant &value);
378 
382  bool has_custom_property(const std::string &property_name) const;
383 
388  std::vector<std::string> custom_properties() const;
389 
393  variant custom_property(const std::string &property_name) const;
394 
398  void custom_property(const std::string &property_name, const variant &value);
399 
405  calendar base_date() const;
406 
411  void base_date(calendar base_date);
412 
416  bool has_title() const;
417 
421  std::string title() const;
422 
426  void title(const std::string &title);
427 
431  void abs_path(const std::string &path);
432 
436  void arch_id_flags(const std::size_t flags);
437 
438  // Named Ranges
439 
443  std::vector<xlnt::named_range> named_ranges() const;
444 
448  void create_named_range(const std::string &name, worksheet worksheet, const range_reference &reference);
449 
453  void create_named_range(const std::string &name, worksheet worksheet, const std::string &reference_string);
454 
458  bool has_named_range(const std::string &name) const;
459 
463  class range named_range(const std::string &name);
464 
468  void remove_named_range(const std::string &name);
469 
470  // Serialization/Deserialization
471 
476  void save(std::vector<std::uint8_t> &data) const;
477 
482  void save(std::vector<std::uint8_t> &data, const std::string &password) const;
483 
488  void save(const std::string &filename) const;
489 
494  void save(const std::string &filename, const std::string &password) const;
495 
496 #ifdef _MSC_VER
497  void save(const std::wstring &filename) const;
502 
507  void save(const std::wstring &filename, const std::string &password) const;
508 #endif
509 
514  void save(const xlnt::path &filename) const;
515 
520  void save(const xlnt::path &filename, const std::string &password) const;
521 
525  void save(std::ostream &stream) const;
526 
531  void save(std::ostream &stream, const std::string &password) const;
532 
537  void load(const std::vector<std::uint8_t> &data);
538 
543  void load(const std::vector<std::uint8_t> &data, const std::string &password);
544 
549  void load(const std::string &filename);
550 
555  void load(const std::string &filename, const std::string &password);
556 
557 #ifdef _MSC_VER
558  void load(const std::wstring &filename);
563 
568  void load(const std::wstring &filename, const std::string &password);
569 #endif
570 
575  void load(const xlnt::path &filename);
576 
581  void load(const xlnt::path &filename, const std::string &password);
582 
587  void load(std::istream &stream);
588 
593  void load(std::istream &stream, const std::string &password);
594 
595  // View
596 
600  bool has_view() const;
601 
605  workbook_view view() const;
606 
610  void view(const workbook_view &view);
611 
612  // Properties
613 
617  bool has_code_name() const;
618 
622  std::string code_name() const;
623 
627  void code_name(const std::string &code_name);
628 
632  bool has_file_version() const;
633 
637  std::string app_name() const;
638 
642  std::size_t last_edited() const;
643 
647  std::size_t lowest_edited() const;
648 
652  std::size_t rup_build() const;
653 
654  // Theme
655 
659  bool has_theme() const;
660 
664  const xlnt::theme &theme() const;
665 
669  void theme(const class theme &value);
670 
671  // Formats
672 
677  xlnt::format format(std::size_t format_index);
678 
683  const xlnt::format format(std::size_t format_index) const;
684 
688  xlnt::format create_format(bool default_format = false);
689 
694  void clear_formats();
695 
696  // Styles
697 
701  bool has_style(const std::string &name) const;
702 
706  class style style(const std::string &name);
707 
711  const class style style(const std::string &name) const;
712 
716  class style create_style(const std::string &name);
717 
721  class style create_builtin_style(std::size_t builtin_id);
722 
728  void clear_styles();
729 
733  void default_slicer_style(const std::string &value);
734 
738  std::string default_slicer_style() const;
739 
743  void enable_known_fonts();
744 
748  void disable_known_fonts();
749 
753  bool known_fonts_enabled() const;
754 
755  // Manifest
756 
760  class manifest &manifest();
761 
765  const class manifest &manifest() const;
766 
767  // shared strings
768 
775  std::size_t add_shared_string(const rich_text &shared, bool allow_duplicates = false);
776 
780  const rich_text &shared_strings(std::size_t index) const;
781 
786  std::vector<rich_text> &shared_strings();
787 
792  const std::vector<rich_text> &shared_strings() const;
793 
794  // Thumbnail
795 
800  void thumbnail(const std::vector<std::uint8_t> &thumbnail,
801  const std::string &extension, const std::string &content_type);
802 
806  const std::vector<std::uint8_t> &thumbnail() const;
807 
811  const std::unordered_map<std::string, std::vector<std::uint8_t>>& binaries() const;
812 
813  // Calculation properties
814 
818  bool has_calculation_properties() const;
819 
824 
828  void calculation_properties(const class calculation_properties &props);
829 
830  // Operators
831 
836  workbook &operator=(workbook other);
837 
841  worksheet operator[](const std::string &name);
842 
846  worksheet operator[](std::size_t index);
847 
852  bool operator==(const workbook &rhs) const;
853 
858  bool operator!=(const workbook &rhs) const;
859 
860 private:
861  friend class streaming_workbook_reader;
862  friend class worksheet;
863  friend class detail::xlsx_consumer;
864  friend class detail::xlsx_producer;
865 
870  workbook(detail::workbook_impl *impl);
871 
876  detail::workbook_impl &impl();
877 
882  const detail::workbook_impl &impl() const;
883 
889  void register_package_part(relationship_type type);
890 
897  void register_workbook_part(relationship_type type);
898 
905  void register_worksheet_part(worksheet ws, relationship_type type);
906 
910  void garbage_collect_formulae();
911 
915  void update_sheet_properties();
916 
920  void swap(workbook &other);
921 
925  void reorder_relationships();
926 
930  std::unique_ptr<detail::workbook_impl> d_;
931 };
932 
933 } // namespace xlnt
core_property
Every core property in a workbook must be one of these types.
Definition: metadata_property.hpp:34
A range is a 2D collection of cells with defined extens that can be iterated upon.
Definition: range.hpp:55
Represents an association between a source Package or part, and a target object which can be a part o...
Definition: relationship.hpp:103
Represents an object that can have variable type.
Definition: variant.hpp:40
Definition: cell_reference.hpp:262
A workbook can be opened in multiple windows with different views. This class represents a particular...
Definition: workbook_view.hpp:37
Enumerates the possible types a cell can be determined by it&#39;s current value.
Definition: cell.hpp:37
extended_property
Every extended property in a workbook must be one of these types.
Definition: metadata_property.hpp:56
std::reverse_iterator< iterator > reverse_iterator
typedef for the iterator used for iterating through this workbook (non-const) in a range-based for lo...
Definition: workbook.hpp:112
A worksheet is a 2D array of cells starting with cell A1 in the top-left corner and extending indefin...
Definition: worksheet.hpp:77
A theme is a combination of fonts, colors, and effects. This isn&#39;t really supported yet...
Definition: theme.hpp:35
Describes a unit of data in a worksheet at a specific coordinate and its associated properties...
Definition: cell.hpp:84
Encapsulates zero or more formatted text runs where a text run is a string of text with the same defi...
Definition: rich_text.hpp:41
An iterator which is used to iterate over the worksheets in a workbook.
Definition: worksheet_iterator.hpp:44
Encapsulates a path that points to location in a filesystem.
Definition: path.hpp:38
relationship_type
All package relationships must be one of these defined types.
Definition: relationship.hpp:54
Workbook file properties relating to calculations.
Definition: calculation_properties.hpp:34
Describes the formatting of a particular cell.
Definition: format.hpp:58
An iterator which is used to iterate over the worksheets in a const workbook.
Definition: worksheet_iterator.hpp:154
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
workbook is the container for all other parts of the document.
Definition: streaming_workbook_reader.hpp:55
The manifest keeps track of all files in the OOXML package and their type and relationships.
Definition: manifest.hpp:41
A range_reference describes a rectangular area of a worksheet with positive width and height defined ...
Definition: range_reference.hpp:36
Describes a style which has a name and can be applied to multiple individual formats. In Excel this is a "Cell Style".
Definition: style.hpp:56
std::reverse_iterator< const_iterator > const_reverse_iterator
typedef for the iterator used for iterating through this workbook (const) in a range-based for loop i...
Definition: workbook.hpp:119
workbook is the container for all other parts of the document.
Definition: workbook.hpp:92
calendar
An enumeration of possible base dates. Dates in Excel are stored as days since this base date...
Definition: calendar.hpp:35