diff --git a/include/xlnt/cell/cell.hpp b/include/xlnt/cell/cell.hpp index 9d45f496..803893ac 100644 --- a/include/xlnt/cell/cell.hpp +++ b/include/xlnt/cell/cell.hpp @@ -508,6 +508,12 @@ private: /// format &get_format_internal(); + /// + /// Use workbook::create_format() to create a new format then copy + /// this cell's formatting to that new format and return it. + /// + format &duplicate_format(); + /// /// Private constructor to create a cell from its implementation. /// diff --git a/include/xlnt/styles/format.hpp b/include/xlnt/styles/format.hpp index 7915287c..f609c235 100644 --- a/include/xlnt/styles/format.hpp +++ b/include/xlnt/styles/format.hpp @@ -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; diff --git a/source/cell/cell.cpp b/source/cell/cell.cpp index 230eba65..0bcd539b 100644 --- a/source/cell/cell.cpp +++ b/source/cell/cell.cpp @@ -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 ¤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 { if (!d_->format_id_) diff --git a/source/detail/cell_impl.hpp b/source/detail/cell_impl.hpp index 266c3999..f1703c71 100644 --- a/source/detail/cell_impl.hpp +++ b/source/detail/cell_impl.hpp @@ -55,7 +55,6 @@ struct cell_impl optional formula_; optional hyperlink_; optional format_id_; - optional style_name_; optional comment_; }; diff --git a/source/styles/format.cpp b/source/styles/format.cpp index 1a67595a..1bfb5cb7 100644 --- a/source/styles/format.cpp +++ b/source/styles/format.cpp @@ -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(); diff --git a/source/styles/tests/test_fill.hpp b/source/styles/tests/test_fill.hpp index 8e84b406..729c61cc 100644 --- a/source/styles/tests/test_fill.hpp +++ b/source/styles/tests/test_fill.hpp @@ -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())); + } };