add interface to worksheet and range for clearing cells entirely, not just their values, for #232

This commit is contained in:
Thomas Fussell 2017-10-26 13:45:56 -04:00
parent 0e0bf0f8a1
commit 06b315b352
6 changed files with 120 additions and 0 deletions

View File

@ -90,6 +90,11 @@ public:
/// </summary>
range(const range &) = default;
/// <summary>
/// Erases all cell data from the worksheet for cells within this range.
/// </summary>
void clear_cells();
/// <summary>
/// Returns a vector pointing to the n-th row or column in this range (depending
/// on major order).

View File

@ -258,6 +258,16 @@ public:
//TODO: finish implementing cell_iterator wrapping before uncommenting
//const class cell_vector cells(bool skip_null = true) const;
/// <summary>
/// Clears memory used by the given cell.
/// </summary>
void clear_cell(const cell_reference &ref);
/// <summary>
/// Clears memory used by all cells in the given row.
/// </summary>
void clear_row(row_t row);
// properties
/// <summary>

View File

@ -43,6 +43,29 @@ range::~range()
{
}
void range::clear_cells()
{
if (ref_.top_left().column() == ws_.lowest_column()
&& ref_.bottom_right().column() == ws_.highest_column())
{
for (auto row = ref_.top_left().row(); row <= ref_.bottom_right().row(); ++row)
{
ws_.clear_row(row);
}
}
else
{
for (auto row = ref_.top_left().row(); row <= ref_.bottom_right().row(); ++row)
{
for (auto column = ref_.top_left().column(); column <= ref_.bottom_right().column(); ++column)
{
ws_.clear_cell(xlnt::cell_reference(column, row));
}
}
}
}
cell_vector range::operator[](std::size_t index)
{
return vector(index);

View File

@ -679,6 +679,18 @@ const cell_vector worksheet::cells(bool skip_null) const
}
*/
void worksheet::clear_cell(const cell_reference &ref)
{
d_->cell_map_.at(ref.row()).erase(ref.column());
// TODO: garbage collect newly unreferenced resources such as styles?
}
void worksheet::clear_row(row_t row)
{
d_->cell_map_.erase(row);
// TODO: garbage collect newly unreferenced resources such as styles?
}
bool worksheet::operator==(const worksheet &other) const
{
return compare(other, true);

View File

@ -36,6 +36,7 @@ public:
range_test_suite()
{
register_test(test_batch_formatting);
register_test(test_clear_cells);
}
void test_batch_formatting()
@ -66,4 +67,19 @@ public:
xlnt_assert(!ws.cell("B2").has_format());
}
void test_clear_cells()
{
xlnt::workbook wb;
auto ws = wb.active_sheet();
ws.cell("A1").value("A1");
ws.cell("A3").value("A3");
ws.cell("C1").value("C1");
ws.cell("B2").value("B2");
ws.cell("C3").value("C3");
xlnt_assert_equals(ws.calculate_dimension(), xlnt::range_reference(1, 1, 3, 3));
auto range = ws.range("B1:C3");
range.clear_cells();
xlnt_assert_equals(ws.calculate_dimension(), xlnt::range_reference(1, 1, 1, 3));
}
};

View File

@ -95,6 +95,8 @@ public:
register_test(test_iteration_skip_empty);
register_test(test_dimensions);
register_test(test_view_properties_serialization);
register_test(test_clear_cell);
register_test(test_clear_row);
}
void test_new_worksheet()
@ -1130,4 +1132,56 @@ public:
xlnt_assert(ws2.has_active_cell());
xlnt_assert_equals(ws2.active_cell(), "B1");
}
void test_clear_cell()
{
xlnt::workbook wb;
wb.load(path_helper::test_file("4_every_style.xlsx"));
auto ws = wb.active_sheet();
auto height = ws.calculate_dimension().height();
auto last_row = ws.highest_row();
xlnt_assert(ws.has_cell(xlnt::cell_reference(1, last_row)));
ws.clear_cell(xlnt::cell_reference(1, last_row));
xlnt_assert(!ws.has_cell(xlnt::cell_reference(1, last_row)));
xlnt_assert_equals(ws.highest_row(), last_row);
wb.save("temp.xlsx");
xlnt::workbook wb2;
wb2.load("temp.xlsx");
auto ws2 = wb2.active_sheet();
xlnt_assert_equals(ws2.calculate_dimension().height(), height);
xlnt_assert(!ws2.has_cell(xlnt::cell_reference(1, last_row)));
}
void test_clear_row()
{
xlnt::workbook wb;
wb.load(path_helper::test_file("4_every_style.xlsx"));
auto ws = wb.active_sheet();
auto height = ws.calculate_dimension().height();
auto last_row = ws.highest_row();
xlnt_assert(ws.has_cell(xlnt::cell_reference(1, last_row)));
ws.clear_row(last_row);
xlnt_assert(!ws.has_cell(xlnt::cell_reference(1, last_row)));
xlnt_assert_equals(ws.highest_row(), last_row - 1);
wb.save("temp.xlsx");
xlnt::workbook wb2;
wb2.load("temp.xlsx");
auto ws2 = wb2.active_sheet();
xlnt_assert_equals(ws2.calculate_dimension().height(), height - 1);
xlnt_assert(!ws2.has_cell(xlnt::cell_reference(1, last_row)));
}
};