remove calcChain when no formulae remain in any cell, correct calcChain relationship type string, closes #98

This commit is contained in:
Thomas Fussell 2017-01-02 19:12:23 -05:00
parent 34304fd9c7
commit c43561b4bd
9 changed files with 75 additions and 2 deletions

View File

@ -758,6 +758,11 @@ private:
/// </summary>
void register_comments_in_manifest(worksheet ws);
/// <summary>
/// Removes calcChain part from manifest if no formulae remain in workbook.
/// </summary>
void garbage_collect_formulae();
/// <summary>
/// An opaque pointer to a structure that holds all of the data relating to this workbook.
/// </summary>

View File

@ -742,6 +742,11 @@ private:
/// </summary>
void register_comments_in_manifest();
/// <summary>
/// Removes calcChain part from manifest if no formulae remain in workbook.
/// </summary>
void garbage_collect_formulae();
/// <summary>
///
/// </summary>

View File

@ -508,6 +508,7 @@ std::string cell::formula() const
void cell::clear_formula()
{
d_->formula_.clear();
worksheet().garbage_collect_formulae();
}
void cell::error(const std::string &error)
@ -609,8 +610,8 @@ void cell::clear_value()
{
d_->value_numeric_ = 0;
d_->value_text_.clear();
d_->formula_.clear();
d_->type_ = cell::type::null;
clear_formula();
}
template <>

View File

@ -37,7 +37,7 @@ std::string to_string(relationship_type t)
case relationship_type::thumbnail:
return "http://schemas.openxmlformats.org/package/2006/relationships/metadata/thumbnail";
case relationship_type::calculation_chain:
return "http://purl.oclc.org/ooxml/officeDocument/relationships/calcChain";
return "http://schemas.openxmlformats.org/officeDocument/2006/relationships/calcChain";
case relationship_type::extended_properties:
return "http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties";
case relationship_type::core_properties:

View File

@ -112,4 +112,16 @@ public:
TS_ASSERT(wb.has_custom_property("Client"));
TS_ASSERT_EQUALS(wb.custom_property("Client"), "me!");
}
void test_read_formulae()
{
xlnt::workbook wb;
wb.load("data/22_formulae.xlsx");
auto ws = wb.active_sheet();
TS_ASSERT_EQUALS(ws.cell("A1").value<int>(), 6);
TS_ASSERT(ws.cell("A1").has_formula());
TS_ASSERT_EQUALS(ws.cell("A1").formula(), "A2*A3");
TS_ASSERT_EQUALS(ws.cell("A2").value<int>(), 2);
TS_ASSERT_EQUALS(ws.cell("A3").value<int>(), 3);
}
};

View File

@ -154,4 +154,15 @@ public:
TS_ASSERT(workbook_matches_file(wb, xlnt::path("data/15_basic_comments.xlsx")));
}
void test_save_after_clear_all_formulae()
{
xlnt::workbook wb;
wb.load("data/22_formulae.xlsx");
auto ws = wb.active_sheet();
TS_ASSERT(ws.cell("A1").has_formula());
TS_ASSERT_EQUALS(ws.cell("A1").formula(), "A2*A3");
ws.cell("A1").clear_formula();
wb.save("clear_formulae.xlsx");
}
};

View File

@ -1260,4 +1260,38 @@ void workbook::calculation_properties(const class calculation_properties &props)
d_->calculation_properties_ = props;
}
/// <summary>
/// Removes calcChain part from manifest if no formulae remain in workbook.
/// </summary>
void workbook::garbage_collect_formulae()
{
auto any_with_formula = false;
for (auto ws : *this)
{
for (auto row : ws.iter_cells(true))
{
for (auto cell : row)
{
if (cell.has_formula())
{
any_with_formula = true;
}
}
}
}
if (any_with_formula) return;
auto wb_rel = manifest().relationship(path("/"), relationship_type::office_document);
if (manifest().has_relationship(wb_rel.target().path(), relationship_type::calculation_chain))
{
auto calc_chain_rel = manifest().relationship(wb_rel.target().path(), relationship_type::calculation_chain);
auto calc_chain_part = manifest().canonicalize({wb_rel, calc_chain_rel});
manifest().unregister_override_type(calc_chain_part);
manifest().unregister_relationship(wb_rel.target(), calc_chain_rel.id());
}
}
} // namespace xlnt

View File

@ -1068,4 +1068,9 @@ double worksheet::row_height(row_t row) const
}
}
void worksheet::garbage_collect_formulae()
{
workbook().garbage_collect_formulae();
}
} // namespace xlnt

BIN
tests/data/22_formulae.xlsx Normal file

Binary file not shown.