// Copyright (c) 2014-2018 Thomas Fussell // Copyright (c) 2010-2015 openpyxl // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, WRISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE // // @license: http://www.opensource.org/licenses/mit-license.php // @author: see AUTHORS file #pragma once #include #include #include #include #include #include #include #include namespace xlnt { enum class calendar; enum class core_property; enum class extended_property; enum class relationship_type; class alignment; class border; class calculation_properties; class cell; class cell_style; class color; class const_worksheet_iterator; class drawing; class fill; class font; class format; class rich_text; class manifest; class metadata_property; class named_range; class number_format; class path; class pattern_fill; class protection; class range; class range_reference; class relationship; class streaming_workbook_reader; class style; class style_serializer; class theme; class variant; class workbook_view; class worksheet; class worksheet_iterator; class zip_file; struct datetime; namespace detail { struct stylesheet; struct workbook_impl; class xlsx_consumer; class xlsx_producer; } // namespace detail /// /// workbook is the container for all other parts of the document. /// class XLNT_API workbook { public: /// /// typedef for the iterator used for iterating through this workbook /// (non-const) in a range-based for loop. /// using iterator = worksheet_iterator; /// /// typedef for the iterator used for iterating through this workbook /// (const) in a range-based for loop. /// using const_iterator = const_worksheet_iterator; /// /// typedef for the iterator used for iterating through this workbook /// (non-const) in a range-based for loop in reverse order using /// std::make_reverse_iterator. /// using reverse_iterator = std::reverse_iterator; /// /// typedef for the iterator used for iterating through this workbook /// (const) in a range-based for loop in reverse order using /// std::make_reverse_iterator. /// using const_reverse_iterator = std::reverse_iterator; /// /// Constructs and returns an empty workbook similar to a default. /// Excel workbook /// static workbook empty(); // Constructors /// /// Default constructor. Constructs a workbook containing a single empty /// worksheet using workbook::empty(). /// workbook(); /// /// Move constructor. Constructs a workbook from existing workbook, other. /// workbook(workbook &&other); /// /// Copy constructor. Constructs this workbook from existing workbook, other. /// workbook(const workbook &other); /// /// Destroys this workbook, deallocating all internal storage space. Any pimpl /// wrapper classes (e.g. cell) pointing into this workbook will be invalid /// after this is executed. /// ~workbook(); // Worksheets /// /// Creates and returns a sheet after the last sheet in this workbook. /// worksheet create_sheet(); /// /// Creates and returns a sheet at the specified index. /// worksheet create_sheet(std::size_t index); /// /// TODO: This should be private... /// worksheet create_sheet_with_rel(const std::string &title, const relationship &rel); /// /// Creates and returns a new sheet after the last sheet initializing it /// with all of the data from the provided worksheet. /// worksheet copy_sheet(worksheet worksheet); /// /// Creates and returns a new sheet at the specified index initializing it /// with all of the data from the provided worksheet. /// worksheet copy_sheet(worksheet worksheet, std::size_t index); /// /// Returns the worksheet that is determined to be active. An active /// sheet is that which is initially shown by the spreadsheet editor. /// worksheet active_sheet(); /// /// Returns the worksheet with the given name. This may throw an exception /// if the sheet isn't found. Use workbook::contains(const std::string &) /// to make sure the sheet exists before calling this method. /// worksheet sheet_by_title(const std::string &title); /// /// Returns the worksheet with the given name. This may throw an exception /// if the sheet isn't found. Use workbook::contains(const std::string &) /// to make sure the sheet exists before calling this method. /// const worksheet sheet_by_title(const std::string &title) const; /// /// Returns the worksheet at the given index. This will throw an exception /// if index is greater than or equal to the number of sheets in this workbook. /// worksheet sheet_by_index(std::size_t index); /// /// Returns the worksheet at the given index. This will throw an exception /// if index is greater than or equal to the number of sheets in this workbook. /// const worksheet sheet_by_index(std::size_t index) const; /// /// Returns the worksheet with a sheetId of id. Sheet IDs are arbitrary numbers /// that uniquely identify a sheet. Most users won't need this. /// worksheet sheet_by_id(std::size_t id); /// /// Returns the worksheet with a sheetId of id. Sheet IDs are arbitrary numbers /// that uniquely identify a sheet. Most users won't need this. /// const worksheet sheet_by_id(std::size_t id) const; /// /// Returns true if this workbook contains a sheet with the given title. /// bool contains(const std::string &title) const; /// /// Returns the index of the given worksheet. The worksheet must be owned by this workbook. /// std::size_t index(worksheet worksheet); // remove worksheets /// /// Removes the given worksheet from this workbook. /// void remove_sheet(worksheet worksheet); /// /// Sets the contents of this workbook to be equivalent to that of /// a workbook returned by workbook::empty(). /// void clear(); // iterators /// /// Returns an iterator to the first worksheet in this workbook. /// iterator begin(); /// /// Returns an iterator to the worksheet following the last worksheet of the workbook. /// This worksheet acts as a placeholder; attempting to access it will cause an /// exception to be thrown. /// iterator end(); /// /// Returns a const iterator to the first worksheet in this workbook. /// const_iterator begin() const; /// /// Returns a const iterator to the worksheet following the last worksheet of the workbook. /// This worksheet acts as a placeholder; attempting to access it will cause an /// exception to be thrown. /// const_iterator end() const; /// /// Returns an iterator to the first worksheet in this workbook. /// const_iterator cbegin() const; /// /// Returns a const iterator to the worksheet following the last worksheet of the workbook. /// This worksheet acts as a placeholder; attempting to access it will cause an /// exception to be thrown. /// const_iterator cend() const; /// /// Applies the function "f" to every non-empty cell in every worksheet in this workbook. /// void apply_to_cells(std::function f); /// /// Returns a temporary vector containing the titles of each sheet in the order /// of the sheets in the workbook. /// std::vector sheet_titles() const; /// /// Returns the number of sheets in this workbook. /// std::size_t sheet_count() const; // Metadata Properties /// /// Returns true if the workbook has the core property with the given name. /// bool has_core_property(xlnt::core_property type) const; /// /// Returns a vector of the type of each core property that is set to /// a particular value in this workbook. /// std::vector core_properties() const; /// /// Returns the value of the given core property. /// variant core_property(xlnt::core_property type) const; /// /// Sets the given core property to the provided value. /// void core_property(xlnt::core_property type, const variant &value); /// /// Returns true if the workbook has the extended property with the given name. /// bool has_extended_property(xlnt::extended_property type) const; /// /// Returns a vector of the type of each extended property that is set to /// a particular value in this workbook. /// std::vector extended_properties() const; /// /// Returns the value of the given extended property. /// variant extended_property(xlnt::extended_property type) const; /// /// Sets the given extended property to the provided value. /// void extended_property(xlnt::extended_property type, const variant &value); /// /// Returns true if the workbook has the custom property with the given name. /// bool has_custom_property(const std::string &property_name) const; /// /// Returns a vector of the name of each custom property that is set to /// a particular value in this workbook. /// std::vector custom_properties() const; /// /// Returns the value of the given custom property. /// variant custom_property(const std::string &property_name) const; /// /// Creates a new custom property in this workbook and sets it to the provided value. /// void custom_property(const std::string &property_name, const variant &value); /// /// Returns the base date used by this workbook. This will generally be windows_1900 /// except on Apple based systems when it will default to mac_1904 unless otherwise /// set via `void workbook::base_date(calendar base_date)`. /// calendar base_date() const; /// /// Sets the base date style of this workbook. This is the date and time that /// a numeric value of 0 represents. /// void base_date(calendar base_date); /// /// Returns true if this workbook has had its title set. /// bool has_title() const; /// /// Returns the title of this workbook. /// std::string title() const; /// /// Sets the title of this workbook to title. /// void title(const std::string &title); // Named Ranges /// /// Returns a vector of the named ranges in this workbook. /// std::vector named_ranges() const; /// /// Creates a new names range. /// void create_named_range(const std::string &name, worksheet worksheet, const range_reference &reference); /// /// Creates a new names range. /// void create_named_range(const std::string &name, worksheet worksheet, const std::string &reference_string); /// /// Returns true if a named range of the given name exists in the workbook. /// bool has_named_range(const std::string &name) const; /// /// Returns the named range with the given name. /// class range named_range(const std::string &name); /// /// Deletes the named range with the given name. /// void remove_named_range(const std::string &name); // Serialization/Deserialization /// /// Serializes the workbook into an XLSX file and saves the bytes into /// byte vector data. /// void save(std::vector &data) const; /// /// Serializes the workbook into an XLSX file encrypted with the given password /// and saves the bytes into byte vector data. /// void save(std::vector &data, const std::string &password) const; /// /// Serializes the workbook into an XLSX file and saves the data into a file /// named filename. /// void save(const std::string &filename) const; /// /// Serializes the workbook into an XLSX file encrypted with the given password /// and loads the bytes into a file named filename. /// void save(const std::string &filename, const std::string &password) const; #ifdef _MSC_VER /// /// Serializes the workbook into an XLSX file and saves the data into a file /// named filename. /// void save(const std::wstring &filename) const; /// /// Serializes the workbook into an XLSX file encrypted with the given password /// and loads the bytes into a file named filename. /// void save(const std::wstring &filename, const std::string &password) const; #endif /// /// Serializes the workbook into an XLSX file and saves the data into a file /// named filename. /// void save(const xlnt::path &filename) const; /// /// Serializes the workbook into an XLSX file encrypted with the given password /// and loads the bytes into a file named filename. /// void save(const xlnt::path &filename, const std::string &password) const; /// /// Serializes the workbook into an XLSX file and saves the data into stream. /// void save(std::ostream &stream) const; /// /// Serializes the workbook into an XLSX file encrypted with the given password /// and loads the bytes into the given stream. /// void save(std::ostream &stream, const std::string &password) const; /// /// Interprets byte vector data as an XLSX file and sets the content of this /// workbook to match that file. /// void load(const std::vector &data); /// /// Interprets byte vector data as an XLSX file encrypted with the /// given password and sets the content of this workbook to match that file. /// void load(const std::vector &data, const std::string &password); /// /// Interprets file with the given filename as an XLSX file and sets /// the content of this workbook to match that file. /// void load(const std::string &filename); /// /// Interprets file with the given filename as an XLSX file encrypted with the /// given password and sets the content of this workbook to match that file. /// void load(const std::string &filename, const std::string &password); #ifdef _MSC_VER /// /// Interprets file with the given filename as an XLSX file and sets /// the content of this workbook to match that file. /// void load(const std::wstring &filename); /// /// Interprets file with the given filename as an XLSX file encrypted with the /// given password and sets the content of this workbook to match that file. /// void load(const std::wstring &filename, const std::string &password); #endif /// /// Interprets file with the given filename as an XLSX file and sets the /// content of this workbook to match that file. /// void load(const xlnt::path &filename); /// /// Interprets file with the given filename as an XLSX file encrypted with the /// given password and sets the content of this workbook to match that file. /// void load(const xlnt::path &filename, const std::string &password); /// /// Interprets data in stream as an XLSX file and sets the content of this /// workbook to match that file. /// void load(std::istream &stream); /// /// Interprets data in stream as an XLSX file encrypted with the given password /// and sets the content of this workbook to match that file. /// void load(std::istream &stream, const std::string &password); // View /// /// Returns true if this workbook has a view. /// bool has_view() const; /// /// Returns the view. /// workbook_view view() const; /// /// Sets the view to view. /// void view(const workbook_view &view); // Properties /// /// Returns true if a code name has been set for this workbook. /// bool has_code_name() const; /// /// Returns the code name that was set for this workbook. /// std::string code_name() const; /// /// Sets the code name of this workbook to code_name. /// void code_name(const std::string &code_name); /// /// Returns true if this workbook has a file version. /// bool has_file_version() const; /// /// Returns the AppName workbook file property. /// std::string app_name() const; /// /// Returns the LastEdited workbook file property. /// std::size_t last_edited() const; /// /// Returns the LowestEdited workbook file property. /// std::size_t lowest_edited() const; /// /// Returns the RupBuild workbook file property. /// std::size_t rup_build() const; // Theme /// /// Returns true if this workbook has a theme defined. /// bool has_theme() const; /// /// Returns a const reference to this workbook's theme. /// const xlnt::theme &theme() const; /// /// Sets the theme to value. /// void theme(const class theme &value); // Formats /// /// Returns the cell format at the given index. The index is the position of /// the format in xl/styles.xml. /// xlnt::format format(std::size_t format_index); /// /// Returns the cell format at the given index. The index is the position of /// the format in xl/styles.xml. /// const xlnt::format format(std::size_t format_index) const; /// /// Creates a new format and returns it. /// xlnt::format create_format(bool default_format = false); /// /// Clear all cell-level formatting and formats from the styelsheet. This leaves /// all other styling in place (e.g. named styles). /// void clear_formats(); // Styles /// /// Returns true if this workbook has a style with a name of name. /// bool has_style(const std::string &name) const; /// /// Returns the named style with the given name. /// class style style(const std::string &name); /// /// Returns the named style with the given name. /// const class style style(const std::string &name) const; /// /// Creates a new style and returns it. /// class style create_style(const std::string &name); /// /// Creates a new style and returns it. /// class style create_builtin_style(std::size_t builtin_id); /// /// Clear all named styles from cells and remove the styles from /// from the styelsheet. This leaves all other styling in place /// (e.g. cell formats). /// void clear_styles(); // Manifest /// /// Returns a reference to the workbook's internal manifest. /// class manifest &manifest(); /// /// Returns a reference to the workbook's internal manifest. /// const class manifest &manifest() const; // shared strings /// /// Append a shared string to the shared string collection in this workbook. /// This should not generally be called unless you know what you're doing. /// If allow_duplicates is false and the string is already in the collection, /// it will not be added. Returns the index of the added string. /// std::size_t add_shared_string(const rich_text &shared, bool allow_duplicates = false); /// /// Returns a reference to the shared strings being used by cells /// in this workbook. /// std::vector &shared_strings(); /// /// Returns a reference to the shared strings being used by cells /// in this workbook. /// const std::vector &shared_strings() const; // Thumbnail /// /// Sets the workbook's thumbnail to the given vector of bytes, thumbnail, /// with the given extension (e.g. jpg) and content_type (e.g. image/jpeg). /// void thumbnail(const std::vector &thumbnail, const std::string &extension, const std::string &content_type); /// /// Returns a vector of bytes representing the workbook's thumbnail. /// const std::vector &thumbnail() const; // Calculation properties /// /// Returns true if this workbook has any calculation properties set. /// bool has_calculation_properties() const; /// /// Returns the calculation properties used in this workbook. /// class calculation_properties calculation_properties() const; /// /// Sets the calculation properties of this workbook to props. /// void calculation_properties(const class calculation_properties &props); // Operators /// /// Set the contents of this workbook to be equal to those of "other". /// Other is passed as value to allow for copy-swap idiom. /// workbook &operator=(workbook other); /// /// Return the worksheet with a title of "name". /// worksheet operator[](const std::string &name); /// /// Return the worksheet at "index". /// worksheet operator[](std::size_t index); /// /// Return true if this workbook internal implementation points to the same /// memory as rhs's. /// bool operator==(const workbook &rhs) const; /// /// Return true if this workbook internal implementation doesn't point to the same /// memory as rhs's. /// bool operator!=(const workbook &rhs) const; private: friend class streaming_workbook_reader; friend class worksheet; friend class detail::xlsx_consumer; friend class detail::xlsx_producer; /// /// Private constructor. Constructs a workbook from an implementation pointer. /// Used by static constructor to resolve circular construction dependency. /// workbook(detail::workbook_impl *impl); /// /// Returns a reference to the workbook implementation structure. Provides /// a nicer interface than constantly dereferencing workbook::d_. /// detail::workbook_impl &impl(); /// /// Returns a reference to the workbook implementation structure. Provides /// a nicer interface than constantly dereferencing workbook::d_. /// const detail::workbook_impl &impl() const; /// /// Adds a package-level part of the given type to the manifest if it doesn't /// already exist. The part will have a path and content type of the default /// for that particular relationship type. /// void register_package_part(relationship_type type); /// /// Adds a workbook-level part of the given type to the manifest if it doesn't /// already exist. The part will have a path and content type of the default /// for that particular relationship type. It will be a relationship target /// of this workbook. /// void register_workbook_part(relationship_type type); /// /// Adds a worksheet-level part of the given type to the manifest if it doesn't /// already exist. The part will have a path and content type of the default /// for that particular relationship type. It will be a relationship target /// of the given worksheet, ws. /// void register_worksheet_part(worksheet ws, relationship_type type); /// /// Removes calcChain part from manifest if no formulae remain in workbook. /// void garbage_collect_formulae(); /// /// Update extended workbook properties titlesOfParts and headingPairs when sheets change. /// void update_sheet_properties(); /// /// Swaps the data held in this workbook with workbook other. /// void swap(workbook &other); /// /// An opaque pointer to a structure that holds all of the data relating to this workbook. /// std::unique_ptr d_; }; } // namespace xlnt