// 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 #include #include #include namespace xlnt { class cell; class cell_reference; class cell_vector; class column_properties; class comment; class condition; class conditional_format; class const_range_iterator; class footer; class header; class range; class range_iterator; class range_reference; class relationship; class row_properties; class workbook; struct date; namespace detail { class xlsx_consumer; class xlsx_producer; struct worksheet_impl; } // namespace detail /// /// A worksheet is a 2D array of cells starting with cell A1 in the top-left corner /// and extending indefinitely down and right as needed. /// class XLNT_API worksheet { public: /// /// Iterate over a non-const worksheet with an iterator of this type. /// using iterator = range_iterator; /// /// Iterate over a non-const worksheet with an iterator of this type. /// using const_iterator = const_range_iterator; /// /// Iterate in reverse over a non-const worksheet with an iterator of this type. /// using reverse_iterator = std::reverse_iterator; /// /// Iterate in reverse order over a const worksheet with an iterator of this type. /// using const_reverse_iterator = std::reverse_iterator; /// /// Construct a null worksheet. No methods should be called on such a worksheet. /// worksheet(); /// /// Copy constructor. This worksheet will point to the same memory as rhs's worksheet. /// worksheet(const worksheet &rhs); /// /// Returns a reference to the workbook this worksheet is owned by. /// class workbook &workbook(); /// /// Returns a reference to the workbook this worksheet is owned by. /// const class workbook &workbook() const; /// /// Deletes data held in the worksheet that does not affect the internal data or display. /// For example, unreference styles and empty cells will be removed. /// void garbage_collect(); // identification /// /// Returns the unique numeric identifier of this worksheet. This will sometimes but not necessarily /// be the index of the worksheet in the workbook. /// std::size_t id() const; /// /// Set the unique numeric identifier. The id defaults to the lowest unused id in the workbook /// so this should not be called without a good reason. /// void id(std::size_t id); /// /// Returns the title of this sheet. /// std::string title() const; /// /// Sets the title of this sheet. /// void title(const std::string &title); // freeze panes /// /// Returns the top left corner of the region above and to the left of which panes are frozen. /// cell_reference frozen_panes() const; /// /// Freeze panes above and to the left of top_left_cell. /// void freeze_panes(cell top_left_cell); /// /// Freeze panes above and to the left of top_left_coordinate. /// void freeze_panes(const cell_reference &top_left_coordinate); /// /// Remove frozen panes. The data in those panes will be unaffected--this affects only the view. /// void unfreeze_panes(); /// /// Returns true if this sheet has a frozen row, frozen column, or both. /// bool has_frozen_panes() const; // container /// /// Returns true if this sheet has an initialized cell at the given reference. /// bool has_cell(const cell_reference &reference) const; /// /// Returns the cell at the given reference. If the cell doesn't exist, it /// will be initialized to null before being returned. /// class cell cell(const cell_reference &reference); /// /// Returns the cell at the given reference. If the cell doesn't exist, an /// invalid_parameter exception will be thrown. /// const class cell cell(const cell_reference &reference) const; /// /// Returns the cell at the given column and row. If the cell doesn't exist, it /// will be initialized to null before being returned. /// class cell cell(column_t column, row_t row); /// /// Returns the cell at the given column and row. If the cell doesn't exist, an /// invalid_parameter exception will be thrown. /// const class cell cell(column_t column, row_t row) const; /// /// Returns the range defined by reference string. If reference string is the name of /// a previously-defined named range in the sheet, it will be returned. /// class range range(const std::string &reference_string); /// /// Returns the range defined by reference string. If reference string is the name of /// a previously-defined named range in the sheet, it will be returned. /// const class range range(const std::string &reference_string) const; /// /// Returns the range defined by reference. /// class range range(const range_reference &reference); /// /// Returns the range defined by reference. /// const class range range(const range_reference &reference) const; /// /// Returns a range encompassing all cells in this sheet which will /// be iterated upon in row-major order. If skip_null is true (default), /// empty rows and cells will be skipped during iteration of the range. /// class range rows(bool skip_null = true); /// /// Returns a range encompassing all cells in this sheet which will /// be iterated upon in row-major order. If skip_null is true (default), /// empty rows and cells will be skipped during iteration of the range. /// const class range rows(bool skip_null = true) const; /// /// Returns a range ecompassing all cells in this sheet which will /// be iterated upon in column-major order. If skip_null is true (default), /// empty columns and cells will be skipped during iteration of the range. /// class range columns(bool skip_null = true); /// /// Returns a range ecompassing all cells in this sheet which will /// be iterated upon in column-major order. If skip_null is true (default), /// empty columns and cells will be skipped during iteration of the range. /// const class range columns(bool skip_null = true) const; //TODO: finish implementing cell_iterator wrapping before uncommenting //class cell_vector cells(bool skip_null = true); //TODO: finish implementing cell_iterator wrapping before uncommenting //const class cell_vector cells(bool skip_null = true) const; /// /// Clears memory used by the given cell. /// void clear_cell(const cell_reference &ref); /// /// Clears memory used by all cells in the given row. /// void clear_row(row_t row); // properties /// /// Returns the column properties for the given column. /// xlnt::column_properties &column_properties(column_t column); /// /// Returns the column properties for the given column. /// const xlnt::column_properties &column_properties(column_t column) const; /// /// Returns true if column properties have been set for the given column. /// bool has_column_properties(column_t column) const; /// /// Sets column properties for the given column to props. /// void add_column_properties(column_t column, const class column_properties &props); /// /// Calculates the width of the given column. This will be the default column width if /// a custom width is not set on this column's column_properties. /// double column_width(column_t column) const; /// /// Returns the row properties for the given row. /// xlnt::row_properties &row_properties(row_t row); /// /// Returns the row properties for the given row. /// const xlnt::row_properties &row_properties(row_t row) const; /// /// Returns true if row properties have been set for the given row. /// bool has_row_properties(row_t row) const; /// /// Sets row properties for the given row to props. /// void add_row_properties(row_t row, const class row_properties &props); /// /// Calculate the height of the given row. This will be the default row height if /// a custom height is not set on this row's row_properties. /// double row_height(row_t row) const; // positioning /// /// Returns a reference to the cell at the given point coordinates. /// cell_reference point_pos(int left, int top) const; // named range /// /// Creates a new named range with the given name encompassing the string representing a range. /// void create_named_range(const std::string &name, const std::string &reference_string); /// /// Creates a new named range with the given name encompassing the given range reference. /// void create_named_range(const std::string &name, const range_reference &reference); /// /// Returns true if this worksheet contains a named range with the given name. /// bool has_named_range(const std::string &name) const; /// /// Returns the named range with the given name. Throws key_not_found /// exception if the named range doesn't exist. /// class range named_range(const std::string &name); /// /// Returns the named range with the given name. Throws key_not_found /// exception if the named range doesn't exist. /// const class range named_range(const std::string &name) const; /// /// Removes a named range with the given name. /// void remove_named_range(const std::string &name); // extents /// /// Returns the row of the first non-empty cell in the worksheet. /// row_t lowest_row() const; /// /// Returns the row of the first non-empty cell or lowest row with properties in the worksheet. /// row_t lowest_row_or_props() const; /// /// Returns the row of the last non-empty cell in the worksheet. /// row_t highest_row() const; /// /// Returns the row of the last non-empty cell or highest row with properties in the worksheet. /// row_t highest_row_or_props() const; /// /// Returns the row directly below the last non-empty cell in the worksheet. /// row_t next_row() const; /// /// Returns the column of the first non-empty cell in the worksheet. /// column_t lowest_column() const; /// /// Returns the column of the first non-empty cell or lowest column with properties in the worksheet. /// column_t lowest_column_or_props() const; /// /// Returns the column of the last non-empty cell in the worksheet. /// column_t highest_column() const; /// /// Returns the column of the last non-empty cell or highest column with properties in the worksheet. /// column_t highest_column_or_props() const; /// /// Returns a range_reference pointing to the full range of non-empty cells in the worksheet. /// range_reference calculate_dimension() const; // cell merge /// /// Merges the cells within the range represented by the given string. /// void merge_cells(const std::string &reference_string); /// /// Merges the cells within the given range. /// void merge_cells(const range_reference &reference); /// /// Removes the merging of the cells in the range represented by the given string. /// void unmerge_cells(const std::string &reference_string); /// /// Removes the merging of the cells in the given range. /// void unmerge_cells(const range_reference &reference); /// /// Returns a vector of references of all merged ranges in the worksheet. /// std::vector merged_ranges() const; // operators /// /// Returns true if this worksheet refers to the same worksheet as other. /// bool operator==(const worksheet &other) const; /// /// Returns true if this worksheet doesn't refer to the same worksheet as other. /// bool operator!=(const worksheet &other) const; /// /// Returns true if this worksheet is null. /// bool operator==(std::nullptr_t) const; /// /// Returns true if this worksheet is not null. /// bool operator!=(std::nullptr_t) const; /// /// Sets the internal pointer of this worksheet object to point to other. /// void operator=(const worksheet &other); /// /// Convenience method for worksheet::cell method. /// class cell operator[](const cell_reference &reference); /// /// Convenience method for worksheet::cell method. /// const class cell operator[](const cell_reference &reference) const; /// /// Returns true if this worksheet is equal to other. If reference is true, the comparison /// will only check that both worksheets point to the same sheet in the same workbook. /// bool compare(const worksheet &other, bool reference) const; // page /// /// Returns true if this worksheet has a page setup. /// bool has_page_setup() const; /// /// Returns the page setup for this sheet. /// xlnt::page_setup page_setup() const; /// /// Sets the page setup for this sheet to setup. /// void page_setup(const struct page_setup &setup); /// /// Returns true if this page has margins. /// bool has_page_margins() const; /// /// Returns the margins of this sheet. /// xlnt::page_margins page_margins() const; /// /// Sets the margins of this sheet to margins. /// void page_margins(const class page_margins &margins); // auto filter /// /// Returns the current auto-filter of this sheet. /// range_reference auto_filter() const; /// /// Sets the auto-filter of this sheet to the range defined by range_string. /// void auto_filter(const std::string &range_string); /// /// Sets the auto-filter of this sheet to the given range. /// void auto_filter(const xlnt::range &range); /// /// Sets the auto-filter of this sheet to the range defined by reference. /// void auto_filter(const range_reference &reference); /// /// Clear the auto-filter from this sheet. /// void clear_auto_filter(); /// /// Returns true if this sheet has an auto-filter set. /// bool has_auto_filter() const; /// /// Reserve n rows. This can be optionally called before adding many rows /// to improve performance. /// void reserve(std::size_t n); /// /// Returns true if this sheet has a header/footer. /// bool has_header_footer() const; /// /// Returns the header/footer of this sheet. /// class header_footer header_footer() const; /// /// Sets the header/footer of this sheet to new_header_footer. /// void header_footer(const class header_footer &new_header_footer); /// /// Returns the sheet state of this sheet. /// xlnt::sheet_state sheet_state() const; /// /// Sets the sheet state of this sheet. /// void sheet_state(xlnt::sheet_state state); /// /// Returns an iterator to the first row in this sheet. /// iterator begin(); /// /// Returns an iterator one past the last row in this sheet. /// iterator end(); /// /// Return a constant iterator to the first row in this sheet. /// const_iterator begin() const; /// /// Returns a constant iterator to one past the last row in this sheet. /// const_iterator end() const; /// /// Return a constant iterator to the first row in this sheet. /// const_iterator cbegin() const; /// /// Returns a constant iterator to one past the last row in this sheet. /// const_iterator cend() const; /// /// Sets rows to repeat at top during printing. /// void print_title_rows(row_t first_row, row_t last_row); /// /// Sets rows to repeat at top during printing. /// void print_title_rows(row_t last_row); /// /// Sets columns to repeat at left during printing. /// void print_title_cols(column_t first_column, column_t last_column); /// /// Sets columns to repeat at left during printing. /// void print_title_cols(column_t last_column); /// /// Returns a string representation of the defined print titles. /// std::string print_titles() const; /// /// Sets the print area of this sheet to print_area. /// void print_area(const std::string &print_area); /// /// Returns the print area defined for this sheet. /// range_reference print_area() const; /// /// Returns true if this sheet has any number of views defined. /// bool has_view() const; /// /// Returns the view at the given index. /// sheet_view view(std::size_t index = 0) const; /// /// Adds new_view to the set of available views for this sheet. /// void add_view(const sheet_view &new_view); /// /// Set the active cell on the default worksheet view to the given reference. /// void active_cell(const cell_reference &ref); /// /// Returns true if the worksheet has a view and the view has an active cell. /// bool has_active_cell() const; /// /// Returns the active cell on the default worksheet view. /// cell_reference active_cell() const; // page breaks /// /// Remove all manual column and row page breaks (represented as dashed /// blue lines in the page view in Excel). /// void clear_page_breaks(); /// /// Returns vector where each element represents a row which will break a page below it. /// const std::vector &page_break_rows() const; /// /// Add a page break at the given row. /// void page_break_at_row(row_t row); /// /// Returns vector where each element represents a column which will break a page to the right. /// const std::vector &page_break_columns() const; /// /// Add a page break at the given column. /// void page_break_at_column(column_t column); /// /// Creates a conditional format for the given range with the given condition. /// xlnt::conditional_format conditional_format(const range_reference &ref, const condition &when); private: friend class cell; friend class const_range_iterator; friend class range_iterator; friend class workbook; friend class detail::xlsx_consumer; friend class detail::xlsx_producer; /// /// Constructs a worksheet impl wrapper from d. /// worksheet(detail::worksheet_impl *d); /// /// Creates a comments part in the manifest as a relationship target of this sheet. /// void register_comments_in_manifest(); /// /// Creates a calcChain part in the manifest if it doesn't already exist. /// void register_calc_chain_in_manifest(); /// /// Removes calcChain part from manifest if no formulae remain in workbook. /// void garbage_collect_formulae(); /// /// Sets the parent of this worksheet to wb. /// void parent(class workbook &wb); /// /// The pointer to this sheet's implementation. /// detail::worksheet_impl *d_; }; } // namespace xlnt