Skip to content

Commit d88c901

Browse files
authored
Merge pull request tfussell#608 from imgspc/iter_has_value
Add has_value to cell_iterator
2 parents 0246c7b + 5ff9808 commit d88c901

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

include/xlnt/worksheet/cell_iterator.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,13 @@ class XLNT_API cell_iterator
136136
/// </summary>
137137
cell_iterator operator++(int);
138138

139+
/// <summary>
140+
/// When iterating over a range that doesn't ignore null cells, operator*()
141+
/// will throw when trying to access the cells that are null. This method
142+
/// checks the existence of a cell.
143+
/// </summary>
144+
bool has_value() const;
145+
139146
private:
140147
/// <summary>
141148
/// If true, cells that don't exist in the worksheet will be skipped during iteration.
@@ -263,6 +270,13 @@ class XLNT_API const_cell_iterator
263270
/// </summary>
264271
const_cell_iterator operator++(int);
265272

273+
/// <summary>
274+
/// When iterating over a range that doesn't ignore null cells, operator*()
275+
/// will throw when trying to access the cells that are null. This method
276+
/// checks the existence of a cell.
277+
/// </summary>
278+
bool has_value() const;
279+
266280
private:
267281
/// <summary>
268282
/// If true, cells that don't exist in the worksheet will be skipped during iteration.

source/worksheet/cell_iterator.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,4 +278,12 @@ const const_cell_iterator::reference const_cell_iterator::operator*() const
278278
{
279279
return ws_.cell(cursor_);
280280
}
281+
282+
bool cell_iterator::has_value() const{
283+
return ws_.has_cell(cursor_);
284+
}
285+
286+
bool const_cell_iterator::has_value() const{
287+
return ws_.has_cell(cursor_);
288+
}
281289
} // namespace xlnt

tests/worksheet/worksheet_test_suite.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ class worksheet_test_suite : public test_suite
7676
register_test(test_lowest_row_or_props);
7777
register_test(test_highest_row);
7878
register_test(test_highest_row_or_props);
79+
register_test(test_iterator_has_value);
7980
register_test(test_const_iterators);
8081
register_test(test_const_reverse_iterators);
8182
register_test(test_column_major_iterators);
@@ -586,6 +587,31 @@ class worksheet_test_suite : public test_suite
586587
xlnt_assert_equals(ws.highest_row_or_props(), 11);
587588
}
588589

590+
void test_iterator_has_value()
591+
{
592+
xlnt::workbook wb;
593+
auto ws = wb.active_sheet();
594+
595+
// make a worksheet with a blank row and column
596+
ws.cell("A1").value("A1");
597+
ws.cell("A3").value("A3");
598+
ws.cell("C1").value("C1");
599+
600+
xlnt_assert_equals(ws.columns(false)[0].begin().has_value(), true);
601+
xlnt_assert_equals(ws.rows(false)[0].begin().has_value(), true);
602+
603+
xlnt_assert_equals(ws.columns(false)[1].begin().has_value(), false);
604+
xlnt_assert_equals(ws.rows(false)[1].begin().has_value(), false);
605+
606+
// also test const interators.
607+
xlnt_assert_equals(ws.columns(false)[0].cbegin().has_value(), true);
608+
xlnt_assert_equals(ws.rows(false)[0].cbegin().has_value(), true);
609+
610+
xlnt_assert_equals(ws.columns(false)[1].cbegin().has_value(), false);
611+
xlnt_assert_equals(ws.rows(false)[1].cbegin().has_value(), false);
612+
613+
}
614+
589615
void test_const_iterators()
590616
{
591617
xlnt::workbook wb;

0 commit comments

Comments
 (0)