// Copyright (c) 2014-2021 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, ARISING 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 namespace xlnt { enum class calendar; class alignment; class base_format; class border; class cell_reference; class comment; class fill; class font; class format; class number_format; class protection; class range; class relationship; class style; class workbook; class worksheet; class xlsx_consumer; class xlsx_producer; class phonetic_pr; struct date; struct datetime; struct time; struct timedelta; namespace detail { class xlsx_consumer; class xlsx_producer; struct cell_impl; } // namespace detail /// /// Describes a unit of data in a worksheet at a specific coordinate and its /// associated properties. /// /// /// Properties of interest include style, type, value, and address. /// The Cell class is required to know its value and type, display options, /// and any other features of an Excel cell.Utilities for referencing /// cells using Excel's 'A1' column/row nomenclature are also provided. /// class XLNT_API cell { public: /// /// Alias xlnt::cell_type to xlnt::cell::type since it looks nicer. /// using type = cell_type; /// /// Returns a map of error strings such as \#DIV/0! and their associated indices. /// static const std::unordered_map &error_codes(); /// /// Default copy constructor. /// cell(const cell &) = default; // value /// /// Returns true if value has been set and has not been cleared using cell::clear_value(). /// bool has_value() const; /// /// Returns the value of this cell as an instance of type T. /// Overloads exist for most C++ fundamental types like bool, int, etc. as well /// as for std::string and xlnt datetime types: date, time, datetime, and timedelta. /// template T value() const; /// /// Makes this cell have a value of type null. /// All other cell attributes are retained. /// void clear_value(); /// /// Sets the type of this cell to null. /// void value(std::nullptr_t); /// /// Sets the value of this cell to the given boolean value. /// void value(bool boolean_value); /// /// Sets the value of this cell to the given value. /// void value(int int_value); /// /// Sets the value of this cell to the given value. /// void value(unsigned int int_value); /// /// Sets the value of this cell to the given value. /// void value(long long int int_value); /// /// Sets the value of this cell to the given value. /// void value(unsigned long long int int_value); /// /// Sets the value of this cell to the given value. /// void value(float float_value); /// /// Sets the value of this cell to the given value. /// void value(double float_value); /// /// Sets the value of this cell to the given value. /// void value(const date &date_value); /// /// Sets the value of this cell to the given value. /// void value(const time &time_value); /// /// Sets the value of this cell to the given value. /// void value(const datetime &datetime_value); /// /// Sets the value of this cell to the given value. /// void value(const timedelta &timedelta_value); /// /// Sets the value of this cell to the given value. /// void value(const std::string &string_value); /// /// Sets the value of this cell to the given value. /// void value(const char *string_value); /// /// Sets the value of this cell to the given value. /// void value(const rich_text &text_value); /// /// Sets the value and formatting of this cell to that of other_cell. /// void value(const cell other_cell); /// /// Analyzes string_value to determine its type, convert it to that type, /// and set the value of this cell to that converted value. /// void value(const std::string &string_value, bool infer_type); /// /// Returns the type of this cell. /// type data_type() const; /// /// Sets the type of this cell. This should usually be done indirectly /// by setting the value of the cell to a value of that type. /// void data_type(type t); // properties /// /// There's no reason to keep a cell which has no value and is not a placeholder. /// Returns true if this cell has no value, style, isn't merged, etc. /// bool garbage_collectible() const; /// /// Returns true iff this cell's number format matches a date format. /// bool is_date() const; // position /// /// Returns a cell_reference that points to the location of this cell. /// cell_reference reference() const; /// /// Returns the column of this cell. /// column_t column() const; /// /// Returns the numeric index (A == 1) of the column of this cell. /// column_t::index_t column_index() const; /// /// Returns the row of this cell. /// row_t row() const; /// /// Returns the location of this cell as an ordered pair (left, top). /// std::pair anchor() const; // hyperlink /// /// Returns the relationship of this cell's hyperlink. /// class hyperlink hyperlink() const; /// /// Adds a hyperlink to this cell pointing to the URI of the given value and sets /// the text value of the cell to the given parameter. /// void hyperlink(const std::string &url, const std::string &display = ""); /// /// Adds an internal hyperlink to this cell pointing to the given cell. /// void hyperlink(xlnt::cell target, const std::string &display = ""); /// /// Adds an internal hyperlink to this cell pointing to the given range. /// void hyperlink(xlnt::range target, const std::string &display = ""); /// /// Returns true if this cell has a hyperlink set. /// bool has_hyperlink() const; // computed formatting /// /// Returns the alignment that should be used when displaying this cell /// graphically based on the workbook default, the cell-level format, /// and the named style applied to the cell in that order. /// class alignment computed_alignment() const; /// /// Returns the border that should be used when displaying this cell /// graphically based on the workbook default, the cell-level format, /// and the named style applied to the cell in that order. /// class border computed_border() const; /// /// Returns the fill that should be used when displaying this cell /// graphically based on the workbook default, the cell-level format, /// and the named style applied to the cell in that order. /// class fill computed_fill() const; /// /// Returns the font that should be used when displaying this cell /// graphically based on the workbook default, the cell-level format, /// and the named style applied to the cell in that order. /// class font computed_font() const; /// /// Returns the number format that should be used when displaying this cell /// graphically based on the workbook default, the cell-level format, /// and the named style applied to the cell in that order. /// class number_format computed_number_format() const; /// /// Returns the protection that should be used when displaying this cell /// graphically based on the workbook default, the cell-level format, /// and the named style applied to the cell in that order. /// class protection computed_protection() const; // format /// /// Returns true if this cell has had a format applied to it. /// bool has_format() const; /// /// Returns the format applied to this cell. If this cell has no /// format, an invalid_attribute exception will be thrown. /// const class format format() const; /// /// Applies the cell-level formatting of new_format to this cell. /// void format(const class format new_format); /// /// Removes the cell-level formatting from this cell. /// This doesn't affect the style that may also be applied to the cell. /// Throws an invalid_attribute exception if no format is applied. /// void clear_format(); /// /// Returns the number format of this cell. /// class number_format number_format() const; /// /// Creates a new format in the workbook, sets its number_format /// to the given format, and applies the format to this cell. /// void number_format(const class number_format &format); /// /// Returns the font applied to the text in this cell. /// class font font() const; /// /// Creates a new format in the workbook, sets its font /// to the given font, and applies the format to this cell. /// void font(const class font &font_); /// /// Returns the fill applied to this cell. /// class fill fill() const; /// /// Creates a new format in the workbook, sets its fill /// to the given fill, and applies the format to this cell. /// void fill(const class fill &fill_); /// /// Returns the border of this cell. /// class border border() const; /// /// Creates a new format in the workbook, sets its border /// to the given border, and applies the format to this cell. /// void border(const class border &border_); /// /// Returns the alignment of the text in this cell. /// class alignment alignment() const; /// /// Creates a new format in the workbook, sets its alignment /// to the given alignment, and applies the format to this cell. /// void alignment(const class alignment &alignment_); /// /// Returns the protection of this cell. /// class protection protection() const; /// /// Creates a new format in the workbook, sets its protection /// to the given protection, and applies the format to this cell. /// void protection(const class protection &protection_); // style /// /// Returns true if this cell has had a style applied to it. /// bool has_style() const; /// /// Returns a wrapper pointing to the named style applied to this cell. /// class style style(); /// /// Returns a wrapper pointing to the named style applied to this cell. /// const class style style() const; /// /// Sets the named style applied to this cell to a style named style_name. /// Equivalent to style(new_style.name()). /// void style(const class style &new_style); /// /// Sets the named style applied to this cell to a style named style_name. /// If this style has not been previously created in the workbook, a /// key_not_found exception will be thrown. /// void style(const std::string &style_name); /// /// Removes the named style from this cell. /// An invalid_attribute exception will be thrown if this cell has no style. /// This will not affect the cell format of the cell. /// void clear_style(); // formula /// /// Returns the string representation of the formula applied to this cell. /// std::string formula() const; /// /// Sets the formula of this cell to the given value. /// This formula string should begin with '='. /// void formula(const std::string &formula); /// /// Removes the formula from this cell. After this is called, has_formula() will return false. /// void clear_formula(); /// /// Returns true if this cell has had a formula applied to it. /// bool has_formula() const; // printing /// /// Returns a string representing the value of this cell. If the data type is not a string, /// it will be converted according to the number format. /// std::string to_string() const; // merging /// /// Returns true iff this cell has been merged with one or more /// surrounding cells. /// bool is_merged() const; /// /// Makes this a merged cell iff merged is true. /// Generally, this shouldn't be called directly. Instead, /// use worksheet::merge_cells on its parent worksheet. /// void merged(bool merged); // phonetics /// /// Returns true if this cell is set to show phonetic information. /// bool phonetics_visible() const; /// /// Enables the display of phonetic information on this cell. /// void show_phonetics(bool phonetics); /// /// Returns the error string that is stored in this cell. /// std::string error() const; /// /// Directly assigns the value of this cell to be the given error. /// void error(const std::string &error); /// /// Returns a cell from this cell's parent workbook at /// a relative offset given by the parameters. /// cell offset(int column, int row); /// /// Returns the worksheet that owns this cell. /// class worksheet worksheet(); /// /// Returns the worksheet that owns this cell. /// const class worksheet worksheet() const; /// /// Returns the workbook of the worksheet that owns this cell. /// class workbook &workbook(); /// /// Returns the workbook of the worksheet that owns this cell. /// const class workbook &workbook() const; /// /// Returns the base date of the parent workbook. /// calendar base_date() const; /// /// Returns to_check after verifying and fixing encoding, size, and illegal characters. /// std::string check_string(const std::string &to_check); // comment /// /// Returns true if this cell has a comment applied. /// bool has_comment(); /// /// Deletes the comment applied to this cell if it exists. /// void clear_comment(); /// /// Gets the comment applied to this cell. /// class comment comment(); /// /// Creates a new comment with the given text and optional author and /// applies it to the cell. /// void comment(const std::string &text, const std::string &author = "Microsoft Office User"); /// /// Creates a new comment with the given text, formatting, and optional /// author and applies it to the cell. /// void comment(const std::string &comment_text, const class font &comment_font, const std::string &author = "Microsoft Office User"); /// /// Apply the comment provided as the only argument to the cell. /// void comment(const class comment &new_comment); /// /// Returns the width of this cell in pixels. /// double width() const; /// /// Returns the height of this cell in pixels. /// double height() const; // operators /// /// Makes this cell interally point to rhs. /// The cell data originally pointed to by this cell will be unchanged. /// cell &operator=(const cell &rhs); /// /// Returns true if this cell the same cell as comparand (compared by reference). /// bool operator==(const cell &comparand) const; /// /// Returns false if this cell the same cell as comparand (compared by reference). /// bool operator!=(const cell &comparand) const; private: friend class style; friend class worksheet; friend class detail::xlsx_consumer; friend class detail::xlsx_producer; friend struct detail::cell_impl; /// /// Returns a non-const reference to the format of this cell. /// This is for internal use only. /// class format modifiable_format(); /// /// Delete the default zero-argument constructor. /// cell() = delete; /// /// Private constructor to create a cell from its implementation. /// cell(detail::cell_impl *d); /// /// A pointer to this cell's implementation. /// detail::cell_impl *d_; }; /// /// Returns true if this cell is uninitialized. /// XLNT_API bool operator==(std::nullptr_t, const cell &cell); /// /// Returns true if this cell is uninitialized. /// XLNT_API bool operator==(const cell &cell, std::nullptr_t); /// /// Convenience function for writing cell to an ostream. /// Uses cell::to_string() internally. /// XLNT_API std::ostream &operator<<(std::ostream &stream, const xlnt::cell &cell); template <> bool cell::value() const; template <> int cell::value() const; template <> unsigned int cell::value() const; template <> long long int cell::value() const; template <> unsigned long long cell::value() const; template <> float cell::value() const; template <> double cell::value() const; template <> date cell::value() const; template <> time cell::value