diff --git a/include/xlnt/worksheet/worksheet.hpp b/include/xlnt/worksheet/worksheet.hpp index 1b895696..49aee891 100644 --- a/include/xlnt/worksheet/worksheet.hpp +++ b/include/xlnt/worksheet/worksheet.hpp @@ -211,75 +211,62 @@ public: const class range range(const range_reference &reference) const; /// - /// + /// Returns a range encompassing all cells in this sheet which will + /// be iterated upon in row-major order. /// class range rows() const; /// - /// - /// - class range rows(const std::string &range_string) const; - - /// - /// - /// - class range rows(int row_offset, int column_offset) const; - - /// - /// - /// - class range rows(const std::string &range_string, int row_offset, int column_offset) const; - - /// - /// + /// Returns a range ecompassing all cells in this sheet which will + /// be iterated upon in column-major order. /// class range columns() const; // properties /// - /// + /// Returns the column properties for the given column. /// xlnt::column_properties &column_properties(column_t column); /// - /// + /// Returns the column properties for the given column. /// const xlnt::column_properties &column_properties(column_t column) const; /// - /// + /// Returns true if column properties have been set for the given column. /// bool has_column_properties(column_t column) const; /// - /// + /// Sets column properties for the given column to props. /// void add_column_properties(column_t column, const class column_properties &props); /// - /// Calculate the width of the given column. This will be the default column width if + /// Calculates the width of the given column. This will be the default column width if /// a custom width is not set on this column's column_properties. /// double column_width(column_t column) const; /// - /// + /// Returns the row properties for the given row. /// xlnt::row_properties &row_properties(row_t row); /// - /// + /// Returns the row properties for the given row. /// const xlnt::row_properties &row_properties(row_t row) const; /// - /// + /// Returns true if row properties have been set for the given row. /// bool has_row_properties(row_t row) const; /// - /// + /// Sets row properties for the given row to props. /// void add_row_properties(row_t row, const class row_properties &props); @@ -292,7 +279,7 @@ public: // positioning /// - /// + /// Returns a reference to the cell at the given point coordinates. /// cell_reference point_pos(int left, int top) const; @@ -399,38 +386,6 @@ public: /// std::vector merged_ranges() const; - // append - - /// - /// - /// - void append(); - - /// - /// - /// - void append(const std::vector &cells); - - /// - /// - /// - void append(const std::vector &cells); - - /// - /// - /// - void append(const std::unordered_map &cells); - - /// - /// - /// - void append(const std::unordered_map &cells); - - /// - /// - /// - void append(const std::vector::const_iterator begin, const std::vector::const_iterator end); - // operators /// @@ -459,27 +414,18 @@ public: void operator=(const worksheet &other); /// - /// + /// Convenience method for worksheet::cell method. /// class cell operator[](const cell_reference &reference); /// - /// + /// Convenience method for worksheet::cell method. /// const class cell operator[](const cell_reference &reference) const; /// - /// - /// - class range operator()(const cell_reference &top_left, const cell_reference &bottom_right); - - /// - /// - /// - const class range operator()(const cell_reference &top_left, const cell_reference &bottom_right) const; - - /// - /// + /// Returns true if this worksheet is equal to other. If reference is true, the comparison + /// will only check that both worksheets point to the same sheet in the same workbook. /// bool compare(const worksheet &other, bool reference) const; diff --git a/source/worksheet/tests/test_worksheet.hpp b/source/worksheet/tests/test_worksheet.hpp index 1ac02648..14a6796f 100644 --- a/source/worksheet/tests/test_worksheet.hpp +++ b/source/worksheet/tests/test_worksheet.hpp @@ -62,97 +62,7 @@ public: TS_ASSERT_EQUALS(ws.rows()[row][column].reference(), coordinate); } - - void test_iter_rows() - { - xlnt::workbook wb; - auto ws = wb.active_sheet(); - - const std::vector> expected = - { - { "A1", "B1", "C1" }, - { "A2", "B2", "C2" }, - { "A3", "B3", "C3" }, - { "A4", "B4", "C4" } - }; - - auto rows = ws.rows("A1:C4"); - auto expected_row_iter = expected.begin(); - - for(auto row : rows) - { - auto expected_cell_iter = (*expected_row_iter).begin(); - - for(auto cell : row) - { - TS_ASSERT_EQUALS(cell.reference(), *expected_cell_iter); - expected_cell_iter++; - } - - expected_row_iter++; - } - } - - void test_iter_rows_offset() - { - xlnt::workbook wb; - auto ws = wb.active_sheet(); - auto rows = ws.rows("A1:C4", 1, 3); - - const std::vector> expected = - { - { "D2", "E2", "F2" }, - { "D3", "E3", "F3" }, - { "D4", "E4", "F4" }, - { "D5", "E5", "F5" } - }; - - auto expected_row_iter = expected.begin(); - - for(auto row : rows) - { - auto expected_cell_iter = (*expected_row_iter).begin(); - - for(auto cell : row) - { - TS_ASSERT_EQUALS(cell.reference(), *expected_cell_iter); - expected_cell_iter++; - } - - expected_row_iter++; - } - } - void test_iter_rows_offset_int_int() - { - xlnt::workbook wb; - auto ws = wb.active_sheet(); - auto rows = ws.rows(1, 3); - - const std::vector> expected = - { - { "D2", "E2", "F2" }, - { "D3", "E3", "F3" }, - { "D4", "E4", "F4" }, - { "D5", "E5", "F5" } - }; - - auto expected_row_iter = expected.begin(); - - for (auto row : rows) - { - auto expected_cell_iter = (*expected_row_iter).begin(); - - for (auto cell : row) - { - TS_ASSERT_EQUALS(cell.reference(), *expected_cell_iter); - expected_cell_iter++; - } - - expected_row_iter++; - } - } - void test_get_named_range() { xlnt::workbook wb; @@ -228,91 +138,6 @@ public: TS_ASSERT_EQUALS("test", ws.cell("A1").value()); TS_ASSERT_EQUALS(ws.cell("A1").hyperlink(), "http://test.com"); } - - void test_append() - { - xlnt::workbook wb; - auto ws = wb.active_sheet(); - ws.append(std::vector {"value"}); - TS_ASSERT_EQUALS("value", ws.cell("A1").value()); - } - - void test_append_list() - { - xlnt::workbook wb; - auto ws = wb.active_sheet(); - - ws.append(std::vector {"This is A1", "This is B1"}); - - TS_ASSERT_EQUALS("This is A1", ws.cell("A1").value()); - TS_ASSERT_EQUALS("This is B1", ws.cell("B1").value()); - } - - void test_append_dict_letter() - { - xlnt::workbook wb; - auto ws = wb.active_sheet(); - - const std::unordered_map dict_letter = - { - { "A", "This is A1" }, - { "C", "This is C1" } - }; - - ws.append(dict_letter); - - TS_ASSERT_EQUALS("This is A1", ws.cell("A1").value()); - TS_ASSERT_EQUALS("This is C1", ws.cell("C1").value()); - } - - void test_append_dict_index() - { - xlnt::workbook wb; - auto ws = wb.active_sheet(); - - const std::unordered_map dict_index = - { - { 1, "This is A1" }, - { 3, "This is C1" } - }; - - ws.append(dict_index); - - TS_ASSERT_EQUALS("This is A1", ws.cell("A1").value()); - TS_ASSERT_EQUALS("This is C1", ws.cell("C1").value()); - } - - void test_append_iterator() - { - std::vector range; - - for(int i = 0; i < 30; i++) - { - range.push_back(i); - } - - xlnt::workbook wb; - auto ws = wb.active_sheet(); - ws.append(range.begin(), range.end()); - - TS_ASSERT_EQUALS(ws[xlnt::cell_reference("AD1")].value(), 29); - } - - void test_append_2d_list() - { - xlnt::workbook wb; - auto ws = wb.active_sheet(); - - ws.append(std::vector {"This is A1", "This is B1"}); - ws.append(std::vector {"This is A2", "This is B2"}); - - auto vals = ws.range("A1:B2"); - - TS_ASSERT_EQUALS(vals[0][0].value(), "This is A1"); - TS_ASSERT_EQUALS(vals[0][1].value(), "This is B1"); - TS_ASSERT_EQUALS(vals[1][0].value(), "This is A2"); - TS_ASSERT_EQUALS(vals[1][1].value(), "This is B2"); - } void test_rows() { @@ -418,7 +243,7 @@ public: { xlnt::workbook wb; auto ws = wb.active_sheet(); - auto cell_range = ws("A1", "B2"); + auto cell_range = ws.range("A1:B2"); TS_ASSERT_EQUALS(cell_range[0][0], ws.cell("A1")); TS_ASSERT_EQUALS(cell_range[1][0], ws.cell("A2")); TS_ASSERT_EQUALS(cell_range[0][1], ws.cell("B1")); @@ -637,10 +462,7 @@ public: { xlnt::workbook wb; auto ws = wb.active_sheet(); - ws.append(); - ws.append(std::vector { 5 }); - ws.append(); - ws.append(std::vector { 4 }); + ws.cell("D4").value("D4"); TS_ASSERT_EQUALS(ws.highest_row(), 4); } @@ -649,8 +471,12 @@ public: xlnt::workbook wb; auto ws = wb.active_sheet(); - ws.append({"A1", "B1", "C1"}); - ws.append({"A2", "B2", "C2"}); + ws.cell("A1").value("A1"); + ws.cell("B1").value("B1"); + ws.cell("C1").value("C1"); + ws.cell("A2").value("A2"); + ws.cell("B2").value("B2"); + ws.cell("C2").value("C2"); const xlnt::worksheet ws_const = ws; const auto rows = ws_const.rows(); @@ -677,8 +503,12 @@ public: xlnt::workbook wb; auto ws = wb.active_sheet(); - ws.append({"A1", "B1", "C1"}); - ws.append({"A2", "B2", "C2"}); + ws.cell("A1").value("A1"); + ws.cell("B1").value("B1"); + ws.cell("C1").value("C1"); + ws.cell("A2").value("A2"); + ws.cell("B2").value("B2"); + ws.cell("C2").value("C2"); const xlnt::worksheet ws_const = ws; const auto rows = ws_const.rows(); @@ -712,8 +542,12 @@ public: xlnt::workbook wb; auto ws = wb.active_sheet(); - ws.append({"A1", "B1", "C1"}); - ws.append({"A2", "B2", "C2"}); + ws.cell("A1").value("A1"); + ws.cell("B1").value("B1"); + ws.cell("C1").value("C1"); + ws.cell("A2").value("A2"); + ws.cell("B2").value("B2"); + ws.cell("C2").value("C2"); auto columns = ws.columns(); @@ -751,8 +585,12 @@ public: xlnt::workbook wb; auto ws = wb.active_sheet(); - ws.append({"A1", "B1", "C1"}); - ws.append({"A2", "B2", "C2"}); + ws.cell("A1").value("A1"); + ws.cell("B1").value("B1"); + ws.cell("C1").value("C1"); + ws.cell("A2").value("A2"); + ws.cell("B2").value("B2"); + ws.cell("C2").value("C2"); auto columns = ws.columns(); @@ -793,8 +631,12 @@ public: xlnt::workbook wb; auto ws = wb.active_sheet(); - ws.append({"A1", "B1", "C1"}); - ws.append({"A2", "B2", "C2"}); + ws.cell("A1").value("A1"); + ws.cell("B1").value("B1"); + ws.cell("C1").value("C1"); + ws.cell("A2").value("A2"); + ws.cell("B2").value("B2"); + ws.cell("C2").value("C2"); const xlnt::worksheet ws_const = ws; const auto columns = ws_const.columns(); @@ -830,8 +672,12 @@ public: xlnt::workbook wb; auto ws = wb.active_sheet(); - ws.append({"A1", "B1", "C1"}); - ws.append({"A2", "B2", "C2"}); + ws.cell("A1").value("A1"); + ws.cell("B1").value("B1"); + ws.cell("C1").value("C1"); + ws.cell("A2").value("A2"); + ws.cell("B2").value("B2"); + ws.cell("C2").value("C2"); const xlnt::worksheet ws_const = ws; const auto columns = ws_const.columns(); diff --git a/source/worksheet/worksheet.cpp b/source/worksheet/worksheet.cpp index 3286a6af..49ccc1db 100644 --- a/source/worksheet/worksheet.cpp +++ b/source/worksheet/worksheet.cpp @@ -111,11 +111,6 @@ void worksheet::create_named_range(const std::string &name, const range_referenc d_->named_ranges_[name] = xlnt::named_range(name, targets); } -range worksheet::operator()(const xlnt::cell_reference &top_left, const xlnt::cell_reference &bottom_right) -{ - return range(range_reference(top_left, bottom_right)); -} - cell worksheet::operator[](const cell_reference &ref) { return cell(ref); @@ -595,22 +590,6 @@ void worksheet::unmerge_cells(column_t start_column, row_t start_row, column_t e unmerge_cells(xlnt::range_reference(start_column, start_row, end_column, end_row)); } -void worksheet::append() -{ - cell(cell_reference(1, next_row())); -} - -void worksheet::append(const std::vector &cells) -{ - xlnt::cell_reference next(1, next_row()); - - for (auto cell : cells) - { - this->cell(next).value(cell); - next.column_index(next.column_index() + 1); - } -} - row_t worksheet::next_row() const { auto row = highest_row() + 1; @@ -623,70 +602,11 @@ row_t worksheet::next_row() const return row; } -void worksheet::append(const std::vector &cells) -{ - xlnt::cell_reference next(1, next_row()); - - for (auto cell : cells) - { - this->cell(next).value(static_cast(cell)); - next.column_index(next.column_index() + 1); - } -} - -void worksheet::append(const std::unordered_map &cells) -{ - auto row = next_row(); - - for (auto cell : cells) - { - this->cell(cell_reference(cell.first, row)).value(cell.second); - } -} - -void worksheet::append(const std::unordered_map &cells) -{ - auto row = next_row(); - - for (auto cell : cells) - { - this->cell(cell_reference(static_cast(cell.first), row)).value(cell.second); - } -} - -void worksheet::append(const std::vector::const_iterator begin, const std::vector::const_iterator end) -{ - xlnt::cell_reference next(1, next_row()); - - for (auto i = begin; i != end; i++) - { - cell(next).value(static_cast(*i)); - next.column_index(next.column_index() + 1); - } -} - xlnt::range worksheet::rows() const { return range(calculate_dimension()); } -xlnt::range worksheet::rows(const std::string &range_string) const -{ - return range(range_reference(range_string)); -} - -xlnt::range worksheet::rows(const std::string &range_string, int row_offset, int column_offset) const -{ - range_reference reference(range_string); - return range(reference.make_offset(column_offset, row_offset)); -} - -xlnt::range worksheet::rows(int row_offset, int column_offset) const -{ - range_reference reference(calculate_dimension()); - return range(reference.make_offset(column_offset, row_offset)); -} - xlnt::range worksheet::columns() const { return xlnt::range(*this, calculate_dimension(), major_order::column);