fix sheet deletion, #71

This commit is contained in:
Thomas Fussell 2016-10-22 13:53:44 -04:00
parent 071f5962c2
commit 0a80c302b3
4 changed files with 38 additions and 3 deletions

View File

@ -94,6 +94,11 @@ public:
/// </summary>
std::string register_relationship(const uri &source, relationship::type type, const uri &target, target_mode mode, const std::string &rel_id);
/// <summary>
///
/// </summary>
void unregister_relationship(const uri &source, const std::string &rel_id);
// Content Types
/// <summary>

View File

@ -98,6 +98,11 @@ void manifest::register_override_type(const path &part, const std::string &conte
override_content_types_[part] = content_type;
}
void manifest::unregister_override_type(const path &part)
{
override_content_types_.erase(part);
}
std::vector<path> manifest::get_parts_with_overriden_types() const
{
std::vector<path> overriden;
@ -176,6 +181,11 @@ std::string manifest::register_relationship(const uri &source, relationship::typ
return rel_id;
}
void manifest::unregister_relationship(const uri &source, const std::string &rel_id)
{
relationships_.at(source.get_path()).erase(rel_id);
}
bool manifest::has_default_type(const std::string &extension) const
{
return default_content_types_.find(extension) != default_content_types_.end();

View File

@ -121,4 +121,20 @@ public:
wb.save("simple.xlsx");
}
void test_save_after_sheet_deletion()
{
xlnt::workbook workbook;
TS_ASSERT_EQUALS(workbook.get_sheet_titles().size(), 1);
auto sheet = workbook.create_sheet();
sheet.set_title("XXX1");
TS_ASSERT_EQUALS(workbook.get_sheet_titles().size(), 2);
workbook.remove_sheet(workbook.get_sheet_by_title("XXX1"));
TS_ASSERT_EQUALS(workbook.get_sheet_titles().size(), 1);
TS_ASSERT_THROWS_NOTHING(workbook.save("sample.xlsx"));
}
};

View File

@ -694,15 +694,19 @@ bool workbook::get_guess_types() const
void workbook::remove_sheet(worksheet ws)
{
auto match_iter = std::find_if(d_->worksheets_.begin(), d_->worksheets_.end(),
[=](detail::worksheet_impl &comp) { return worksheet(&comp) == ws; });
[=](detail::worksheet_impl &comp) { return &comp == ws.d_; });
if (match_iter == d_->worksheets_.end())
{
throw invalid_parameter();
}
auto sheet_filename = path("worksheets/sheet" + std::to_string(d_->worksheets_.size()) + ".xml");
auto ws_rel_id = d_->sheet_title_rel_id_map_.at(ws.get_title());
auto wb_rel = d_->manifest_.get_relationship(xlnt::path("/"), xlnt::relationship_type::office_document);
auto ws_rel = d_->manifest_.get_relationship(wb_rel.get_target().get_path(), ws_rel_id);
d_->manifest_.unregister_override_type(ws_rel.get_target().get_path());
d_->manifest_.unregister_relationship(wb_rel.get_target(), ws_rel_id);;
d_->sheet_title_rel_id_map_.erase(ws.get_title());
d_->worksheets_.erase(match_iter);
}