From 7f5b76447fcd6486d7bc30b5d18336f231f27058 Mon Sep 17 00:00:00 2001 From: degaart Date: Thu, 24 Dec 2015 09:58:11 +0300 Subject: [PATCH] added worksheet::has_cell Added method worksheet::has_cell to check wether a const-qualified worksheet objecthas data for a given cell --- include/xlnt/worksheet/worksheet.hpp | 1 + source/worksheet/tests/test_worksheet.hpp | 12 ++++++++++++ source/worksheet/worksheet.cpp | 13 +++++++++++++ 3 files changed, 26 insertions(+) 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();