mirror of
https://github.com/tfussell/xlnt.git
synced 2024-03-22 13:11:17 +08:00
copy previous formatting when changing the format of a cell, closes #76
This commit is contained in:
parent
f2f5097164
commit
eeba8ffa86
|
@ -508,6 +508,12 @@ private:
|
||||||
/// </summary>
|
/// </summary>
|
||||||
format &get_format_internal();
|
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>
|
/// <summary>
|
||||||
/// Private constructor to create a cell from its implementation.
|
/// Private constructor to create a cell from its implementation.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
@ -51,6 +51,7 @@ public:
|
||||||
std::size_t id() const;
|
std::size_t id() const;
|
||||||
|
|
||||||
bool has_style() const;
|
bool has_style() const;
|
||||||
|
void clear_style();
|
||||||
void style(const std::string &name);
|
void style(const std::string &name);
|
||||||
void style(const xlnt::style &new_style);
|
void style(const xlnt::style &new_style);
|
||||||
const class style &style() const;
|
const class style &style() const;
|
||||||
|
|
|
@ -444,7 +444,6 @@ cell &cell::operator=(const cell &rhs)
|
||||||
d_->is_merged_ = rhs.d_->is_merged_;
|
d_->is_merged_ = rhs.d_->is_merged_;
|
||||||
d_->parent_ = rhs.d_->parent_;
|
d_->parent_ = rhs.d_->parent_;
|
||||||
d_->row_ = rhs.d_->row_;
|
d_->row_ = rhs.d_->row_;
|
||||||
d_->style_name_ = rhs.d_->style_name_;
|
|
||||||
d_->type_ = rhs.d_->type_;
|
d_->type_ = rhs.d_->type_;
|
||||||
d_->value_numeric_ = rhs.d_->value_numeric_;
|
d_->value_numeric_ = rhs.d_->value_numeric_;
|
||||||
d_->value_text_ = rhs.d_->value_text_;
|
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_);
|
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_)
|
void cell::set_border(const xlnt::border &border_)
|
||||||
{
|
{
|
||||||
auto &format = get_workbook().create_format();
|
auto &format = duplicate_format();
|
||||||
format.border(border_, true);
|
format.border(border_, true);
|
||||||
d_->format_id_ = format.id();
|
d_->format_id_ = format.id();
|
||||||
}
|
}
|
||||||
|
|
||||||
void cell::set_fill(const xlnt::fill &fill_)
|
void cell::set_fill(const xlnt::fill &fill_)
|
||||||
{
|
{
|
||||||
auto &format = get_workbook().create_format();
|
auto &format = duplicate_format();
|
||||||
format.fill(fill_, true);
|
format.fill(fill_, true);
|
||||||
d_->format_id_ = format.id();
|
d_->format_id_ = format.id();
|
||||||
}
|
}
|
||||||
|
|
||||||
void cell::set_font(const font &font_)
|
void cell::set_font(const font &font_)
|
||||||
{
|
{
|
||||||
auto &format = get_workbook().create_format();
|
auto &format = duplicate_format();
|
||||||
format.font(font_, true);
|
format.font(font_, true);
|
||||||
d_->format_id_ = format.id();
|
d_->format_id_ = format.id();
|
||||||
}
|
}
|
||||||
|
|
||||||
void cell::set_number_format(const number_format &number_format_)
|
void cell::set_number_format(const number_format &number_format_)
|
||||||
{
|
{
|
||||||
auto &format = get_workbook().create_format();
|
auto &format = duplicate_format();
|
||||||
format.number_format(number_format_, true);
|
format.number_format(number_format_, true);
|
||||||
d_->format_id_ = format.id();
|
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
void cell::set_protection(const xlnt::protection &protection_)
|
void cell::set_protection(const xlnt::protection &protection_)
|
||||||
{
|
{
|
||||||
auto &format = get_workbook().create_format();
|
auto &format = duplicate_format();
|
||||||
format.protection(protection_, true);
|
format.protection(protection_, true);
|
||||||
d_->format_id_ = format.id();
|
d_->format_id_ = format.id();
|
||||||
}
|
}
|
||||||
|
@ -897,32 +896,39 @@ void cell::clear_format()
|
||||||
|
|
||||||
void cell::clear_style()
|
void cell::clear_style()
|
||||||
{
|
{
|
||||||
d_->style_name_.clear();
|
if (has_format())
|
||||||
|
{
|
||||||
|
get_format_internal().clear_style();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void cell::set_style(const style &new_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)
|
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
|
style cell::get_style() const
|
||||||
{
|
{
|
||||||
if (!d_->style_name_)
|
if (!has_format() || !get_format().has_style())
|
||||||
{
|
{
|
||||||
throw invalid_attribute();
|
throw invalid_attribute();
|
||||||
}
|
}
|
||||||
|
|
||||||
return get_workbook().get_style(*d_->style_name_);
|
return get_format().style();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool cell::has_style() const
|
bool cell::has_style() const
|
||||||
{
|
{
|
||||||
return d_->style_name_;
|
return has_format() && get_format().has_style();
|
||||||
}
|
}
|
||||||
|
|
||||||
base_format cell::get_computed_format() const
|
base_format cell::get_computed_format() const
|
||||||
|
@ -979,6 +985,25 @@ format &cell::get_format_internal()
|
||||||
return get_workbook().get_format(*d_->format_id_);
|
return get_workbook().get_format(*d_->format_id_);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
format &cell::duplicate_format()
|
||||||
|
{
|
||||||
|
auto &new_format = get_workbook().create_format();
|
||||||
|
|
||||||
|
if (has_format())
|
||||||
|
{
|
||||||
|
auto ¤t_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
|
format cell::get_format() const
|
||||||
{
|
{
|
||||||
if (!d_->format_id_)
|
if (!d_->format_id_)
|
||||||
|
|
|
@ -55,7 +55,6 @@ struct cell_impl
|
||||||
optional<std::string> formula_;
|
optional<std::string> formula_;
|
||||||
optional<std::string> hyperlink_;
|
optional<std::string> hyperlink_;
|
||||||
optional<std::size_t> format_id_;
|
optional<std::size_t> format_id_;
|
||||||
optional<std::string> style_name_;
|
|
||||||
optional<comment> comment_;
|
optional<comment> comment_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -38,6 +38,11 @@ std::size_t format::id() const
|
||||||
return d_->id;
|
return d_->id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void format::clear_style()
|
||||||
|
{
|
||||||
|
d_->style.clear();
|
||||||
|
}
|
||||||
|
|
||||||
void format::style(const xlnt::style &new_style)
|
void format::style(const xlnt::style &new_style)
|
||||||
{
|
{
|
||||||
d_->style = new_style.name();
|
d_->style = new_style.name();
|
||||||
|
|
|
@ -53,4 +53,22 @@ public:
|
||||||
TS_ASSERT_DIFFERS(gradient_fill_linear, gradient_fill_path);
|
TS_ASSERT_DIFFERS(gradient_fill_linear, gradient_fill_path);
|
||||||
TS_ASSERT_DIFFERS(gradient_fill_path, pattern_fill);
|
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()));
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue
Block a user