clarify naming of styles and formats

This commit is contained in:
Thomas Fussell 2016-05-14 13:57:07 -04:00
parent 916883e427
commit 74cc503215
19 changed files with 368 additions and 374 deletions

View File

@ -42,6 +42,7 @@ class cell_style;
class comment; class comment;
class fill; class fill;
class font; class font;
class format;
class number_format; class number_format;
class protection; class protection;
class relationship; class relationship;
@ -53,9 +54,7 @@ struct datetime;
struct time; struct time;
struct timedelta; struct timedelta;
namespace detail { namespace detail { struct cell_impl; }
struct cell_impl;
}
/// <summary> /// <summary>
/// Describes cell associated properties. /// Describes cell associated properties.
@ -191,21 +190,21 @@ public:
// style // style
/// <summary> /// <summary>
/// Return true if this cell has had a style applied to it. /// Return true if this cell has had a format applied to it.
/// </summary> /// </summary>
bool has_style() const; bool has_format() const;
/// <summary> /// <summary>
/// Return a reference to the style applied to this cell. /// Return a reference to the format applied to this cell.
/// </summary> /// </summary>
cell_style &get_style(); format &get_format();
/// <summary> /// <summary>
/// Return a reference to the style applied to this cell. /// Return a reference to the format applied to this cell.
/// </summary> /// </summary>
const cell_style &get_style() const; const format &get_format() const;
void set_style(const cell_style &style); void set_format(const format &new_format);
/// <summary> /// <summary>
/// Return the number format of this cell. /// Return the number format of this cell.
@ -384,7 +383,7 @@ public:
friend XLNT_FUNCTION std::ostream &operator<<(std::ostream &stream, const xlnt::cell &cell); friend XLNT_FUNCTION std::ostream &operator<<(std::ostream &stream, const xlnt::cell &cell);
private: private:
std::size_t get_style_id() const; std::size_t get_format_id() const;
// make these friends so they can use the private constructor // make these friends so they can use the private constructor
friend class style; friend class style;

View File

@ -28,24 +28,24 @@
#include <vector> #include <vector>
#include <xlnt/xlnt_config.hpp> #include <xlnt/xlnt_config.hpp>
#include <xlnt/styles/named_style.hpp> #include <xlnt/styles/style.hpp>
#include <xlnt/workbook/workbook.hpp> #include <xlnt/workbook/workbook.hpp>
namespace xlnt { namespace xlnt {
class alignment; class alignment;
class border; class border;
class cell_style;
class color; class color;
class common_style;
class conditional_format; class conditional_format;
class fill; class fill;
class font; class font;
class format; class format;
class named_style; class formattable;
class style;
class number_format; class number_format;
class protection; class protection;
class side; class side;
class style;
class xml_document; class xml_document;
class xml_node; class xml_node;
@ -140,15 +140,15 @@ public:
/// these is "applied". For example, a style with a defined font id, font=# but with /// these is "applied". For example, a style with a defined font id, font=# but with
/// "applyFont=0" will not use the font in formatting. /// "applyFont=0" will not use the font in formatting.
/// </summary> /// </summary>
cell_style read_cell_style(const xml_node &style_node); format read_format(const xml_node &format_node);
/// <summary> /// <summary>
/// Read a single named style from the given named_style_node. In styles.xml, this is a /// Read a single named style from the given named_style_node. In styles.xml, this is a
/// (confusingly named) "cellStyle" element. This node defines the name, whether it is /// (confusingly named) "cellStyle" element. This node defines the name, whether it is
/// built-in and an xfId which is the index of an element in cellStyleXfs which should /// built-in and an xfId which is the index of an element in cellStyleXfs. style_format_node
/// be provided as the style_node parameter (cellStyleXfs itself, not the child xf node). /// should be the XML node of the element at the index of xfId.
/// </summary> /// </summary>
named_style read_named_style(const xml_node &named_style_node, const xml_node &style_parent_node); style read_style(const xml_node &style_node, const xml_node &style_format_node);
// //
// Non-static element readers (i.e. readers that modify internal state) // Non-static element readers (i.e. readers that modify internal state)
@ -184,13 +184,13 @@ public:
/// Read all cell styles from cell_styles_node and add them to workbook. /// Read all cell styles from cell_styles_node and add them to workbook.
/// Return true on success. /// Return true on success.
/// </summary> /// </summary>
bool read_cell_styles(const xml_node &cell_styles_node); bool read_formats(const xml_node &formats_node);
/// <summary> /// <summary>
/// Read all named styles from named_style_node and cell_styles_node and add them to workbook. /// Read all named styles from named_style_node and cell_styles_node and add them to workbook.
/// Return true on success. /// Return true on success.
/// </summary> /// </summary>
bool read_named_styles(const xml_node &cell_styles_node, const xml_node &cell_style_formats_node); bool read_styles(const xml_node &styles_node, const xml_node &style_formats_node);
/// <summary> /// <summary>
/// Read all borders from number_formats_node and add them to workbook. /// Read all borders from number_formats_node and add them to workbook.
@ -232,27 +232,15 @@ public:
/// </summary> /// </summary>
bool write_number_formats(xml_node &number_formats_node) const; bool write_number_formats(xml_node &number_formats_node) const;
bool write_style_common(const common_style &style, xml_node xf_node) const; bool write_formattable(const formattable &xf, xml_node xf_node) const;
/// <summary>
/// Build an xml tree representing the given style into style_node.
/// Returns true on success.
/// </summary>
bool write_cell_style(const cell_style &style, xml_node &style_node) const;
/// <summary> /// <summary>
/// Build an xml tree representing the given style into style_node. /// Build an xml tree representing the given style into style_node.
/// Returns true on success. /// Returns true on success.
/// </summary> /// </summary>
bool write_cell_styles(xml_node &styles_node) const; bool write_formats(xml_node &styles_node) const;
/// <summary> bool write_styles(xml_node &cell_styles_node, xml_node &cell_style_formats_node) const;
/// Build an xml tree representing the given style into cell_styles_node and styles_node.
/// Returns true on success.
/// </summary>
bool write_named_style(const named_style &style, xml_node &cell_styles_node) const;
bool write_named_styles(xml_node &cell_styles_node, xml_node &styles_node) const;
bool write_colors(xml_node &colors_node) const; bool write_colors(xml_node &colors_node) const;
@ -281,8 +269,8 @@ private:
std::vector<fill> fills_; std::vector<fill> fills_;
std::vector<font> fonts_; std::vector<font> fonts_;
std::vector<number_format> number_formats_; std::vector<number_format> number_formats_;
std::vector<cell_style> cell_styles_; std::vector<format> formats_;
std::unordered_map<std::size_t, named_style> named_styles_; std::vector<style> styles_;
}; };
} // namespace xlnt } // namespace xlnt

View File

@ -23,28 +23,31 @@
// @author: see AUTHORS file // @author: see AUTHORS file
#pragma once #pragma once
#include <xlnt/styles/common_style.hpp> #include <xlnt/styles/formattable.hpp>
namespace xlnt { namespace xlnt {
class named_style; class style;
namespace detail { struct workbook_impl; }
/// <summary> /// <summary>
/// Describes the formatting of a particular cell. /// Describes the formatting of a particular cell.
/// </summary> /// </summary>
class XLNT_CLASS cell_style : public common_style class XLNT_CLASS format : public formattable
{ {
public: public:
cell_style(); format();
cell_style(const cell_style &other); format(const format &other);
cell_style &operator=(const cell_style &other); format &operator=(const format &other);
// Named Style // Named Style
named_style &get_named_style(); bool has_style() const;
const named_style &get_named_style() const; std::string get_style_name() const;
void set_named_style(const std::string &style_name); style &get_style();
void set_named_style(const named_style &new_named_style); const style &get_style() const;
void remove_named_style(); void set_style(const std::string &name);
void set_style(const style &new_style);
void remove_style();
protected: protected:
std::string to_hash_string() const override; std::string to_hash_string() const override;

View File

@ -34,17 +34,16 @@
namespace xlnt { namespace xlnt {
class cell; class cell;
namespace detail { struct workbook_impl; }
/// <summary> /// <summary>
/// Describes the formatting of a particular cell. /// Describes the formatting of a particular cell.
/// </summary> /// </summary>
class XLNT_CLASS common_style : public hashable class XLNT_CLASS formattable : public hashable
{ {
public: public:
common_style(); formattable();
common_style(const common_style &other); formattable(const formattable &other);
common_style &operator=(const common_style &other); formattable &operator=(const formattable &other);
void reset(); void reset();

View File

@ -23,7 +23,10 @@
// @author: see AUTHORS file // @author: see AUTHORS file
#pragma once #pragma once
#include <xlnt/styles/common_style.hpp> #include <cstdint>
#include <string>
#include <xlnt/styles/formattable.hpp>
namespace xlnt { namespace xlnt {
@ -31,14 +34,14 @@ class workbook;
/// <summary> /// <summary>
/// Describes a style which has a name and can be applied to multiple individual /// Describes a style which has a name and can be applied to multiple individual
/// cell_styles. /// formats. In Excel this is a "Cell Style".
/// </summary> /// </summary>
class XLNT_CLASS named_style : public common_style class XLNT_CLASS style : public formattable
{ {
public: public:
named_style(); style();
named_style(const named_style &other); style(const style &other);
named_style &operator=(const named_style &other); style &operator=(const style &other);
std::string get_name() const; std::string get_name() const;
void set_name(const std::string &name); void set_name(const std::string &name);

View File

@ -53,7 +53,7 @@ class protection;
class range; class range;
class range_reference; class range_reference;
class relationship; class relationship;
class named_style; class style;
class theme; class theme;
class worksheet; class worksheet;
class worksheet_iterator; class worksheet_iterator;
@ -183,19 +183,19 @@ public:
bool has_loaded_theme() const; bool has_loaded_theme() const;
const theme &get_loaded_theme() const; const theme &get_loaded_theme() const;
cell_style &get_style(std::size_t style_index); format &get_format(std::size_t format_index);
const cell_style &get_style(std::size_t style_index) const; const format &get_format(std::size_t format_index) const;
std::size_t add_style(const cell_style &style); std::size_t add_format(const format &new_format);
void clear_styles(); void clear_formats();
std::vector<cell_style> &get_styles(); std::vector<format> &get_formats();
const std::vector<cell_style> &get_styles() const; const std::vector<format> &get_formats() const;
// Named Styles // Named Styles
bool has_named_style(const std::string &name); bool has_style(const std::string &name);
named_style &get_named_style(const std::string &name); style &get_style(const std::string &name);
const named_style &get_named_style(const std::string &name) const; const style &get_style(const std::string &name) const;
named_style &create_named_style(const std::string &name); style &create_style(const std::string &name);
const std::vector<named_style> &get_named_styles() const; const std::vector<style> &get_styles() const;
manifest &get_manifest(); manifest &get_manifest();
const manifest &get_manifest() const; const manifest &get_manifest() const;

View File

@ -34,10 +34,10 @@
#include <xlnt/serialization/encoding.hpp> #include <xlnt/serialization/encoding.hpp>
#include <xlnt/styles/alignment.hpp> #include <xlnt/styles/alignment.hpp>
#include <xlnt/styles/border.hpp> #include <xlnt/styles/border.hpp>
#include <xlnt/styles/cell_style.hpp>
#include <xlnt/styles/color.hpp> #include <xlnt/styles/color.hpp>
#include <xlnt/styles/fill.hpp> #include <xlnt/styles/fill.hpp>
#include <xlnt/styles/font.hpp> #include <xlnt/styles/font.hpp>
#include <xlnt/styles/format.hpp>
#include <xlnt/styles/number_format.hpp> #include <xlnt/styles/number_format.hpp>
#include <xlnt/styles/protection.hpp> #include <xlnt/styles/protection.hpp>
#include <xlnt/styles/side.hpp> #include <xlnt/styles/side.hpp>

View File

@ -31,7 +31,7 @@
#include <xlnt/packaging/document_properties.hpp> #include <xlnt/packaging/document_properties.hpp>
#include <xlnt/packaging/relationship.hpp> #include <xlnt/packaging/relationship.hpp>
#include <xlnt/serialization/encoding.hpp> #include <xlnt/serialization/encoding.hpp>
#include <xlnt/styles/cell_style.hpp> #include <xlnt/styles/format.hpp>
#include <xlnt/styles/color.hpp> #include <xlnt/styles/color.hpp>
#include <xlnt/utils/date.hpp> #include <xlnt/utils/date.hpp>
#include <xlnt/utils/datetime.hpp> #include <xlnt/utils/datetime.hpp>
@ -223,7 +223,7 @@ cell::cell(worksheet worksheet, const cell_reference &reference, const T &initia
bool cell::garbage_collectible() const bool cell::garbage_collectible() const
{ {
return !(get_data_type() != type::null || is_merged() || has_comment() || has_formula() || has_style()); return !(get_data_type() != type::null || is_merged() || has_comment() || has_formula() || has_format());
} }
template <> template <>
@ -389,7 +389,7 @@ XLNT_FUNCTION void cell::set_value(cell c)
d_->hyperlink_ = c.d_->hyperlink_; d_->hyperlink_ = c.d_->hyperlink_;
d_->has_hyperlink_ = c.d_->has_hyperlink_; d_->has_hyperlink_ = c.d_->has_hyperlink_;
d_->formula_ = c.d_->formula_; d_->formula_ = c.d_->formula_;
d_->style_id_ = c.d_->style_id_; d_->format_id_ = c.d_->format_id_;
set_comment(c.get_comment()); set_comment(c.get_comment());
} }
@ -691,73 +691,73 @@ void cell::set_data_type(type t)
const number_format &cell::get_number_format() const const number_format &cell::get_number_format() const
{ {
if (d_->has_style_) if (d_->has_format_)
{ {
return get_workbook().get_style(d_->style_id_).get_number_format(); return get_workbook().get_format(d_->format_id_).get_number_format();
} }
else else
{ {
return get_workbook().get_style(0).get_number_format(); return get_workbook().get_format(0).get_number_format();
} }
} }
const font &cell::get_font() const const font &cell::get_font() const
{ {
if (d_->has_style_) if (d_->has_format_)
{ {
return get_workbook().get_style(d_->style_id_).get_font(); return get_workbook().get_format(d_->format_id_).get_font();
} }
else else
{ {
return get_workbook().get_style(0).get_font(); return get_workbook().get_format(0).get_font();
} }
} }
const fill &cell::get_fill() const const fill &cell::get_fill() const
{ {
if (d_->has_style_) if (d_->has_format_)
{ {
return get_workbook().get_style(d_->style_id_).get_fill(); return get_workbook().get_format(d_->format_id_).get_fill();
} }
else else
{ {
return get_workbook().get_style(0).get_fill(); return get_workbook().get_format(0).get_fill();
} }
} }
const border &cell::get_border() const const border &cell::get_border() const
{ {
if (d_->has_style_) if (d_->has_format_)
{ {
return get_workbook().get_style(d_->style_id_).get_border(); return get_workbook().get_format(d_->format_id_).get_border();
} }
else else
{ {
return get_workbook().get_style(0).get_border(); return get_workbook().get_format(0).get_border();
} }
} }
const alignment &cell::get_alignment() const const alignment &cell::get_alignment() const
{ {
if (d_->has_style_) if (d_->has_format_)
{ {
return get_workbook().get_style(d_->style_id_).get_alignment(); return get_workbook().get_format(d_->format_id_).get_alignment();
} }
else else
{ {
return get_workbook().get_style(0).get_alignment(); return get_workbook().get_format(0).get_alignment();
} }
} }
const protection &cell::get_protection() const const protection &cell::get_protection() const
{ {
if (d_->has_style_) if (d_->has_format_)
{ {
return get_workbook().get_style(d_->style_id_).get_protection(); return get_workbook().get_format(d_->format_id_).get_protection();
} }
else else
{ {
return get_workbook().get_style(0).get_protection(); return get_workbook().get_format(0).get_protection();
} }
} }
@ -875,50 +875,50 @@ XLNT_FUNCTION timedelta cell::get_value() const
void cell::set_border(const xlnt::border &border_) void cell::set_border(const xlnt::border &border_)
{ {
d_->has_style_ = true; d_->has_format_ = true;
auto style_copy = get_workbook().get_style(d_->style_id_); auto format_copy = get_workbook().get_format(d_->format_id_);
style_copy.set_border(border_); format_copy.set_border(border_);
d_->style_id_ = get_workbook().add_style(style_copy); d_->format_id_ = get_workbook().add_format(format_copy);
} }
void cell::set_fill(const xlnt::fill &fill_) void cell::set_fill(const xlnt::fill &fill_)
{ {
d_->has_style_ = true; d_->has_format_ = true;
auto style_copy = get_workbook().get_style(d_->style_id_); auto format_copy = get_workbook().get_format(d_->format_id_);
style_copy.set_fill(fill_); format_copy.set_fill(fill_);
d_->style_id_ = get_workbook().add_style(style_copy); d_->format_id_ = get_workbook().add_format(format_copy);
} }
void cell::set_font(const font &font_) void cell::set_font(const font &font_)
{ {
d_->has_style_ = true; d_->has_format_ = true;
auto style_copy = get_workbook().get_style(d_->style_id_); auto format_copy = get_workbook().get_format(d_->format_id_);
style_copy.set_font(font_); format_copy.set_font(font_);
d_->style_id_ = get_workbook().add_style(style_copy); d_->format_id_ = get_workbook().add_format(format_copy);
} }
void cell::set_number_format(const number_format &number_format_) void cell::set_number_format(const number_format &number_format_)
{ {
d_->has_style_ = true; d_->has_format_ = true;
auto style_copy = get_workbook().get_style(d_->style_id_); auto format_copy = get_workbook().get_format(d_->format_id_);
style_copy.set_number_format(number_format_); format_copy.set_number_format(number_format_);
d_->style_id_ = get_workbook().add_style(style_copy); d_->format_id_ = get_workbook().add_format(format_copy);
} }
void cell::set_alignment(const xlnt::alignment &alignment_) void cell::set_alignment(const xlnt::alignment &alignment_)
{ {
d_->has_style_ = true; d_->has_format_ = true;
auto style_copy = get_workbook().get_style(d_->style_id_); auto format_copy = get_workbook().get_format(d_->format_id_);
style_copy.set_alignment(alignment_); format_copy.set_alignment(alignment_);
d_->style_id_ = get_workbook().add_style(style_copy); d_->format_id_ = get_workbook().add_format(format_copy);
} }
void cell::set_protection(const xlnt::protection &protection_) void cell::set_protection(const xlnt::protection &protection_)
{ {
d_->has_style_ = true; d_->has_format_ = true;
auto style_copy = get_workbook().get_style(d_->style_id_); auto format_copy = get_workbook().get_format(d_->format_id_);
style_copy.set_protection(protection_); format_copy.set_protection(protection_);
d_->style_id_ = get_workbook().add_style(style_copy); d_->format_id_ = get_workbook().add_format(format_copy);
} }
template <> template <>
@ -953,20 +953,20 @@ std::string cell::to_string() const
} }
} }
cell_style &cell::get_style() format &cell::get_format()
{ {
return get_workbook().get_style(d_->style_id_); return get_workbook().get_format(d_->format_id_);
} }
bool cell::has_style() const bool cell::has_format() const
{ {
return d_->has_style_; return d_->has_format_;
} }
void cell::set_style(const cell_style &style) void cell::set_format(const format &new_format)
{ {
d_->style_id_ = get_workbook().add_style(style); d_->format_id_ = get_workbook().add_format(new_format);
d_->has_style_ = true; d_->has_format_ = true;
} }
calendar cell::get_base_date() const calendar cell::get_base_date() const
@ -979,9 +979,9 @@ std::ostream &operator<<(std::ostream &stream, const xlnt::cell &cell)
return stream << cell.to_string(); return stream << cell.to_string();
} }
std::size_t cell::get_style_id() const std::size_t cell::get_format_id() const
{ {
return d_->style_id_; return d_->format_id_;
} }
void cell::guess_type_and_set_value(const std::string &value) void cell::guess_type_and_set_value(const std::string &value)

View File

@ -10,9 +10,9 @@
#include <xlnt/serialization/encoding.hpp> #include <xlnt/serialization/encoding.hpp>
#include <xlnt/styles/alignment.hpp> #include <xlnt/styles/alignment.hpp>
#include <xlnt/styles/border.hpp> #include <xlnt/styles/border.hpp>
#include <xlnt/styles/cell_style.hpp>
#include <xlnt/styles/font.hpp> #include <xlnt/styles/font.hpp>
#include <xlnt/styles/fill.hpp> #include <xlnt/styles/fill.hpp>
#include <xlnt/styles/format.hpp>
#include <xlnt/styles/number_format.hpp> #include <xlnt/styles/number_format.hpp>
#include <xlnt/styles/protection.hpp> #include <xlnt/styles/protection.hpp>
#include <xlnt/utils/date.hpp> #include <xlnt/utils/date.hpp>
@ -367,8 +367,8 @@ public:
cell.set_font(font); cell.set_font(font);
TS_ASSERT(cell.has_style()); TS_ASSERT(cell.has_format());
TS_ASSERT(cell.get_style().font_applied()); TS_ASSERT(cell.get_format().font_applied());
TS_ASSERT_EQUALS(cell.get_font(), font); TS_ASSERT_EQUALS(cell.get_font(), font);
} }
@ -383,8 +383,8 @@ public:
cell.set_fill(f); cell.set_fill(f);
TS_ASSERT(cell.has_style()); TS_ASSERT(cell.has_format());
TS_ASSERT(cell.get_style().fill_applied()); TS_ASSERT(cell.get_format().fill_applied());
TS_ASSERT_EQUALS(cell.get_fill(), f); TS_ASSERT_EQUALS(cell.get_fill(), f);
} }
@ -397,8 +397,8 @@ public:
cell.set_border(border); cell.set_border(border);
TS_ASSERT(cell.has_style()); TS_ASSERT(cell.has_format());
TS_ASSERT(cell.get_style().border_applied()); TS_ASSERT(cell.get_format().border_applied());
TS_ASSERT_EQUALS(cell.get_border(), border); TS_ASSERT_EQUALS(cell.get_border(), border);
} }
@ -410,8 +410,8 @@ public:
xlnt::number_format format("dd--hh--mm"); xlnt::number_format format("dd--hh--mm");
cell.set_number_format(format); cell.set_number_format(format);
TS_ASSERT(cell.has_style()); TS_ASSERT(cell.has_format());
TS_ASSERT(cell.get_style().number_format_applied()); TS_ASSERT(cell.get_format().number_format_applied());
TS_ASSERT_EQUALS(cell.get_number_format().get_format_string(), "dd--hh--mm"); TS_ASSERT_EQUALS(cell.get_number_format().get_format_string(), "dd--hh--mm");
} }
@ -425,8 +425,8 @@ public:
cell.set_alignment(align); cell.set_alignment(align);
TS_ASSERT(cell.has_style()); TS_ASSERT(cell.has_format());
TS_ASSERT(cell.get_style().alignment_applied()); TS_ASSERT(cell.get_format().alignment_applied());
TS_ASSERT_EQUALS(cell.get_alignment(), align); TS_ASSERT_EQUALS(cell.get_alignment(), align);
} }
@ -440,8 +440,8 @@ public:
cell.set_protection(prot); cell.set_protection(prot);
TS_ASSERT(cell.has_style()); TS_ASSERT(cell.has_format());
TS_ASSERT(cell.get_style().protection_applied()); TS_ASSERT(cell.get_format().protection_applied());
TS_ASSERT_EQUALS(cell.get_protection(), prot); TS_ASSERT_EQUALS(cell.get_protection(), prot);
} }
}; };

View File

@ -45,8 +45,8 @@ cell_impl::cell_impl(worksheet_impl *parent, column_t column, row_t row)
value_numeric_(0), value_numeric_(0),
has_hyperlink_(false), has_hyperlink_(false),
is_merged_(false), is_merged_(false),
has_style_(false), has_format_(false),
style_id_(0), format_id_(0),
comment_(nullptr) comment_(nullptr)
{ {
} }
@ -68,8 +68,8 @@ cell_impl &cell_impl::operator=(const cell_impl &rhs)
is_merged_ = rhs.is_merged_; is_merged_ = rhs.is_merged_;
has_hyperlink_ = rhs.has_hyperlink_; has_hyperlink_ = rhs.has_hyperlink_;
type_ = rhs.type_; type_ = rhs.type_;
style_id_ = rhs.style_id_; format_id_ = rhs.format_id_;
has_style_ = rhs.has_style_; has_format_ = rhs.has_format_;
if (rhs.comment_ != nullptr) if (rhs.comment_ != nullptr)
{ {

View File

@ -70,13 +70,10 @@ struct cell_impl
bool is_merged_; bool is_merged_;
bool has_style_; bool has_format_;
std::size_t style_id_; std::size_t format_id_;
std::unique_ptr<comment_impl> comment_; std::unique_ptr<comment_impl> comment_;
bool pivot_button_;
bool quote_prefix_;
}; };
} // namespace detail } // namespace detail

View File

@ -43,8 +43,8 @@ struct workbook_impl
guess_types_(other.guess_types_), guess_types_(other.guess_types_),
data_only_(other.data_only_), data_only_(other.data_only_),
read_only_(other.read_only_), read_only_(other.read_only_),
cell_styles_(other.cell_styles_), formats_(other.formats_),
named_styles_(other.named_styles_), styles_(other.styles_),
manifest_(other.manifest_) manifest_(other.manifest_)
{ {
} }
@ -84,8 +84,8 @@ struct workbook_impl
bool data_only_; bool data_only_;
bool read_only_; bool read_only_;
std::vector<cell_style> cell_styles_; std::vector<format> formats_;
std::vector<named_style> named_styles_; std::vector<style> styles_;
std::size_t next_custom_format_id_; std::size_t next_custom_format_id_;
manifest manifest_; manifest manifest_;

View File

@ -37,8 +37,8 @@
#include <xlnt/serialization/xml_serializer.hpp> #include <xlnt/serialization/xml_serializer.hpp>
#include <xlnt/packaging/document_properties.hpp> #include <xlnt/packaging/document_properties.hpp>
#include <xlnt/packaging/manifest.hpp> #include <xlnt/packaging/manifest.hpp>
#include <xlnt/styles/cell_style.hpp> #include <xlnt/styles/format.hpp>
#include <xlnt/styles/named_style.hpp> #include <xlnt/styles/style.hpp>
#include <xlnt/utils/exceptions.hpp> #include <xlnt/utils/exceptions.hpp>
#include <xlnt/workbook/workbook.hpp> #include <xlnt/workbook/workbook.hpp>
#include <xlnt/workbook/worksheet_iterator.hpp> #include <xlnt/workbook/worksheet_iterator.hpp>

View File

@ -30,11 +30,10 @@
#include <xlnt/serialization/xml_node.hpp> #include <xlnt/serialization/xml_node.hpp>
#include <xlnt/styles/alignment.hpp> #include <xlnt/styles/alignment.hpp>
#include <xlnt/styles/border.hpp> #include <xlnt/styles/border.hpp>
#include <xlnt/styles/cell_style.hpp>
#include <xlnt/styles/common_style.hpp>
#include <xlnt/styles/fill.hpp> #include <xlnt/styles/fill.hpp>
#include <xlnt/styles/font.hpp> #include <xlnt/styles/font.hpp>
#include <xlnt/styles/named_style.hpp> #include <xlnt/styles/format.hpp>
#include <xlnt/styles/style.hpp>
#include <xlnt/styles/number_format.hpp> #include <xlnt/styles/number_format.hpp>
#include <xlnt/styles/protection.hpp> #include <xlnt/styles/protection.hpp>
#include <xlnt/workbook/worksheet_iterator.hpp> #include <xlnt/workbook/worksheet_iterator.hpp>
@ -197,7 +196,7 @@ style_serializer::style_serializer(workbook &wb) : workbook_(wb)
{ {
for (auto cell : row) for (auto cell : row)
{ {
if (cell.has_style()) if (cell.has_format())
{ {
number_format_set.insert(cell.get_number_format()); number_format_set.insert(cell.get_number_format());
} }
@ -295,36 +294,36 @@ alignment style_serializer::read_alignment(const xml_node &alignment_node)
return align; return align;
} }
cell_style style_serializer::read_cell_style(const xml_node &style_node) format style_serializer::read_format(const xml_node &format_node)
{ {
cell_style s; format f;
// Alignment // Alignment
s.alignment_applied(style_node.has_child("alignment") || is_true(style_node.get_attribute("applyAlignment"))); f.alignment_applied(format_node.has_child("alignment") || is_true(format_node.get_attribute("applyAlignment")));
if (s.alignment_applied()) if (f.alignment_applied())
{ {
auto inline_alignment = read_alignment(style_node.get_child("alignment")); auto inline_alignment = read_alignment(format_node.get_child("alignment"));
s.set_alignment(inline_alignment); f.set_alignment(inline_alignment);
} }
// Border // Border
auto border_index = style_node.has_attribute("borderId") ? std::stoull(style_node.get_attribute("borderId")) : 0; auto border_index = format_node.has_attribute("borderId") ? std::stoull(format_node.get_attribute("borderId")) : 0;
s.set_border(borders_.at(border_index)); f.set_border(borders_.at(border_index));
s.border_applied(is_true(style_node.get_attribute("applyBorder"))); f.border_applied(is_true(format_node.get_attribute("applyBorder")));
// Fill // Fill
auto fill_index = style_node.has_attribute("fillId") ? std::stoull(style_node.get_attribute("fillId")) : 0; auto fill_index = format_node.has_attribute("fillId") ? std::stoull(format_node.get_attribute("fillId")) : 0;
s.set_fill(fills_.at(fill_index)); f.set_fill(fills_.at(fill_index));
s.fill_applied(is_true(style_node.get_attribute("applyFill"))); f.fill_applied(is_true(format_node.get_attribute("applyFill")));
// Font // Font
auto font_index = style_node.has_attribute("fontId") ? std::stoull(style_node.get_attribute("fontId")) : 0; auto font_index = format_node.has_attribute("fontId") ? std::stoull(format_node.get_attribute("fontId")) : 0;
s.set_font(fonts_.at(font_index)); f.set_font(fonts_.at(font_index));
s.font_applied(is_true(style_node.get_attribute("applyFont"))); f.font_applied(is_true(format_node.get_attribute("applyFont")));
// Number Format // Number Format
auto number_format_id = std::stoull(style_node.get_attribute("numFmtId")); auto number_format_id = std::stoull(format_node.get_attribute("numFmtId"));
bool builtin_format = true; bool builtin_format = true;
@ -332,7 +331,7 @@ cell_style style_serializer::read_cell_style(const xml_node &style_node)
{ {
if (static_cast<std::size_t>(num_fmt.get_id()) == number_format_id) if (static_cast<std::size_t>(num_fmt.get_id()) == number_format_id)
{ {
s.set_number_format(num_fmt); f.set_number_format(num_fmt);
builtin_format = false; builtin_format = false;
break; break;
} }
@ -340,32 +339,32 @@ cell_style style_serializer::read_cell_style(const xml_node &style_node)
if (builtin_format) if (builtin_format)
{ {
s.set_number_format(number_format::from_builtin_id(number_format_id)); f.set_number_format(number_format::from_builtin_id(number_format_id));
} }
s.number_format_applied(is_true(style_node.get_attribute("applyNumberFormat"))); f.number_format_applied(is_true(format_node.get_attribute("applyNumberFormat")));
// Protection // Protection
s.protection_applied(style_node.has_attribute("protection") || is_true(style_node.get_attribute("applyProtection"))); f.protection_applied(format_node.has_attribute("protection") || is_true(format_node.get_attribute("applyProtection")));
if (s.protection_applied()) if (f.protection_applied())
{ {
auto inline_protection = read_protection(style_node.get_child("protection")); auto inline_protection = read_protection(format_node.get_child("protection"));
s.set_protection(inline_protection); f.set_protection(inline_protection);
} }
return s; return f;
} }
named_style style_serializer::read_named_style(const xml_node &cell_style_node, const xml_node &cell_style_format_node) style style_serializer::read_style(const xml_node &style_node, const xml_node &style_format_node)
{ {
named_style s; style s;
s.set_name(cell_style_node.get_attribute("name")); s.set_name(style_node.get_attribute("name"));
s.set_hidden(cell_style_node.has_attribute("hidden") && is_true(cell_style_node.get_attribute("hidden"))); s.set_hidden(style_node.has_attribute("hidden") && is_true(style_node.get_attribute("hidden")));
s.set_builtin_id(std::stoull(cell_style_node.get_attribute("builtinId"))); s.set_builtin_id(std::stoull(style_node.get_attribute("builtinId")));
auto base_style = read_cell_style(cell_style_format_node); auto base_style = read_format(style_format_node);
//TODO shouldn't have to set apply after set_X() //TODO shouldn't have to set apply after set_X()
s.set_alignment(base_style.get_alignment()); s.set_alignment(base_style.get_alignment());
@ -393,81 +392,86 @@ bool style_serializer::read_stylesheet(const xml_document &xml)
read_fonts(stylesheet_node.get_child("fonts")); read_fonts(stylesheet_node.get_child("fonts"));
read_number_formats(stylesheet_node.get_child("numFmts")); read_number_formats(stylesheet_node.get_child("numFmts"));
read_colors(stylesheet_node.get_child("colors")); read_colors(stylesheet_node.get_child("colors"));
read_named_styles(stylesheet_node.get_child("cellStyles"), stylesheet_node.get_child("cellStyleXfs")); read_styles(stylesheet_node.get_child("cellStyles"), stylesheet_node.get_child("cellStyleXfs"));
read_cell_styles(stylesheet_node.get_child("cellXfs")); read_formats(stylesheet_node.get_child("cellXfs"));
for (const auto &ns : named_styles_) for (const auto &ns : styles_)
{ {
workbook_.create_named_style(ns.second.get_name()) = ns.second; workbook_.create_style(ns.get_name()) = ns;
} }
if (!cell_styles_.empty()) if (!formats_.empty())
{ {
workbook_.clear_styles(); workbook_.clear_formats();
} }
for (const auto &s : cell_styles_) for (const auto &s : formats_)
{ {
workbook_.add_style(s); workbook_.add_format(s);
} }
return true; return true;
} }
bool style_serializer::read_cell_styles(const xlnt::xml_node &cell_styles_node) bool style_serializer::read_formats(const xlnt::xml_node &formats_node)
{ {
for (auto style_node : cell_styles_node.get_children()) for (auto format_node : formats_node.get_children())
{ {
if (style_node.get_name() != "xf") if (format_node.get_name() != "xf")
{ {
continue; continue;
} }
auto style = read_cell_style(style_node); auto cell_format = read_format(format_node);
if(style_node.has_attribute("xfId")) // TODO do all formats have xfId?
if(format_node.has_attribute("xfId"))
{ {
auto named_style_index = std::stoull(style_node.get_attribute("xfId")); auto style_index = std::stoull(format_node.get_attribute("xfId"));
auto named_style = named_styles_.at(named_style_index); auto cell_style = styles_.at(style_index);
style.set_named_style(named_style.get_name()); cell_format.set_style(cell_style.get_name());
} }
cell_styles_.push_back(style); formats_.push_back(cell_format);
} }
return true; return true;
} }
bool style_serializer::read_named_styles(const xlnt::xml_node &cell_styles_node, const xlnt::xml_node &cell_style_formats_node) bool style_serializer::read_styles(const xlnt::xml_node &styles_node, const xlnt::xml_node &style_formats_node)
{ {
std::size_t style_index = 0; std::size_t style_index = 0;
for (auto cell_style_format_node : cell_style_formats_node.get_children()) for (auto cell_style_format_node : style_formats_node.get_children())
{ {
for (auto cell_style_node : cell_styles_node.get_children()) bool match = false;
style ns;
for (auto cell_style_node : styles_node.get_children())
{ {
auto cell_style_format_index = std::stoull(cell_style_node.get_attribute("xfId")); auto cell_style_format_index = std::stoull(cell_style_node.get_attribute("xfId"));
if (cell_style_format_index == style_index) if (cell_style_format_index == style_index)
{ {
auto ns = read_named_style(cell_style_node, cell_style_format_node); ns = read_style(cell_style_node, cell_style_format_node);
named_styles_[style_index] = ns; match = true;
break; break;
} }
} }
styles_.push_back(ns);
style_index++; style_index++;
} }
if (named_styles_.empty()) if (styles_.empty())
{ {
named_style ns; style ns;
ns.set_name("Standard"); ns.set_name("Standard");
ns.set_hidden(false); ns.set_hidden(false);
ns.set_builtin_id(0); ns.set_builtin_id(0);
named_styles_[0] = ns; styles_.push_back(ns);
} }
return true; return true;
@ -762,7 +766,7 @@ void style_serializer::initialize_vectors()
{ {
for (auto cell : row) for (auto cell : row)
{ {
if (cell.has_style()) if (cell.has_format())
{ {
any_style = true; any_style = true;
} }
@ -772,7 +776,7 @@ void style_serializer::initialize_vectors()
if (any_style) if (any_style)
{ {
cell_styles_.assign(workbook_.get_styles().begin(), workbook_.get_styles().end()); formats_.assign(workbook_.get_formats().begin(), workbook_.get_formats().end());
} }
colors_.clear(); colors_.clear();
@ -781,26 +785,26 @@ void style_serializer::initialize_vectors()
fonts_.clear(); fonts_.clear();
number_formats_.clear(); number_formats_.clear();
for (auto &style : cell_styles_) for (auto &current_format : formats_)
{ {
if (std::find(borders_.begin(), borders_.end(), style.get_border()) == borders_.end()) if (std::find(borders_.begin(), borders_.end(), current_format.get_border()) == borders_.end())
{ {
borders_.push_back(style.get_border()); borders_.push_back(current_format.get_border());
} }
if (std::find(fills_.begin(), fills_.end(), style.get_fill()) == fills_.end()) if (std::find(fills_.begin(), fills_.end(), current_format.get_fill()) == fills_.end())
{ {
fills_.push_back(style.get_fill()); fills_.push_back(current_format.get_fill());
} }
if (std::find(fonts_.begin(), fonts_.end(), style.get_font()) == fonts_.end()) if (std::find(fonts_.begin(), fonts_.end(), current_format.get_font()) == fonts_.end())
{ {
fonts_.push_back(style.get_font()); fonts_.push_back(current_format.get_font());
} }
if (std::find(number_formats_.begin(), number_formats_.end(), style.get_number_format()) == number_formats_.end()) if (std::find(number_formats_.begin(), number_formats_.end(), current_format.get_number_format()) == number_formats_.end())
{ {
number_formats_.push_back(style.get_number_format()); number_formats_.push_back(current_format.get_number_format());
} }
} }
@ -1104,39 +1108,39 @@ bool style_serializer::write_borders(xlnt::xml_node &borders_node) const
return true; return true;
} }
bool style_serializer::write_style_common(const common_style &style, xml_node xf_node) const bool style_serializer::write_formattable(const formattable &xf, xml_node xf_node) const
{ {
xf_node.add_attribute("numFmtId", std::to_string(style.get_number_format().get_id())); xf_node.add_attribute("numFmtId", std::to_string(xf.get_number_format().get_id()));
auto font_id = std::distance(fonts_.begin(), std::find(fonts_.begin(), fonts_.end(), style.get_font())); auto font_id = std::distance(fonts_.begin(), std::find(fonts_.begin(), fonts_.end(), xf.get_font()));
xf_node.add_attribute("fontId", std::to_string(font_id)); xf_node.add_attribute("fontId", std::to_string(font_id));
if (style.fill_applied()) if (xf.fill_applied())
{ {
auto fill_id = std::distance(fills_.begin(), std::find(fills_.begin(), fills_.end(), style.get_fill())); auto fill_id = std::distance(fills_.begin(), std::find(fills_.begin(), fills_.end(), xf.get_fill()));
xf_node.add_attribute("fillId", std::to_string(fill_id)); xf_node.add_attribute("fillId", std::to_string(fill_id));
} }
if (style.border_applied()) if (xf.border_applied())
{ {
auto border_id = std::distance(borders_.begin(), std::find(borders_.begin(), borders_.end(), style.get_border())); auto border_id = std::distance(borders_.begin(), std::find(borders_.begin(), borders_.end(), xf.get_border()));
xf_node.add_attribute("borderId", std::to_string(border_id)); xf_node.add_attribute("borderId", std::to_string(border_id));
} }
xf_node.add_attribute("applyNumberFormat", style.number_format_applied() ? "1" : "0"); xf_node.add_attribute("applyNumberFormat", xf.number_format_applied() ? "1" : "0");
xf_node.add_attribute("applyFill", style.fill_applied() ? "1" : "0"); xf_node.add_attribute("applyFill", xf.fill_applied() ? "1" : "0");
xf_node.add_attribute("applyFont", style.font_applied() ? "1" : "0"); xf_node.add_attribute("applyFont", xf.font_applied() ? "1" : "0");
xf_node.add_attribute("applyBorder", style.border_applied() ? "1" : "0"); xf_node.add_attribute("applyBorder", xf.border_applied() ? "1" : "0");
xf_node.add_attribute("applyAlignment", style.alignment_applied() ? "1" : "0"); xf_node.add_attribute("applyAlignment", xf.alignment_applied() ? "1" : "0");
xf_node.add_attribute("applyProtection", style.protection_applied() ? "1" : "0"); xf_node.add_attribute("applyProtection", xf.protection_applied() ? "1" : "0");
if (style.alignment_applied()) if (xf.alignment_applied())
{ {
auto alignment_node = xf_node.add_child("alignment"); auto alignment_node = xf_node.add_child("alignment");
if (style.get_alignment().has_vertical()) if (xf.get_alignment().has_vertical())
{ {
switch (style.get_alignment().get_vertical()) switch (xf.get_alignment().get_vertical())
{ {
case vertical_alignment::bottom: case vertical_alignment::bottom:
alignment_node.add_attribute("vertical", "bottom"); alignment_node.add_attribute("vertical", "bottom");
@ -1155,9 +1159,9 @@ bool style_serializer::write_style_common(const common_style &style, xml_node xf
} }
} }
if (style.get_alignment().has_horizontal()) if (xf.get_alignment().has_horizontal())
{ {
switch (style.get_alignment().get_horizontal()) switch (xf.get_alignment().get_horizontal())
{ {
case horizontal_alignment::center: case horizontal_alignment::center:
alignment_node.add_attribute("horizontal", "center"); alignment_node.add_attribute("horizontal", "center");
@ -1182,12 +1186,12 @@ bool style_serializer::write_style_common(const common_style &style, xml_node xf
} }
} }
if (style.get_alignment().get_wrap_text()) if (xf.get_alignment().get_wrap_text())
{ {
alignment_node.add_attribute("wrapText", "1"); alignment_node.add_attribute("wrapText", "1");
} }
if (style.get_alignment().get_shrink_to_fit()) if (xf.get_alignment().get_shrink_to_fit())
{ {
alignment_node.add_attribute("shrinkToFit", "1"); alignment_node.add_attribute("shrinkToFit", "1");
} }
@ -1196,37 +1200,24 @@ bool style_serializer::write_style_common(const common_style &style, xml_node xf
return true; return true;
} }
bool style_serializer::write_cell_style(const cell_style &style, xml_node &xf_node) const bool style_serializer::write_styles(xml_node &styles_node, xml_node &style_formats_node) const
{ {
return write_style_common(style, xf_node); style_formats_node.add_attribute("count", std::to_string(styles_.size()));
} styles_node.add_attribute("count", std::to_string(styles_.size()));
std::size_t style_index = 0;
bool style_serializer::write_named_style(const named_style &style, xml_node &xf_node) const for(auto &current_style : styles_)
{
return write_style_common(style, xf_node);
}
bool style_serializer::write_named_styles(xml_node &cell_styles_node, xml_node &styles_node) const
{
styles_node.add_attribute("count", std::to_string(named_styles_.size()));
for(auto &key_style : named_styles_)
{ {
auto xf_node = styles_node.add_child("xf"); auto xf_node = style_formats_node.add_child("xf");
write_named_style(key_style.second, xf_node); write_formattable(current_style, xf_node);
}
cell_styles_node.add_attribute("count", std::to_string(named_styles_.size()));
for(auto &key_style : named_styles_) auto cell_style_node = styles_node.add_child("cellStyle");
{
auto cell_style_node = cell_styles_node.add_child("cellStyle");
cell_style_node.add_attribute("name", key_style.second.get_name()); cell_style_node.add_attribute("name", current_style.get_name());
cell_style_node.add_attribute("xfId", std::to_string(key_style.first)); cell_style_node.add_attribute("xfId", std::to_string(style_index++));
cell_style_node.add_attribute("builtinId", std::to_string(key_style.second.get_builtin_id())); cell_style_node.add_attribute("builtinId", std::to_string(current_style.get_builtin_id()));
if (key_style.second.get_hidden()) if (current_style.get_hidden())
{ {
cell_style_node.add_attribute("hidden", "1"); cell_style_node.add_attribute("hidden", "1");
} }
@ -1235,15 +1226,19 @@ bool style_serializer::write_named_styles(xml_node &cell_styles_node, xml_node &
return true; return true;
} }
bool style_serializer::write_cell_styles(xml_node &cell_styles_node) const bool style_serializer::write_formats(xml_node &formats_node) const
{ {
cell_styles_node.add_attribute("count", std::to_string(cell_styles_.size())); formats_node.add_attribute("count", std::to_string(formats_.size()));
for(auto &style : cell_styles_) for(auto &current_format : formats_)
{ {
auto xf_node = cell_styles_node.add_child("xf"); auto xf_node = formats_node.add_child("xf");
write_cell_style(style, xf_node); write_formattable(current_format, xf_node);
xf_node.add_attribute("xfId", "0"); //TODO point to named style here
if(current_format.has_style())
{
xf_node.add_attribute("xfId", "0");
}
} }
return true; return true;
@ -1312,10 +1307,10 @@ xml_document style_serializer::write_stylesheet()
auto cell_style_xfs_node = root_node.add_child("cellStyleXfs"); auto cell_style_xfs_node = root_node.add_child("cellStyleXfs");
auto cell_xfs_node = root_node.add_child("cellXfs"); auto cell_xfs_node = root_node.add_child("cellXfs");
write_cell_styles(cell_xfs_node); write_formats(cell_xfs_node);
auto cell_styles_node = root_node.add_child("cellStyles"); auto cell_styles_node = root_node.add_child("cellStyles");
write_named_styles(cell_style_xfs_node, cell_styles_node); write_styles(cell_styles_node, cell_style_xfs_node);
auto dxfs_node = root_node.add_child("dxfs"); auto dxfs_node = root_node.add_child("dxfs");
write_dxfs(dxfs_node); write_dxfs(dxfs_node);

View File

@ -149,8 +149,8 @@ bool worksheet_serializer::read_worksheet(const xml_document &xml)
bool has_type = cell_node.has_attribute("t"); bool has_type = cell_node.has_attribute("t");
std::string type = has_type ? cell_node.get_attribute("t") : ""; std::string type = has_type ? cell_node.get_attribute("t") : "";
bool has_style = cell_node.has_attribute("s"); bool has_format = cell_node.has_attribute("s");
auto style_id = static_cast<std::size_t>(has_style ? std::stoull(cell_node.get_attribute("s")) : 0LL); auto format_id = static_cast<std::size_t>(has_format ? std::stoull(cell_node.get_attribute("s")) : 0LL);
bool has_formula = cell_node.has_child("f"); bool has_formula = cell_node.has_child("f");
bool has_shared_formula = has_formula && cell_node.get_child("f").has_attribute("t") && bool has_shared_formula = has_formula && cell_node.get_child("f").has_attribute("t") &&
@ -195,9 +195,9 @@ bool worksheet_serializer::read_worksheet(const xml_document &xml)
} }
} }
if (has_style) if (has_format)
{ {
cell.set_style(sheet_.get_workbook().get_style(style_id)); cell.set_format(sheet_.get_workbook().get_format(format_id));
} }
} }
} }
@ -509,9 +509,9 @@ xml_document worksheet_serializer::write_worksheet() const
} }
} }
if (cell.has_style()) if (cell.has_format())
{ {
cell_node.add_attribute("s", std::to_string(cell.get_style_id())); cell_node.add_attribute("s", std::to_string(cell.get_format_id()));
} }
} }
} }

View File

@ -21,11 +21,11 @@
// //
// @license: http://www.opensource.org/licenses/mit-license.php // @license: http://www.opensource.org/licenses/mit-license.php
// @author: see AUTHORS file // @author: see AUTHORS file
#include <xlnt/styles/common_style.hpp> #include <xlnt/styles/formattable.hpp>
namespace xlnt { namespace xlnt {
common_style::common_style() formattable::formattable()
: apply_alignment_(false), : apply_alignment_(false),
apply_border_(false), apply_border_(false),
apply_fill_(false), apply_fill_(false),
@ -35,7 +35,7 @@ common_style::common_style()
{ {
} }
common_style::common_style(const common_style &other) formattable::formattable(const formattable &other)
: alignment_(other.alignment_), : alignment_(other.alignment_),
border_(other.border_), border_(other.border_),
fill_(other.fill_), fill_(other.fill_),
@ -51,7 +51,7 @@ common_style::common_style(const common_style &other)
{ {
} }
common_style &common_style::operator=(const xlnt::common_style &other) formattable &formattable::operator=(const xlnt::formattable &other)
{ {
alignment_ = other.alignment_; alignment_ = other.alignment_;
border_ = other.border_; border_ = other.border_;
@ -70,105 +70,105 @@ common_style &common_style::operator=(const xlnt::common_style &other)
return *this; return *this;
} }
alignment &common_style::get_alignment() alignment &formattable::get_alignment()
{ {
return alignment_; return alignment_;
} }
const alignment &common_style::get_alignment() const const alignment &formattable::get_alignment() const
{ {
return alignment_; return alignment_;
} }
void common_style::set_alignment(const xlnt::alignment &new_alignment) void formattable::set_alignment(const xlnt::alignment &new_alignment)
{ {
alignment_ = new_alignment; alignment_ = new_alignment;
alignment_applied(true); alignment_applied(true);
} }
number_format &common_style::get_number_format() number_format &formattable::get_number_format()
{ {
return number_format_; return number_format_;
} }
const number_format &common_style::get_number_format() const const number_format &formattable::get_number_format() const
{ {
return number_format_; return number_format_;
} }
void common_style::set_number_format(const xlnt::number_format &new_number_format) void formattable::set_number_format(const xlnt::number_format &new_number_format)
{ {
number_format_ = new_number_format; number_format_ = new_number_format;
number_format_applied(true); number_format_applied(true);
} }
border &common_style::get_border() border &formattable::get_border()
{ {
return border_; return border_;
} }
const border &common_style::get_border() const const border &formattable::get_border() const
{ {
return border_; return border_;
} }
void common_style::set_border(const xlnt::border &new_border) void formattable::set_border(const xlnt::border &new_border)
{ {
border_ = new_border; border_ = new_border;
border_applied(true); border_applied(true);
} }
fill &common_style::get_fill() fill &formattable::get_fill()
{ {
return fill_; return fill_;
} }
const fill &common_style::get_fill() const const fill &formattable::get_fill() const
{ {
return fill_; return fill_;
} }
void common_style::set_fill(const xlnt::fill &new_fill) void formattable::set_fill(const xlnt::fill &new_fill)
{ {
fill_ = new_fill; fill_ = new_fill;
fill_applied(true); fill_applied(true);
} }
font &common_style::get_font() font &formattable::get_font()
{ {
return font_; return font_;
} }
const font &common_style::get_font() const const font &formattable::get_font() const
{ {
return font_; return font_;
} }
void common_style::set_font(const xlnt::font &new_font) void formattable::set_font(const xlnt::font &new_font)
{ {
font_ = new_font; font_ = new_font;
font_applied(true); font_applied(true);
} }
protection &common_style::get_protection() protection &formattable::get_protection()
{ {
return protection_; return protection_;
} }
const protection &common_style::get_protection() const const protection &formattable::get_protection() const
{ {
return protection_; return protection_;
} }
void common_style::set_protection(const xlnt::protection &new_protection) void formattable::set_protection(const xlnt::protection &new_protection)
{ {
protection_ = new_protection; protection_ = new_protection;
protection_applied(true); protection_applied(true);
} }
std::string common_style::to_hash_string() const std::string formattable::to_hash_string() const
{ {
std::string hash_string("common_style:"); std::string hash_string("formattable:");
hash_string.append(std::to_string(alignment_applied())); hash_string.append(std::to_string(alignment_applied()));
hash_string.append(alignment_applied() ? std::to_string(alignment_.hash()) : ":"); hash_string.append(alignment_applied() ? std::to_string(alignment_.hash()) : ":");
@ -191,62 +191,62 @@ std::string common_style::to_hash_string() const
return hash_string; return hash_string;
} }
void common_style::alignment_applied(bool applied) void formattable::alignment_applied(bool applied)
{ {
apply_alignment_ = applied; apply_alignment_ = applied;
} }
void common_style::border_applied(bool applied) void formattable::border_applied(bool applied)
{ {
apply_border_ = applied; apply_border_ = applied;
} }
void common_style::fill_applied(bool applied) void formattable::fill_applied(bool applied)
{ {
apply_fill_ = applied; apply_fill_ = applied;
} }
void common_style::font_applied(bool applied) void formattable::font_applied(bool applied)
{ {
apply_font_ = applied; apply_font_ = applied;
} }
void common_style::number_format_applied(bool applied) void formattable::number_format_applied(bool applied)
{ {
apply_number_format_ = applied; apply_number_format_ = applied;
} }
void common_style::protection_applied(bool applied) void formattable::protection_applied(bool applied)
{ {
apply_protection_ = applied; apply_protection_ = applied;
} }
bool common_style::alignment_applied() const bool formattable::alignment_applied() const
{ {
return apply_alignment_; return apply_alignment_;
} }
bool common_style::border_applied() const bool formattable::border_applied() const
{ {
return apply_border_; return apply_border_;
} }
bool common_style::fill_applied() const bool formattable::fill_applied() const
{ {
return apply_fill_; return apply_fill_;
} }
bool common_style::font_applied() const bool formattable::font_applied() const
{ {
return apply_font_; return apply_font_;
} }
bool common_style::number_format_applied() const bool formattable::number_format_applied() const
{ {
return apply_number_format_; return apply_number_format_;
} }
bool common_style::protection_applied() const bool formattable::protection_applied() const
{ {
return apply_protection_; return apply_protection_;
} }

View File

@ -21,27 +21,27 @@
// //
// @license: http://www.opensource.org/licenses/mit-license.php // @license: http://www.opensource.org/licenses/mit-license.php
// @author: see AUTHORS file // @author: see AUTHORS file
#include <xlnt/cell/cell.hpp> #include <xlnt/cell/cell.hpp>
#include <xlnt/styles/cell_style.hpp>
#include <xlnt/styles/font.hpp> #include <xlnt/styles/font.hpp>
#include <xlnt/styles/format.hpp>
#include <xlnt/styles/number_format.hpp> #include <xlnt/styles/number_format.hpp>
#include <xlnt/styles/protection.hpp> #include <xlnt/styles/protection.hpp>
#include <xlnt/utils/hash_combine.hpp> #include <xlnt/utils/hash_combine.hpp>
namespace xlnt { namespace xlnt {
cell_style::cell_style() format::format()
{ {
} }
cell_style::cell_style(const cell_style &other) format::format(const format &other) : formattable(other)
: common_style(other)
{ {
} }
cell_style &cell_style::operator=(const cell_style &other) format &format::operator=(const format &other)
{ {
common_style::operator=(other); formattable::operator=(other);
parent_ = other.parent_; parent_ = other.parent_;
named_style_name_ = other.named_style_name_; named_style_name_ = other.named_style_name_;
@ -49,10 +49,10 @@ cell_style &cell_style::operator=(const cell_style &other)
return *this; return *this;
} }
std::string cell_style::to_hash_string() const std::string format::to_hash_string() const
{ {
auto hash_string = common_style::to_hash_string(); auto hash_string = formattable::to_hash_string();
hash_string.append(":cell_style:"); hash_string.append(":format:");
if (!named_style_name_.empty()) if (!named_style_name_.empty())
{ {
@ -66,9 +66,14 @@ std::string cell_style::to_hash_string() const
return hash_string; return hash_string;
} }
void cell_style::set_named_style(const std::string &style_name) void format::set_style(const std::string &style_name)
{ {
named_style_name_ = style_name; named_style_name_ = style_name;
} }
bool format::has_style() const
{
return !named_style_name_.empty();
}
} // namespace xlnt } // namespace xlnt

View File

@ -21,27 +21,29 @@
// //
// @license: http://www.opensource.org/licenses/mit-license.php // @license: http://www.opensource.org/licenses/mit-license.php
// @author: see AUTHORS file // @author: see AUTHORS file
#include <xlnt/styles/named_style.hpp>
#include <xlnt/styles/style.hpp>
namespace xlnt { namespace xlnt {
named_style::named_style() style::style()
: hidden_(false), : formattable(),
hidden_(false),
builtin_id_(0) builtin_id_(0)
{ {
} }
named_style::named_style(const named_style &other) style::style(const style &other)
: common_style(other), : formattable(other),
name_(other.name_), name_(other.name_),
hidden_(other.hidden_), hidden_(other.hidden_),
builtin_id_(other.builtin_id_) builtin_id_(other.builtin_id_)
{ {
} }
named_style &named_style::operator=(const named_style &other) style &style::operator=(const style &other)
{ {
common_style::operator=(other); formattable::operator=(other);
name_ = other.name_; name_ = other.name_;
hidden_ = other.hidden_; hidden_ = other.hidden_;
@ -50,39 +52,39 @@ named_style &named_style::operator=(const named_style &other)
return *this; return *this;
} }
bool named_style::get_hidden() const bool style::get_hidden() const
{ {
return hidden_; return hidden_;
} }
void named_style::set_hidden(bool value) void style::set_hidden(bool value)
{ {
hidden_ = value; hidden_ = value;
} }
std::size_t named_style::get_builtin_id() const std::size_t style::get_builtin_id() const
{ {
return builtin_id_; return builtin_id_;
} }
void named_style::set_builtin_id(std::size_t builtin_id) void style::set_builtin_id(std::size_t builtin_id)
{ {
builtin_id_ = builtin_id; builtin_id_ = builtin_id;
} }
std::string named_style::get_name() const std::string style::get_name() const
{ {
return name_; return name_;
} }
void named_style::set_name(const std::string &name) void style::set_name(const std::string &name)
{ {
name_ = name; name_ = name;
} }
std::string named_style::to_hash_string() const std::string style::to_hash_string() const
{ {
return common_style::to_hash_string() + name_; return formattable::to_hash_string() + name_;
} }
} // namespace xlnt } // namespace xlnt

View File

@ -36,10 +36,10 @@
#include <xlnt/serialization/excel_serializer.hpp> #include <xlnt/serialization/excel_serializer.hpp>
#include <xlnt/styles/alignment.hpp> #include <xlnt/styles/alignment.hpp>
#include <xlnt/styles/border.hpp> #include <xlnt/styles/border.hpp>
#include <xlnt/styles/cell_style.hpp> #include <xlnt/styles/format.hpp>
#include <xlnt/styles/fill.hpp> #include <xlnt/styles/fill.hpp>
#include <xlnt/styles/font.hpp> #include <xlnt/styles/font.hpp>
#include <xlnt/styles/named_style.hpp> #include <xlnt/styles/style.hpp>
#include <xlnt/styles/number_format.hpp> #include <xlnt/styles/number_format.hpp>
#include <xlnt/styles/protection.hpp> #include <xlnt/styles/protection.hpp>
#include <xlnt/utils/exceptions.hpp> #include <xlnt/utils/exceptions.hpp>
@ -86,7 +86,7 @@ workbook::workbook() : d_(new detail::workbook_impl())
d_->manifest_.add_override_type("/docProps/core.xml", "application/vnd.openxmlformats-package.core-properties+xml"); d_->manifest_.add_override_type("/docProps/core.xml", "application/vnd.openxmlformats-package.core-properties+xml");
d_->manifest_.add_override_type("/docProps/app.xml", "application/vnd.openxmlformats-officedocument.extended-properties+xml"); d_->manifest_.add_override_type("/docProps/app.xml", "application/vnd.openxmlformats-officedocument.extended-properties+xml");
add_style(cell_style()); add_format(format());
} }
workbook::workbook(encoding e) : workbook() workbook::workbook(encoding e) : workbook()
@ -551,9 +551,9 @@ workbook::~workbook()
{ {
} }
const std::vector<named_style> &workbook::get_named_styles() const const std::vector<style> &workbook::get_styles() const
{ {
return d_->named_styles_; return d_->styles_;
} }
bool workbook::get_data_only() const bool workbook::get_data_only() const
@ -605,20 +605,22 @@ std::vector<named_range> workbook::get_named_ranges() const
return named_ranges; return named_ranges;
} }
std::size_t workbook::add_style(const cell_style &style) std::size_t workbook::add_format(const format &style)
{ {
cell_style style_copy = style; auto format_copy = style;
//TODO this is ugly //TODO this is ugly
if (!style.get_number_format().has_id()) if (!style.get_number_format().has_id())
{ {
bool match = false; bool match = false;
for (std::size_t i = 0; i < d_->cell_styles_.size(); i++) for (std::size_t i = 0; i < d_->formats_.size(); i++)
{ {
if (d_->cell_styles_.at(i).get_number_format().get_format_string() == style_copy.get_number_format().get_format_string()) const auto &current_number_format = d_->formats_.at(i).get_number_format();
if (current_number_format.get_format_string() == format_copy.get_number_format().get_format_string())
{ {
style_copy.set_number_format(d_->cell_styles_.at(i).get_number_format()); format_copy.set_number_format(current_number_format);
match = true; match = true;
break; break;
} }
@ -626,36 +628,37 @@ std::size_t workbook::add_style(const cell_style &style)
if (!match) if (!match)
{ {
style_copy.get_number_format().set_id(d_->next_custom_format_id_++); format_copy.get_number_format().set_id(d_->next_custom_format_id_++);
} }
} }
for (std::size_t i = 0; i < d_->cell_styles_.size(); i++) // TODO hashmap?
for (std::size_t i = 0; i < d_->formats_.size(); i++)
{ {
if (d_->cell_styles_[i] == style_copy) if (d_->formats_[i] == format_copy)
{ {
return i; return i;
} }
} }
d_->cell_styles_.push_back(style_copy); d_->formats_.push_back(format_copy);
return d_->cell_styles_.size() - 1; return d_->formats_.size() - 1;
} }
void workbook::clear_styles() void workbook::clear_formats()
{ {
d_->cell_styles_.clear(); d_->formats_.clear();
} }
cell_style &workbook::get_style(std::size_t style_index) format &workbook::get_format(std::size_t format_index)
{ {
return d_->cell_styles_.at(style_index); return d_->formats_.at(format_index);
} }
const cell_style &workbook::get_style(std::size_t style_index) const const format &workbook::get_format(std::size_t format_index) const
{ {
return d_->cell_styles_.at(style_index); return d_->formats_.at(format_index);
} }
manifest &workbook::get_manifest() manifest &workbook::get_manifest()
@ -726,24 +729,24 @@ const std::vector<std::uint8_t> &workbook::get_thumbnail() const
return d_->thumbnail_; return d_->thumbnail_;
} }
named_style &workbook::create_named_style(const std::string &name) style &workbook::create_style(const std::string &name)
{ {
named_style style; style style;
style.set_name(name); style.set_name(name);
d_->named_styles_.push_back(style); d_->styles_.push_back(style);
return d_->named_styles_.back(); return d_->styles_.back();
} }
std::vector<cell_style> &workbook::get_styles() std::vector<format> &workbook::get_formats()
{ {
return d_->cell_styles_; return d_->formats_;
} }
const std::vector<cell_style> &workbook::get_styles() const const std::vector<format> &workbook::get_formats() const
{ {
return d_->cell_styles_; return d_->formats_;
} }
} // namespace xlnt } // namespace xlnt