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 fill;
class font;
class format;
class number_format;
class protection;
class relationship;
@ -53,9 +54,7 @@ struct datetime;
struct time;
struct timedelta;
namespace detail {
struct cell_impl;
}
namespace detail { struct cell_impl; }
/// <summary>
/// Describes cell associated properties.
@ -191,21 +190,21 @@ public:
// style
/// <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>
bool has_style() const;
bool has_format() const;
/// <summary>
/// Return a reference to the style applied to this cell.
/// Return a reference to the format applied to this cell.
/// </summary>
cell_style &get_style();
format &get_format();
/// <summary>
/// Return a reference to the style applied to this cell.
/// Return a reference to the format applied to this cell.
/// </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>
/// 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);
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
friend class style;

View File

@ -28,24 +28,24 @@
#include <vector>
#include <xlnt/xlnt_config.hpp>
#include <xlnt/styles/named_style.hpp>
#include <xlnt/styles/style.hpp>
#include <xlnt/workbook/workbook.hpp>
namespace xlnt {
class alignment;
class border;
class cell_style;
class color;
class common_style;
class conditional_format;
class fill;
class font;
class format;
class named_style;
class formattable;
class style;
class number_format;
class protection;
class side;
class style;
class xml_document;
class xml_node;
@ -140,15 +140,15 @@ public:
/// these is "applied". For example, a style with a defined font id, font=# but with
/// "applyFont=0" will not use the font in formatting.
/// </summary>
cell_style read_cell_style(const xml_node &style_node);
format read_format(const xml_node &format_node);
/// <summary>
/// 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
/// built-in and an xfId which is the index of an element in cellStyleXfs which should
/// be provided as the style_node parameter (cellStyleXfs itself, not the child xf node).
/// built-in and an xfId which is the index of an element in cellStyleXfs. style_format_node
/// should be the XML node of the element at the index of xfId.
/// </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)
@ -184,13 +184,13 @@ public:
/// Read all cell styles from cell_styles_node and add them to workbook.
/// Return true on success.
/// </summary>
bool read_cell_styles(const xml_node &cell_styles_node);
bool read_formats(const xml_node &formats_node);
/// <summary>
/// Read all named styles from named_style_node and cell_styles_node and add them to workbook.
/// Return true on success.
/// </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>
/// Read all borders from number_formats_node and add them to workbook.
@ -232,27 +232,15 @@ public:
/// </summary>
bool write_number_formats(xml_node &number_formats_node) const;
bool write_style_common(const common_style &style, 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;
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_styles(xml_node &styles_node) const;
bool write_formats(xml_node &styles_node) const;
/// <summary>
/// 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_styles(xml_node &cell_styles_node, xml_node &cell_style_formats_node) const;
bool write_colors(xml_node &colors_node) const;
@ -281,8 +269,8 @@ private:
std::vector<fill> fills_;
std::vector<font> fonts_;
std::vector<number_format> number_formats_;
std::vector<cell_style> cell_styles_;
std::unordered_map<std::size_t, named_style> named_styles_;
std::vector<format> formats_;
std::vector<style> styles_;
};
} // namespace xlnt

View File

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

View File

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

View File

@ -23,7 +23,10 @@
// @author: see AUTHORS file
#pragma once
#include <xlnt/styles/common_style.hpp>
#include <cstdint>
#include <string>
#include <xlnt/styles/formattable.hpp>
namespace xlnt {
@ -31,14 +34,14 @@ class workbook;
/// <summary>
/// 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>
class XLNT_CLASS named_style : public common_style
class XLNT_CLASS style : public formattable
{
public:
named_style();
named_style(const named_style &other);
named_style &operator=(const named_style &other);
style();
style(const style &other);
style &operator=(const style &other);
std::string get_name() const;
void set_name(const std::string &name);

View File

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

View File

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

View File

@ -31,7 +31,7 @@
#include <xlnt/packaging/document_properties.hpp>
#include <xlnt/packaging/relationship.hpp>
#include <xlnt/serialization/encoding.hpp>
#include <xlnt/styles/cell_style.hpp>
#include <xlnt/styles/format.hpp>
#include <xlnt/styles/color.hpp>
#include <xlnt/utils/date.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
{
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 <>
@ -389,7 +389,7 @@ XLNT_FUNCTION void cell::set_value(cell c)
d_->hyperlink_ = c.d_->hyperlink_;
d_->has_hyperlink_ = c.d_->has_hyperlink_;
d_->formula_ = c.d_->formula_;
d_->style_id_ = c.d_->style_id_;
d_->format_id_ = c.d_->format_id_;
set_comment(c.get_comment());
}
@ -691,73 +691,73 @@ void cell::set_data_type(type t)
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
{
return get_workbook().get_style(0).get_number_format();
return get_workbook().get_format(0).get_number_format();
}
}
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
{
return get_workbook().get_style(0).get_font();
return get_workbook().get_format(0).get_font();
}
}
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
{
return get_workbook().get_style(0).get_fill();
return get_workbook().get_format(0).get_fill();
}
}
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
{
return get_workbook().get_style(0).get_border();
return get_workbook().get_format(0).get_border();
}
}
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
{
return get_workbook().get_style(0).get_alignment();
return get_workbook().get_format(0).get_alignment();
}
}
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
{
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_)
{
d_->has_style_ = true;
auto style_copy = get_workbook().get_style(d_->style_id_);
style_copy.set_border(border_);
d_->style_id_ = get_workbook().add_style(style_copy);
d_->has_format_ = true;
auto format_copy = get_workbook().get_format(d_->format_id_);
format_copy.set_border(border_);
d_->format_id_ = get_workbook().add_format(format_copy);
}
void cell::set_fill(const xlnt::fill &fill_)
{
d_->has_style_ = true;
auto style_copy = get_workbook().get_style(d_->style_id_);
style_copy.set_fill(fill_);
d_->style_id_ = get_workbook().add_style(style_copy);
d_->has_format_ = true;
auto format_copy = get_workbook().get_format(d_->format_id_);
format_copy.set_fill(fill_);
d_->format_id_ = get_workbook().add_format(format_copy);
}
void cell::set_font(const font &font_)
{
d_->has_style_ = true;
auto style_copy = get_workbook().get_style(d_->style_id_);
style_copy.set_font(font_);
d_->style_id_ = get_workbook().add_style(style_copy);
d_->has_format_ = true;
auto format_copy = get_workbook().get_format(d_->format_id_);
format_copy.set_font(font_);
d_->format_id_ = get_workbook().add_format(format_copy);
}
void cell::set_number_format(const number_format &number_format_)
{
d_->has_style_ = true;
auto style_copy = get_workbook().get_style(d_->style_id_);
style_copy.set_number_format(number_format_);
d_->style_id_ = get_workbook().add_style(style_copy);
d_->has_format_ = true;
auto format_copy = get_workbook().get_format(d_->format_id_);
format_copy.set_number_format(number_format_);
d_->format_id_ = get_workbook().add_format(format_copy);
}
void cell::set_alignment(const xlnt::alignment &alignment_)
{
d_->has_style_ = true;
auto style_copy = get_workbook().get_style(d_->style_id_);
style_copy.set_alignment(alignment_);
d_->style_id_ = get_workbook().add_style(style_copy);
d_->has_format_ = true;
auto format_copy = get_workbook().get_format(d_->format_id_);
format_copy.set_alignment(alignment_);
d_->format_id_ = get_workbook().add_format(format_copy);
}
void cell::set_protection(const xlnt::protection &protection_)
{
d_->has_style_ = true;
auto style_copy = get_workbook().get_style(d_->style_id_);
style_copy.set_protection(protection_);
d_->style_id_ = get_workbook().add_style(style_copy);
d_->has_format_ = true;
auto format_copy = get_workbook().get_format(d_->format_id_);
format_copy.set_protection(protection_);
d_->format_id_ = get_workbook().add_format(format_copy);
}
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_->has_style_ = true;
d_->format_id_ = get_workbook().add_format(new_format);
d_->has_format_ = true;
}
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();
}
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)

View File

@ -10,9 +10,9 @@
#include <xlnt/serialization/encoding.hpp>
#include <xlnt/styles/alignment.hpp>
#include <xlnt/styles/border.hpp>
#include <xlnt/styles/cell_style.hpp>
#include <xlnt/styles/font.hpp>
#include <xlnt/styles/fill.hpp>
#include <xlnt/styles/format.hpp>
#include <xlnt/styles/number_format.hpp>
#include <xlnt/styles/protection.hpp>
#include <xlnt/utils/date.hpp>
@ -367,8 +367,8 @@ public:
cell.set_font(font);
TS_ASSERT(cell.has_style());
TS_ASSERT(cell.get_style().font_applied());
TS_ASSERT(cell.has_format());
TS_ASSERT(cell.get_format().font_applied());
TS_ASSERT_EQUALS(cell.get_font(), font);
}
@ -383,8 +383,8 @@ public:
cell.set_fill(f);
TS_ASSERT(cell.has_style());
TS_ASSERT(cell.get_style().fill_applied());
TS_ASSERT(cell.has_format());
TS_ASSERT(cell.get_format().fill_applied());
TS_ASSERT_EQUALS(cell.get_fill(), f);
}
@ -397,8 +397,8 @@ public:
cell.set_border(border);
TS_ASSERT(cell.has_style());
TS_ASSERT(cell.get_style().border_applied());
TS_ASSERT(cell.has_format());
TS_ASSERT(cell.get_format().border_applied());
TS_ASSERT_EQUALS(cell.get_border(), border);
}
@ -410,8 +410,8 @@ public:
xlnt::number_format format("dd--hh--mm");
cell.set_number_format(format);
TS_ASSERT(cell.has_style());
TS_ASSERT(cell.get_style().number_format_applied());
TS_ASSERT(cell.has_format());
TS_ASSERT(cell.get_format().number_format_applied());
TS_ASSERT_EQUALS(cell.get_number_format().get_format_string(), "dd--hh--mm");
}
@ -425,8 +425,8 @@ public:
cell.set_alignment(align);
TS_ASSERT(cell.has_style());
TS_ASSERT(cell.get_style().alignment_applied());
TS_ASSERT(cell.has_format());
TS_ASSERT(cell.get_format().alignment_applied());
TS_ASSERT_EQUALS(cell.get_alignment(), align);
}
@ -440,8 +440,8 @@ public:
cell.set_protection(prot);
TS_ASSERT(cell.has_style());
TS_ASSERT(cell.get_style().protection_applied());
TS_ASSERT(cell.has_format());
TS_ASSERT(cell.get_format().protection_applied());
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),
has_hyperlink_(false),
is_merged_(false),
has_style_(false),
style_id_(0),
has_format_(false),
format_id_(0),
comment_(nullptr)
{
}
@ -68,8 +68,8 @@ cell_impl &cell_impl::operator=(const cell_impl &rhs)
is_merged_ = rhs.is_merged_;
has_hyperlink_ = rhs.has_hyperlink_;
type_ = rhs.type_;
style_id_ = rhs.style_id_;
has_style_ = rhs.has_style_;
format_id_ = rhs.format_id_;
has_format_ = rhs.has_format_;
if (rhs.comment_ != nullptr)
{

View File

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

View File

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

View File

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

View File

@ -30,11 +30,10 @@
#include <xlnt/serialization/xml_node.hpp>
#include <xlnt/styles/alignment.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/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/protection.hpp>
#include <xlnt/workbook/worksheet_iterator.hpp>
@ -197,7 +196,7 @@ style_serializer::style_serializer(workbook &wb) : workbook_(wb)
{
for (auto cell : row)
{
if (cell.has_style())
if (cell.has_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;
}
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
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"));
s.set_alignment(inline_alignment);
auto inline_alignment = read_alignment(format_node.get_child("alignment"));
f.set_alignment(inline_alignment);
}
// Border
auto border_index = style_node.has_attribute("borderId") ? std::stoull(style_node.get_attribute("borderId")) : 0;
s.set_border(borders_.at(border_index));
s.border_applied(is_true(style_node.get_attribute("applyBorder")));
auto border_index = format_node.has_attribute("borderId") ? std::stoull(format_node.get_attribute("borderId")) : 0;
f.set_border(borders_.at(border_index));
f.border_applied(is_true(format_node.get_attribute("applyBorder")));
// Fill
auto fill_index = style_node.has_attribute("fillId") ? std::stoull(style_node.get_attribute("fillId")) : 0;
s.set_fill(fills_.at(fill_index));
s.fill_applied(is_true(style_node.get_attribute("applyFill")));
auto fill_index = format_node.has_attribute("fillId") ? std::stoull(format_node.get_attribute("fillId")) : 0;
f.set_fill(fills_.at(fill_index));
f.fill_applied(is_true(format_node.get_attribute("applyFill")));
// Font
auto font_index = style_node.has_attribute("fontId") ? std::stoull(style_node.get_attribute("fontId")) : 0;
s.set_font(fonts_.at(font_index));
s.font_applied(is_true(style_node.get_attribute("applyFont")));
auto font_index = format_node.has_attribute("fontId") ? std::stoull(format_node.get_attribute("fontId")) : 0;
f.set_font(fonts_.at(font_index));
f.font_applied(is_true(format_node.get_attribute("applyFont")));
// 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;
@ -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)
{
s.set_number_format(num_fmt);
f.set_number_format(num_fmt);
builtin_format = false;
break;
}
@ -340,32 +339,32 @@ cell_style style_serializer::read_cell_style(const xml_node &style_node)
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
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"));
s.set_protection(inline_protection);
auto inline_protection = read_protection(format_node.get_child("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_hidden(cell_style_node.has_attribute("hidden") && is_true(cell_style_node.get_attribute("hidden")));
s.set_builtin_id(std::stoull(cell_style_node.get_attribute("builtinId")));
s.set_name(style_node.get_attribute("name"));
s.set_hidden(style_node.has_attribute("hidden") && is_true(style_node.get_attribute("hidden")));
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()
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_number_formats(stylesheet_node.get_child("numFmts"));
read_colors(stylesheet_node.get_child("colors"));
read_named_styles(stylesheet_node.get_child("cellStyles"), stylesheet_node.get_child("cellStyleXfs"));
read_cell_styles(stylesheet_node.get_child("cellXfs"));
read_styles(stylesheet_node.get_child("cellStyles"), stylesheet_node.get_child("cellStyleXfs"));
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;
}
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;
}
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 named_style = named_styles_.at(named_style_index);
auto style_index = std::stoull(format_node.get_attribute("xfId"));
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;
}
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;
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"));
if (cell_style_format_index == style_index)
{
auto ns = read_named_style(cell_style_node, cell_style_format_node);
named_styles_[style_index] = ns;
ns = read_style(cell_style_node, cell_style_format_node);
match = true;
break;
}
}
styles_.push_back(ns);
style_index++;
}
if (named_styles_.empty())
if (styles_.empty())
{
named_style ns;
style ns;
ns.set_name("Standard");
ns.set_hidden(false);
ns.set_builtin_id(0);
named_styles_[0] = ns;
styles_.push_back(ns);
}
return true;
@ -762,7 +766,7 @@ void style_serializer::initialize_vectors()
{
for (auto cell : row)
{
if (cell.has_style())
if (cell.has_format())
{
any_style = true;
}
@ -772,7 +776,7 @@ void style_serializer::initialize_vectors()
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();
@ -781,26 +785,26 @@ void style_serializer::initialize_vectors()
fonts_.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;
}
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));
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));
}
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("applyNumberFormat", style.number_format_applied() ? "1" : "0");
xf_node.add_attribute("applyFill", style.fill_applied() ? "1" : "0");
xf_node.add_attribute("applyFont", style.font_applied() ? "1" : "0");
xf_node.add_attribute("applyBorder", style.border_applied() ? "1" : "0");
xf_node.add_attribute("applyAlignment", style.alignment_applied() ? "1" : "0");
xf_node.add_attribute("applyProtection", style.protection_applied() ? "1" : "0");
xf_node.add_attribute("applyNumberFormat", xf.number_format_applied() ? "1" : "0");
xf_node.add_attribute("applyFill", xf.fill_applied() ? "1" : "0");
xf_node.add_attribute("applyFont", xf.font_applied() ? "1" : "0");
xf_node.add_attribute("applyBorder", xf.border_applied() ? "1" : "0");
xf_node.add_attribute("applyAlignment", xf.alignment_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");
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:
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:
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");
}
if (style.get_alignment().get_shrink_to_fit())
if (xf.get_alignment().get_shrink_to_fit())
{
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;
}
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
{
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_)
for(auto &current_style : styles_)
{
auto xf_node = styles_node.add_child("xf");
write_named_style(key_style.second, xf_node);
}
cell_styles_node.add_attribute("count", std::to_string(named_styles_.size()));
auto xf_node = style_formats_node.add_child("xf");
write_formattable(current_style, xf_node);
for(auto &key_style : named_styles_)
{
auto cell_style_node = cell_styles_node.add_child("cellStyle");
auto cell_style_node = styles_node.add_child("cellStyle");
cell_style_node.add_attribute("name", key_style.second.get_name());
cell_style_node.add_attribute("xfId", std::to_string(key_style.first));
cell_style_node.add_attribute("builtinId", std::to_string(key_style.second.get_builtin_id()));
cell_style_node.add_attribute("name", current_style.get_name());
cell_style_node.add_attribute("xfId", std::to_string(style_index++));
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");
}
@ -1235,15 +1226,19 @@ bool style_serializer::write_named_styles(xml_node &cell_styles_node, xml_node &
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");
write_cell_style(style, xf_node);
xf_node.add_attribute("xfId", "0"); //TODO point to named style here
auto xf_node = formats_node.add_child("xf");
write_formattable(current_format, xf_node);
if(current_format.has_style())
{
xf_node.add_attribute("xfId", "0");
}
}
return true;
@ -1312,10 +1307,10 @@ xml_document style_serializer::write_stylesheet()
auto cell_style_xfs_node = root_node.add_child("cellStyleXfs");
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");
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");
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");
std::string type = has_type ? cell_node.get_attribute("t") : "";
bool has_style = cell_node.has_attribute("s");
auto style_id = static_cast<std::size_t>(has_style ? std::stoull(cell_node.get_attribute("s")) : 0LL);
bool has_format = cell_node.has_attribute("s");
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_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
// @author: see AUTHORS file
#include <xlnt/styles/common_style.hpp>
#include <xlnt/styles/formattable.hpp>
namespace xlnt {
common_style::common_style()
formattable::formattable()
: apply_alignment_(false),
apply_border_(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_),
border_(other.border_),
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_;
border_ = other.border_;
@ -70,105 +70,105 @@ common_style &common_style::operator=(const xlnt::common_style &other)
return *this;
}
alignment &common_style::get_alignment()
alignment &formattable::get_alignment()
{
return alignment_;
}
const alignment &common_style::get_alignment() const
const alignment &formattable::get_alignment() const
{
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_applied(true);
}
number_format &common_style::get_number_format()
number_format &formattable::get_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_;
}
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_applied(true);
}
border &common_style::get_border()
border &formattable::get_border()
{
return border_;
}
const border &common_style::get_border() const
const border &formattable::get_border() const
{
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_applied(true);
}
fill &common_style::get_fill()
fill &formattable::get_fill()
{
return fill_;
}
const fill &common_style::get_fill() const
const fill &formattable::get_fill() const
{
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_applied(true);
}
font &common_style::get_font()
font &formattable::get_font()
{
return font_;
}
const font &common_style::get_font() const
const font &formattable::get_font() const
{
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_applied(true);
}
protection &common_style::get_protection()
protection &formattable::get_protection()
{
return protection_;
}
const protection &common_style::get_protection() const
const protection &formattable::get_protection() const
{
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_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(alignment_applied() ? std::to_string(alignment_.hash()) : ":");
@ -191,62 +191,62 @@ std::string common_style::to_hash_string() const
return hash_string;
}
void common_style::alignment_applied(bool applied)
void formattable::alignment_applied(bool applied)
{
apply_alignment_ = applied;
}
void common_style::border_applied(bool applied)
void formattable::border_applied(bool applied)
{
apply_border_ = applied;
}
void common_style::fill_applied(bool applied)
void formattable::fill_applied(bool applied)
{
apply_fill_ = applied;
}
void common_style::font_applied(bool applied)
void formattable::font_applied(bool applied)
{
apply_font_ = applied;
}
void common_style::number_format_applied(bool applied)
void formattable::number_format_applied(bool applied)
{
apply_number_format_ = applied;
}
void common_style::protection_applied(bool applied)
void formattable::protection_applied(bool applied)
{
apply_protection_ = applied;
}
bool common_style::alignment_applied() const
bool formattable::alignment_applied() const
{
return apply_alignment_;
}
bool common_style::border_applied() const
bool formattable::border_applied() const
{
return apply_border_;
}
bool common_style::fill_applied() const
bool formattable::fill_applied() const
{
return apply_fill_;
}
bool common_style::font_applied() const
bool formattable::font_applied() const
{
return apply_font_;
}
bool common_style::number_format_applied() const
bool formattable::number_format_applied() const
{
return apply_number_format_;
}
bool common_style::protection_applied() const
bool formattable::protection_applied() const
{
return apply_protection_;
}

View File

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

View File

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

View File

@ -36,10 +36,10 @@
#include <xlnt/serialization/excel_serializer.hpp>
#include <xlnt/styles/alignment.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/font.hpp>
#include <xlnt/styles/named_style.hpp>
#include <xlnt/styles/style.hpp>
#include <xlnt/styles/number_format.hpp>
#include <xlnt/styles/protection.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/app.xml", "application/vnd.openxmlformats-officedocument.extended-properties+xml");
add_style(cell_style());
add_format(format());
}
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
@ -605,20 +605,22 @@ std::vector<named_range> workbook::get_named_ranges() const
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
if (!style.get_number_format().has_id())
{
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;
break;
}
@ -626,36 +628,37 @@ std::size_t workbook::add_style(const cell_style &style)
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;
}
}
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()
@ -726,24 +729,24 @@ const std::vector<std::uint8_t> &workbook::get_thumbnail() const
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);
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