xlnt/include/xlnt/cell/cell.hpp

410 lines
11 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;
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.
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;
// style
2016-01-18 14:23:31 +08:00
2015-11-04 07:26:33 +08:00
/// <summary>
2016-05-15 01:57:07 +08:00
/// Return true if this cell has had a format applied to it.
2015-11-04 07:26:33 +08:00
/// </summary>
2016-05-15 01:57:07 +08:00
bool has_format() const;
2016-01-18 14:23:31 +08:00
2015-11-04 07:26:33 +08:00
/// <summary>
2016-05-15 01:57:07 +08:00
/// Return a reference to the format applied to this cell.
2015-11-04 07:26:33 +08:00
/// </summary>
2016-05-15 01:57:07 +08:00
format &get_format();
2016-01-18 14:23:31 +08:00
2015-11-04 07:26:33 +08:00
/// <summary>
2016-05-15 01:57:07 +08:00
/// Return a reference to the format applied to this cell.
2015-11-04 07:26:33 +08:00
/// </summary>
2016-05-15 01:57:07 +08:00
const format &get_format() const;
2016-05-01 23:08:56 +08:00
2016-05-15 01:57:07 +08:00
void set_format(const format &new_format);
2016-08-05 13:52:05 +08:00
void set_format_id(std::size_t format_id);
2016-06-11 01:40:50 +08:00
void clear_format();
// style
/// <summary>
/// Return true if this cell has had a format applied to it.
/// </summary>
bool has_style() const;
/// <summary>
/// Return a reference to the format applied to this cell.
/// </summary>
const style &get_style() const;
void set_style(const style &new_style);
void set_style(const std::string &style_name);
void clear_style();
2016-01-18 14:23:31 +08:00
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;
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>
/// Return the font applied to the text in this cell.
/// </summary>
const font &get_font() const;
2016-01-18 14:23:31 +08:00
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>
/// Return the fill applied to this cell.
/// </summary>
const fill &get_fill() const;
2016-01-18 14:23:31 +08:00
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>
/// Return the border of this cell.
/// </summary>
const border &get_border() const;
2016-01-18 14:23:31 +08:00
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>
/// Return the alignment of the text in this cell.
/// </summary>
const alignment &get_alignment() const;
2016-01-18 14:23:31 +08:00
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>
/// Return the protection of this cell.
/// </summary>
const protection &get_protection() const;
2016-01-18 14:23:31 +08:00
2015-10-19 03:30:46 +08:00
void set_protection(const protection &protection_);
2016-01-18 14:23:31 +08:00
// comment
2016-01-18 14:23:31 +08:00
2015-11-04 07:26:33 +08:00
/// <summary>
/// Return the comment of this cell.
/// </summary>
comment get_comment();
2016-01-18 14:23:31 +08:00
2015-11-04 07:26:33 +08:00
/// <summary>
/// Return the comment of this cell.
/// </summary>
const comment get_comment() const;
2016-01-18 14:23:31 +08:00
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
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:
2016-05-15 01:57:07 +08:00
std::size_t get_format_id() const;
2016-05-01 23:08:56 +08:00
2015-11-04 07:26:33 +08:00
// 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;
2016-05-12 07:24:53 +08:00
void guess_type_and_set_value(const std::string &value);
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