diff --git a/include/xlnt/worksheet/worksheet.hpp b/include/xlnt/worksheet/worksheet.hpp index ebffb74b..64d21b8c 100644 --- a/include/xlnt/worksheet/worksheet.hpp +++ b/include/xlnt/worksheet/worksheet.hpp @@ -86,6 +86,7 @@ public: // container cell get_cell(const cell_reference &reference); const cell get_cell(const cell_reference &reference) const; + bool has_cell(const cell_reference &reference) const; range get_range(const std::string &reference_string); range get_range(const range_reference &reference); const range get_range(const std::string &reference_string) const; diff --git a/source/worksheet/tests/test_worksheet.hpp b/source/worksheet/tests/test_worksheet.hpp index 48295480..ed5f0a47 100644 --- a/source/worksheet/tests/test_worksheet.hpp +++ b/source/worksheet/tests/test_worksheet.hpp @@ -21,6 +21,18 @@ public: auto cell = ws.get_cell(xlnt::cell_reference(1, 1)); TS_ASSERT_EQUALS(cell.get_reference(), "A1"); } + + void test_has_cell() + { + xlnt::worksheet ws(wb_); + + const xlnt::worksheet& const_ws = ws; + TS_ASSERT_EQUALS(const_ws.has_cell(xlnt::cell_reference("A", 1)), false); + + ws.get_cell("C10").set_value("value"); + TS_ASSERT_EQUALS(const_ws.has_cell(xlnt::cell_reference("C", 9)), false); + TS_ASSERT_EQUALS(const_ws.has_cell(xlnt::cell_reference("C", 10)), true); + } void test_worksheet_dimension() { diff --git a/source/worksheet/worksheet.cpp b/source/worksheet/worksheet.cpp index d4f7450b..39968e0c 100644 --- a/source/worksheet/worksheet.cpp +++ b/source/worksheet/worksheet.cpp @@ -229,6 +229,19 @@ const cell worksheet::get_cell(const cell_reference &reference) const return cell(&d_->cell_map_.at(reference.get_row()).at(reference.get_column_index())); } +bool worksheet::has_cell(const cell_reference &reference) const +{ + const auto row = d_->cell_map_.find(reference.get_row()); + if(row == d_->cell_map_.cend()) + return false; + + const auto col = row->second.find(reference.get_column_index()); + if(col == row->second.cend()) + return false; + + return true; +} + bool worksheet::has_row_properties(row_t row) const { return d_->row_properties_.find(row) != d_->row_properties_.end();