copy previous formatting when changing the format of a cell, closes #76

This commit is contained in:
Thomas Fussell 2016-11-01 08:50:29 -04:00
parent f2f5097164
commit eeba8ffa86
6 changed files with 76 additions and 22 deletions

View File

@ -508,6 +508,12 @@ private:
/// </summary>
format &get_format_internal();
/// <summary>
/// Use workbook::create_format() to create a new format then copy
/// this cell's formatting to that new format and return it.
/// </summary>
format &duplicate_format();
/// <summary>
/// Private constructor to create a cell from its implementation.
/// </summary>

View File

@ -51,6 +51,7 @@ public:
std::size_t id() const;
bool has_style() const;
void clear_style();
void style(const std::string &name);
void style(const xlnt::style &new_style);
const class style &style() const;

View File

@ -444,7 +444,6 @@ cell &cell::operator=(const cell &rhs)
d_->is_merged_ = rhs.d_->is_merged_;
d_->parent_ = rhs.d_->parent_;
d_->row_ = rhs.d_->row_;
d_->style_name_ = rhs.d_->style_name_;
d_->type_ = rhs.d_->type_;
d_->value_numeric_ = rhs.d_->value_numeric_;
d_->value_text_ = rhs.d_->value_text_;
@ -757,44 +756,44 @@ XLNT_API timedelta cell::get_value() const
return timedelta::from_number(d_->value_numeric_);
}
void cell::set_alignment(const xlnt::alignment &alignment_)
{
auto &format = duplicate_format();
format.alignment(alignment_, true);
d_->format_id_ = format.id();
}
void cell::set_border(const xlnt::border &border_)
{
auto &format = get_workbook().create_format();
auto &format = duplicate_format();
format.border(border_, true);
d_->format_id_ = format.id();
}
void cell::set_fill(const xlnt::fill &fill_)
{
auto &format = get_workbook().create_format();
auto &format = duplicate_format();
format.fill(fill_, true);
d_->format_id_ = format.id();
}
void cell::set_font(const font &font_)
{
auto &format = get_workbook().create_format();
auto &format = duplicate_format();
format.font(font_, true);
d_->format_id_ = format.id();
}
void cell::set_number_format(const number_format &number_format_)
{
auto &format = get_workbook().create_format();
format.number_format(number_format_, true);
d_->format_id_ = format.id();
}
void cell::set_alignment(const xlnt::alignment &alignment_)
{
auto &format = get_workbook().create_format();
format.alignment(alignment_, true);
d_->format_id_ = format.id();
auto &format = duplicate_format();
format.number_format(number_format_, true);
d_->format_id_ = format.id();
}
void cell::set_protection(const xlnt::protection &protection_)
{
auto &format = get_workbook().create_format();
auto &format = duplicate_format();
format.protection(protection_, true);
d_->format_id_ = format.id();
}
@ -897,32 +896,39 @@ void cell::clear_format()
void cell::clear_style()
{
d_->style_name_.clear();
if (has_format())
{
get_format_internal().clear_style();
}
}
void cell::set_style(const style &new_style)
{
d_->style_name_ = new_style.name();
auto &new_format = duplicate_format();
new_format.style(new_style);
d_->format_id_ = new_format.id();
}
void cell::set_style(const std::string &style_name)
{
d_->style_name_ = get_workbook().get_style(style_name).name();
auto &new_format = duplicate_format();
new_format.style(get_workbook().get_style(style_name).name());
d_->format_id_ = new_format.id();
}
style cell::get_style() const
{
if (!d_->style_name_)
if (!has_format() || !get_format().has_style())
{
throw invalid_attribute();
}
return get_workbook().get_style(*d_->style_name_);
return get_format().style();
}
bool cell::has_style() const
{
return d_->style_name_;
return has_format() && get_format().has_style();
}
base_format cell::get_computed_format() const
@ -979,6 +985,25 @@ format &cell::get_format_internal()
return get_workbook().get_format(*d_->format_id_);
}
format &cell::duplicate_format()
{
auto &new_format = get_workbook().create_format();
if (has_format())
{
auto &current_format = get_format_internal();
new_format.alignment(current_format.alignment(), current_format.alignment_applied());
new_format.border(current_format.border(), current_format.border_applied());
new_format.fill(current_format.fill(), current_format.fill_applied());
new_format.font(current_format.font(), current_format.font_applied());
new_format.number_format(current_format.number_format(), current_format.number_format_applied());
new_format.protection(current_format.protection(), current_format.protection_applied());
}
return new_format;
}
format cell::get_format() const
{
if (!d_->format_id_)

View File

@ -55,7 +55,6 @@ struct cell_impl
optional<std::string> formula_;
optional<std::string> hyperlink_;
optional<std::size_t> format_id_;
optional<std::string> style_name_;
optional<comment> comment_;
};

View File

@ -38,6 +38,11 @@ std::size_t format::id() const
return d_->id;
}
void format::clear_style()
{
d_->style.clear();
}
void format::style(const xlnt::style &new_style)
{
d_->style = new_style.name();

View File

@ -53,4 +53,22 @@ public:
TS_ASSERT_DIFFERS(gradient_fill_linear, gradient_fill_path);
TS_ASSERT_DIFFERS(gradient_fill_path, pattern_fill);
}
void test_two_fills()
{
xlnt::workbook wb;
auto ws1 = wb.get_active_sheet();
auto cell1 = ws1.get_cell("A10");
auto cell2 = ws1.get_cell("A11");
cell1.set_fill(xlnt::fill::solid(xlnt::color::yellow()));
cell1.set_value("Fill Yellow");
cell2.set_fill(xlnt::fill::solid(xlnt::color::green()));
cell2.set_value(xlnt::date(2010, 7, 13));
TS_ASSERT_EQUALS(cell1.get_fill(), xlnt::fill::solid(xlnt::color::yellow()));
TS_ASSERT_EQUALS(cell2.get_fill(), xlnt::fill::solid(xlnt::color::green()));
}
};