xlnt/include/xlnt/cell/cell.hpp

501 lines
14 KiB
C++
Raw Normal View History

2015-12-25 06:10:02 +08:00
// Copyright (c) 2014-2016 Thomas Fussell
// Copyright (c) 2010-2015 openpyxl
2014-06-06 04:19:31 +08:00
//
// 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
2014-05-21 22:20:30 +08:00
#pragma once
#include <memory>
#include <string>
2014-05-21 22:20:30 +08:00
#include <unordered_map>
#include <xlnt/xlnt_config.hpp> // for XLNT_CLASS, XLNT_FUNCTION
#include <xlnt/cell/cell_type.hpp> // for cell_type
#include <xlnt/cell/index_types.hpp> // for column_t, row_t
2014-05-22 05:48:51 +08:00
2014-05-21 22:20:30 +08:00
namespace xlnt {
2015-10-19 03:30:46 +08:00
enum class calendar;
2015-10-30 01:46:56 +08:00
class alignment;
2016-08-18 19:34:18 +08:00
class base_format;
2015-10-30 01:46:56 +08:00
class border;
2014-05-21 22:20:30 +08:00
class cell_reference;
2014-07-26 04:39:25 +08:00
class comment;
2015-10-30 01:46:56 +08:00
class fill;
class font;
2016-05-15 01:57:07 +08:00
class format;
2015-10-30 01:46:56 +08:00
class number_format;
class protection;
2016-06-11 01:40:50 +08:00
class style;
2016-05-12 07:24:53 +08:00
class workbook;
2014-05-21 22:20:30 +08:00
class worksheet;
2016-08-05 13:52:05 +08:00
class xlsx_consumer;
class xlsx_producer;
2014-05-31 06:42:25 +08:00
2014-06-07 23:49:19 +08:00
struct date;
struct datetime;
struct time;
2014-07-20 04:59:05 +08:00
struct timedelta;
2014-06-07 23:49:19 +08:00
2016-08-05 13:52:05 +08:00
namespace detail {
class xlsx_consumer;
class xlsx_producer;
struct cell_impl;
} // namespace detail
2014-07-20 02:43:48 +08:00
2014-05-21 22:20:30 +08:00
/// <summary>
/// Describes cell associated properties.
/// </summary>
/// <remarks>
/// 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.
/// </remarks>
class XLNT_CLASS cell
2014-05-21 22:20:30 +08:00
{
2015-11-04 07:26:33 +08:00
public:
using type = cell_type;
2016-01-18 14:23:31 +08:00
2015-10-30 01:46:56 +08:00
/// <summary>
2015-11-04 07:26:33 +08:00
/// Return a map of error strings such as \#DIV/0! and their associated indices.
2015-10-30 01:46:56 +08:00
/// </summary>
static const std::unordered_map<std::string, int> &error_codes();
// value
2015-10-30 01:46:56 +08:00
/// <summary>
/// Return true if value has been set and has not been cleared using cell::clear_value().
/// </summary>
bool has_value() const;
2015-11-04 07:26:33 +08:00
/// <summary>
/// Return 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.
2015-11-04 07:26:33 +08:00
/// </summary>
template <typename T>
T get_value() const;
2015-11-04 07:26:33 +08:00
/// <summary>
/// Make this cell have a value of type null.
/// All other cell attributes are retained.
/// </summary>
void clear_value();
2015-11-04 07:26:33 +08:00
/// <summary>
/// Set the value of this cell to the given value.
/// 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.
2016-08-18 19:34:18 +08:00
/// </summary>
template <typename T>
void set_value(T value);
2015-11-04 07:26:33 +08:00
/// <summary>
/// Return the type of this cell.
/// </summary>
type get_data_type() const;
2016-01-18 14:23:31 +08:00
2015-11-04 07:26:33 +08:00
/// <summary>
/// Set the type of this cell.
/// </summary>
void set_data_type(type t);
2015-10-30 01:46:56 +08:00
// properties
2015-10-30 01:46:56 +08:00
/// <summary>
/// There's no reason to keep a cell which has no value and is not a placeholder.
/// Return true if this cell has no value, style, isn't merged, etc.
/// </summary>
bool garbage_collectible() const;
2015-10-30 01:46:56 +08:00
/// <summary>
/// Return true iff this cell's number format matches a date format.
/// </summary>
bool is_date() const;
// position
2016-01-18 14:23:31 +08:00
2015-11-04 07:26:33 +08:00
/// <summary>
/// Return a cell_reference that points to the location of this cell.
/// </summary>
cell_reference get_reference() const;
2016-01-18 14:23:31 +08:00
2015-11-04 07:26:33 +08:00
/// <summary>
/// Return the column of this cell.
/// </summary>
column_t get_column() const;
2016-01-18 14:23:31 +08:00
2015-11-04 07:26:33 +08:00
/// <summary>
/// Return the row of this cell.
/// </summary>
row_t get_row() const;
2016-01-18 14:23:31 +08:00
2015-11-04 07:26:33 +08:00
/// <summary>
/// Return the location of this cell as an ordered pair.
/// </summary>
std::pair<int, int> get_anchor() const;
// hyperlink
2016-01-18 14:23:31 +08:00
2015-11-04 07:26:33 +08:00
/// <summary>
2016-08-05 13:52:05 +08:00
/// Return the URL of this cell's hyperlink.
2015-11-04 07:26:33 +08:00
/// </summary>
2016-08-05 13:52:05 +08:00
std::string get_hyperlink() const;
2016-01-18 14:23:31 +08:00
2015-11-04 07:26:33 +08:00
/// <summary>
/// Add a hyperlink to this cell pointing to the URI of the given value.
/// </summary>
void set_hyperlink(const std::string &value);
2016-01-18 14:23:31 +08:00
2015-11-04 07:26:33 +08:00
/// <summary>
/// Return true if this cell has a hyperlink set.
/// </summary>
2014-06-11 05:12:15 +08:00
bool has_hyperlink() const;
// computed format
2016-01-18 14:23:31 +08:00
/// <summary>
/// For each of alignment, border, fill, font, number format, and protection,
/// returns a format using the value from the cell format if that value is
/// applied, or else the value from the named style if that value is applied,
/// or else the default value. This is used to retreive the formatting of the cell
/// as it will be displayed in an editing application.
/// </summary>
2016-08-18 19:34:18 +08:00
base_format get_computed_format() const;
2016-01-18 14:23:31 +08:00
/// <summary>
/// Returns the result of get_computed_format().get_alignment().
/// </summary>
alignment get_computed_alignment() const;
2016-01-18 14:23:31 +08:00
/// <summary>
/// Returns the result of get_computed_format().get_border().
/// </summary>
border get_computed_border() const;
2016-08-05 13:52:05 +08:00
/// <summary>
/// Returns the result of get_computed_format().get_fill().
/// </summary>
fill get_computed_fill() const;
/// <summary>
/// Returns the result of get_computed_format().get_font().
/// </summary>
font get_computed_font() const;
/// <summary>
/// Returns the result of get_computed_format().get_number_format().
/// </summary>
number_format get_computed_number_format() const;
/// <summary>
/// Returns the result of get_computed_format().get_protection().
/// </summary>
protection get_computed_protection() const;
// format
2016-06-11 01:40:50 +08:00
/// <summary>
/// Return true if this cell has had a format applied to it.
/// </summary>
bool has_format() const;
2016-06-11 01:40:50 +08:00
/// <summary>
/// Return a reference to the format applied to this cell.
/// If this cell has no format, an invalid_attribute exception will be thrown.
2016-06-11 01:40:50 +08:00
/// </summary>
format get_format() const;
2016-06-11 01:40:50 +08:00
/// <summary>
/// Applies the cell-level formatting of new_format to this cell.
/// </summary>
void set_format(const format &new_format);
2016-06-11 01:40:50 +08:00
/// <summary>
/// Remove 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.
/// </summary>
void clear_format();
2016-01-18 14:23:31 +08:00
2015-11-04 07:26:33 +08:00
/// <summary>
/// Returns the number format of this cell.
2015-11-04 07:26:33 +08:00
/// </summary>
2016-08-18 19:34:18 +08:00
number_format get_number_format() const;
/// <summary>
/// Creates a new format in the workbook, sets its number_format
/// to the given format, and applies the format to this cell.
/// </summary>
2015-10-19 03:30:46 +08:00
void set_number_format(const number_format &format);
2016-01-18 14:23:31 +08:00
2015-11-04 07:26:33 +08:00
/// <summary>
/// Returns the font applied to the text in this cell.
2015-11-04 07:26:33 +08:00
/// </summary>
2016-08-18 19:34:18 +08:00
font get_font() const;
2016-01-18 14:23:31 +08:00
/// <summary>
/// Creates a new format in the workbook, sets its font
/// to the given font, and applies the format to this cell.
/// </summary>
2015-10-19 03:30:46 +08:00
void set_font(const font &font_);
2016-01-18 14:23:31 +08:00
2015-11-04 07:26:33 +08:00
/// <summary>
/// Returns the fill applied to this cell.
2015-11-04 07:26:33 +08:00
/// </summary>
2016-08-18 19:34:18 +08:00
fill get_fill() const;
2016-01-18 14:23:31 +08:00
/// <summary>
/// Creates a new format in the workbook, sets its fill
/// to the given fill, and applies the format to this cell.
/// </summary>
2015-10-19 03:30:46 +08:00
void set_fill(const fill &fill_);
2016-01-18 14:23:31 +08:00
2015-11-04 07:26:33 +08:00
/// <summary>
/// Returns the border of this cell.
2015-11-04 07:26:33 +08:00
/// </summary>
2016-08-18 19:34:18 +08:00
border get_border() const;
2016-01-18 14:23:31 +08:00
/// <summary>
/// Creates a new format in the workbook, sets its border
/// to the given border, and applies the format to this cell.
/// </summary>
2015-10-19 03:30:46 +08:00
void set_border(const border &border_);
2016-01-18 14:23:31 +08:00
2015-11-04 07:26:33 +08:00
/// <summary>
/// Returns the alignment of the text in this cell.
2015-11-04 07:26:33 +08:00
/// </summary>
2016-08-18 19:34:18 +08:00
alignment get_alignment() const;
2016-01-18 14:23:31 +08:00
/// <summary>
/// Creates a new format in the workbook, sets its alignment
/// to the given alignment, and applies the format to this cell.
/// </summary>
2015-10-19 03:30:46 +08:00
void set_alignment(const alignment &alignment_);
2016-01-18 14:23:31 +08:00
2015-11-04 07:26:33 +08:00
/// <summary>
/// Returns the protection of this cell.
2015-11-04 07:26:33 +08:00
/// </summary>
2016-08-18 19:34:18 +08:00
protection get_protection() const;
2016-01-18 14:23:31 +08:00
/// <summary>
/// Creates a new format in the workbook, sets its protection
/// to the given protection, and applies the format to this cell.
/// </summary>
2015-10-19 03:30:46 +08:00
void set_protection(const protection &protection_);
2016-01-18 14:23:31 +08:00
// style
2016-01-18 14:23:31 +08:00
/// <summary>
/// Returns true if this cell has had a style applied to it.
/// </summary>
bool has_style() const;
2016-01-18 14:23:31 +08:00
/// <summary>
/// Returns a reference to the named style applied to this cell.
/// </summary>
style get_style() const;
2016-01-18 14:23:31 +08:00
/// <summary>
/// Equivalent to set_style(new_style.name())
/// </summary>
void set_style(const style &new_style);
/// <summary>
/// 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.
/// </summary>
void set_style(const std::string &style_name);
/// <summary>
/// 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.
/// </summary>
void clear_style();
// formula
/// <summary>
/// Returns the string representation of the formula applied to this cell.
/// </summary>
std::string get_formula() const;
/// <summary>
/// Sets the formula of this cell to the given value.
/// This formula string should begin with '='.
/// </summary>
void set_formula(const std::string &formula);
/// <summary>
/// Removes the formula from this cell. After this is called, has_formula() will return false.
/// </summary>
2014-07-25 05:31:46 +08:00
void clear_formula();
/// <summary>
/// Returns true if this cell has had a formula applied to it.
/// </summary>
2014-07-25 05:31:46 +08:00
bool has_formula() const;
2014-05-31 06:42:25 +08:00
// printing
2015-10-17 06:35:11 +08:00
/// <summary>
/// Returns a string describing this cell like <Cell Sheet.A1>.
/// </summary>
std::string to_repr() const;
2015-10-17 06:35:11 +08:00
/// <summary>
/// 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.
/// </summary>
std::string to_string() const;
// merging
2016-01-18 14:23:31 +08:00
2015-11-04 07:26:33 +08:00
/// <summary>
/// Return true iff this cell has been merged with one or more
/// surrounding cells.
/// </summary>
bool is_merged() const;
2016-01-18 14:23:31 +08:00
2015-11-04 07:26:33 +08:00
/// <summary>
/// Make this a merged cell iff merged is true.
/// Generally, this shouldn't be called directly. Instead,
/// use worksheet::merge_cells on its parent worksheet.
/// </summary>
void set_merged(bool merged);
2015-11-04 07:26:33 +08:00
/// <summary>
/// Return the error string that is stored in this cell.
/// </summary>
std::string get_error() const;
2016-01-18 14:23:31 +08:00
2015-11-04 07:26:33 +08:00
/// <summary>
/// Directly assign the value of this cell to be the given error.
/// </summary>
void set_error(const std::string &error);
2015-11-04 07:26:33 +08:00
/// <summary>
/// Return a cell from this cell's parent workbook at
/// a relative offset given by the parameters.
/// </summary>
cell offset(int column, int row);
2016-01-18 14:23:31 +08:00
2015-11-04 07:26:33 +08:00
/// <summary>
/// Return the worksheet that owns this cell.
/// </summary>
2016-05-12 07:24:53 +08:00
worksheet get_worksheet();
2016-01-18 14:23:31 +08:00
2015-11-04 07:26:33 +08:00
/// <summary>
/// Return the worksheet that owns this cell.
/// </summary>
2016-05-12 07:24:53 +08:00
const worksheet get_worksheet() const;
/// <summary>
/// Return the workbook of the worksheet that owns this cell.
/// </summary>
workbook &get_workbook();
/// <summary>
/// Return the workbook of the worksheet that owns this cell.
/// </summary>
const workbook &get_workbook() const;
2015-11-04 07:26:33 +08:00
/// <summary>
/// Shortcut to return the base date of the parent workbook.
2016-05-12 07:24:53 +08:00
/// Equivalent to get_workbook().get_properties().excel_base_date
2015-11-04 07:26:33 +08:00
/// </summary>
2015-10-19 03:30:46 +08:00
calendar get_base_date() const;
2015-11-23 01:41:27 +08:00
/// <summary>
/// Return to_check after checking encoding, size, and illegal characters.
/// </summary>
std::string check_string(const std::string &to_check);
// operators
2016-01-18 14:23:31 +08:00
2015-11-04 07:26:33 +08:00
/// <summary>
/// Make this cell point to rhs.
/// The cell originally pointed to by this cell will be unchanged.
/// </summary>
2014-05-21 22:20:30 +08:00
cell &operator=(const cell &rhs);
2015-11-04 07:26:33 +08:00
/// <summary>
/// Return true if this cell the same cell as comparand (compare by reference).
/// </summary>
2014-06-14 03:05:24 +08:00
bool operator==(const cell &comparand) const;
2016-01-18 14:23:31 +08:00
2015-11-04 07:26:33 +08:00
/// <summary>
/// Return true if this cell is uninitialized.
/// </summary>
2014-05-21 22:20:30 +08:00
bool operator==(std::nullptr_t) const;
2014-05-31 06:42:25 +08:00
2015-10-30 01:46:56 +08:00
// friend operators, so we can put cell on either side of comparisons with other types
2016-01-18 14:23:31 +08:00
2015-11-04 07:26:33 +08:00
/// <summary>
/// Return true if this cell is uninitialized.
/// </summary>
2015-11-11 09:47:07 +08:00
friend XLNT_FUNCTION bool operator==(std::nullptr_t, const cell &cell);
2016-01-18 14:23:31 +08:00
/// <summary>
/// Convenience function for writing cell to an ostream.
/// Uses cell::to_string() internally.
/// </summary>
friend XLNT_FUNCTION std::ostream &operator<<(std::ostream &stream, const xlnt::cell &cell);
2015-11-04 07:26:33 +08:00
private:
// make these friends so they can use the private constructor
friend class style;
friend class worksheet;
2016-08-05 13:52:05 +08:00
friend class detail::xlsx_consumer;
friend class detail::xlsx_producer;
2015-10-19 03:30:46 +08:00
friend struct detail::cell_impl;
/// <summary>
/// Helper function to guess the type of a string, convert it,
/// and then use the correct cell::get_value according to the type.
/// </summary>
2016-05-12 07:24:53 +08:00
void guess_type_and_set_value(const std::string &value);
/// <summary>
/// Returns a non-const reference to the format of this cell.
/// This is for internal use only.
/// </summary>
2016-08-18 19:34:18 +08:00
format &get_format_internal();
2015-11-04 07:26:33 +08:00
/// <summary>
/// Private constructor to create a cell from its implementation.
/// </summary>
2014-05-31 06:42:25 +08:00
cell(detail::cell_impl *d);
2016-01-18 14:23:31 +08:00
2015-11-04 07:26:33 +08:00
/// <summary>
/// A pointer to this cell's implementation.
/// </summary>
2014-05-31 06:42:25 +08:00
detail::cell_impl *d_;
2014-05-21 22:20:30 +08:00
};
} // namespace xlnt