xlnt/include/xlnt/cell/cell.hpp

418 lines
12 KiB
C++
Raw Normal View History

// Copyright (c) 2015 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/cell/types.hpp>
2014-05-22 05:48:51 +08:00
#include "xlnt_config.hpp"
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;
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;
class number_format;
class protection;
2014-06-11 05:12:15 +08:00
class relationship;
2014-05-21 22:20:30 +08:00
class worksheet;
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
2015-11-04 07:26:33 +08:00
namespace detail { struct cell_impl; }
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:
2015-10-30 01:46:56 +08:00
/// <summary>
/// Enumerates the possible types a cell can be determined by it's current value.
/// </summary>
enum class type
{
2015-11-04 07:26:33 +08:00
/// no value. note: this is different from an empty string value or 0 numeric value
null,
2015-11-04 07:26:33 +08:00
/// number
numeric,
2015-11-04 07:26:33 +08:00
/// string
string,
2015-11-04 07:26:33 +08:00
/// value is a formula
formula,
2015-11-04 07:26:33 +08:00
/// value is a known error code such as \#VALUE!
error,
2015-11-04 07:26:33 +08:00
/// value is TRUE or FALSE
boolean
};
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();
// TODO: Should it be possible to construct and use a cell without a parent worksheet?
2015-10-30 01:46:56 +08:00
//(cont'd) If so, it would need to be responsible for allocating and deleting its PIMPL.
2015-10-30 01:46:56 +08:00
/// <summary>
/// Construct a null cell without a parent.
/// Most methods will throw an exception if this cell is not further initialized.
/// </summary>
2014-05-21 22:20:30 +08:00
cell();
2015-10-30 01:46:56 +08:00
/// <summary>
/// Construct a cell in worksheet, sheet, at the given reference location (e.g. A1).
/// </summary>
cell(worksheet sheet, const cell_reference &reference);
2015-10-30 01:46:56 +08:00
/// <summary>
/// This constructor, provided for convenience, is equivalent to calling:
/// cell c(sheet, reference);
/// c.set_value(initial_value);
/// </summary>
template <typename T>
2015-10-30 01:46:56 +08:00
cell(worksheet sheet, const cell_reference &reference, const T &initial_value);
// 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.
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;
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
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;
2015-11-04 07:26:33 +08:00
/// <summary>
/// Return the column of this cell.
/// </summary>
column_t get_column() const;
2015-11-04 07:26:33 +08:00
/// <summary>
/// Return the row of this cell.
/// </summary>
row_t get_row() const;
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
2015-11-04 07:26:33 +08:00
/// <summary>
/// Return a relationship representing this cell's hyperlink.
/// </summary>
2014-06-11 05:12:15 +08:00
relationship get_hyperlink() const;
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);
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;
// style
2015-11-04 07:26:33 +08:00
/// <summary>
/// Return true if this cell has had a style applied to it.
/// </summary>
2015-10-24 02:42:36 +08:00
bool has_style() const;
2015-11-04 07:26:33 +08:00
/// <summary>
/// Return the index of this cell's style in its parent workbook.
/// This is also the index of the style in the stylesheet XML, xl/styles.xml.
/// </summary>
2015-10-19 03:30:46 +08:00
std::size_t get_style_id() const;
2015-11-04 07:26:33 +08:00
/// <summary>
/// Set the style index of this cell. This should be an existing style in
/// the parent workbook.
/// </summary>
2015-10-24 02:42:36 +08:00
void set_style_id(std::size_t style_id);
2015-11-04 07:26:33 +08:00
/// <summary>
/// Return the number format of this cell.
/// </summary>
2015-10-19 03:30:46 +08:00
const number_format &get_number_format() const;
void set_number_format(const number_format &format);
2015-11-04 07:26:33 +08:00
/// <summary>
/// Return the font applied to the text in this cell.
/// </summary>
const font &get_font() const;
2015-11-04 07:26:33 +08:00
2015-10-19 03:30:46 +08:00
void set_font(const font &font_);
2015-11-04 07:26:33 +08:00
/// <summary>
/// Return the fill applied to this cell.
/// </summary>
const fill &get_fill() const;
2015-11-04 07:26:33 +08:00
2015-10-19 03:30:46 +08:00
void set_fill(const fill &fill_);
2015-11-04 07:26:33 +08:00
/// <summary>
/// Return the border of this cell.
/// </summary>
const border &get_border() const;
2015-11-04 07:26:33 +08:00
2015-10-19 03:30:46 +08:00
void set_border(const border &border_);
2015-11-04 07:26:33 +08:00
/// <summary>
/// Return the alignment of the text in this cell.
/// </summary>
const alignment &get_alignment() const;
2015-11-04 07:26:33 +08:00
2015-10-19 03:30:46 +08:00
void set_alignment(const alignment &alignment_);
2015-11-04 07:26:33 +08:00
/// <summary>
/// Return the protection of this cell.
/// </summary>
const protection &get_protection() const;
2015-11-04 07:26:33 +08:00
2015-10-19 03:30:46 +08:00
void set_protection(const protection &protection_);
2015-11-04 07:26:33 +08:00
2015-10-19 03:30:46 +08:00
void set_pivot_button(bool b);
2015-11-04 07:26:33 +08:00
/// <summary>
/// Return true iff pivot button?
/// </summary>
bool pivot_button() const;
2015-11-04 07:26:33 +08:00
2015-10-19 03:30:46 +08:00
void set_quote_prefix(bool b);
2015-11-04 07:26:33 +08:00
/// <summary>
/// Return true iff quote prefix?
/// </summary>
bool quote_prefix() const;
// comment
2015-11-04 07:26:33 +08:00
/// <summary>
/// Return the comment of this cell.
/// </summary>
comment get_comment();
2015-11-04 07:26:33 +08:00
/// <summary>
/// Return the comment of this cell.
/// </summary>
const comment get_comment() const;
void set_comment(const comment &comment);
2014-07-20 02:43:48 +08:00
void clear_comment();
2014-07-25 05:31:46 +08:00
bool has_comment() const;
// formula
std::string get_formula() const;
void set_formula(const std::string &formula);
2014-07-25 05:31:46 +08:00
void clear_formula();
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
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;
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;
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);
2015-11-04 07:26:33 +08:00
/// <summary>
/// Return the worksheet that owns this cell.
/// </summary>
2014-07-20 02:43:48 +08:00
worksheet get_parent();
2015-11-04 07:26:33 +08:00
/// <summary>
/// Return the worksheet that owns this cell.
/// </summary>
2014-07-26 04:39:25 +08:00
const worksheet get_parent() const;
2015-11-04 07:26:33 +08:00
/// <summary>
/// Shortcut to return the base date of the parent workbook.
/// Equivalent to get_parent().get_parent().get_properties().excel_base_date
/// </summary>
2015-10-19 03:30:46 +08:00
calendar get_base_date() const;
// operators
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;
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
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);
2015-11-04 07:26:33 +08:00
/// <summary>
/// Return the result of left.get_reference() < right.get_reference().
/// What's the point of this?
/// </summary>
2015-11-11 09:47:07 +08:00
friend XLNT_FUNCTION bool operator<(cell left, cell right);
/// <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)
{
return stream << cell.to_string();
}
2015-11-04 07:26:33 +08:00
private:
// make these friends so they can use the private constructor
friend class worksheet;
2015-10-19 03:30:46 +08:00
friend struct detail::cell_impl;
friend class style;
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);
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