diff --git a/source/tests/WorksheetTestSuite.h b/source/tests/WorksheetTestSuite.h index 3c5c7dc0..1ba10aab 100644 --- a/source/tests/WorksheetTestSuite.h +++ b/source/tests/WorksheetTestSuite.h @@ -80,7 +80,7 @@ public: void test_bad_named_range() { xlnt::worksheet ws(wb); - ws.range("bad_range"); + TS_ASSERT_THROWS_ANYTHING(ws.range("bad_range")); } void test_named_range_wrong_sheet() @@ -88,7 +88,7 @@ public: xlnt::worksheet ws1(wb); xlnt::worksheet ws2(wb); wb.create_named_range("wrong_sheet_range", ws1, "C5"); - ws2.range("wrong_sheet_range"); + TS_ASSERT_THROWS_ANYTHING(ws2.range("wrong_sheet_range")); } void test_cell_offset() @@ -245,7 +245,7 @@ public: TS_ASSERT_EQUALS(cols.size(), 3); TS_ASSERT_EQUALS(cols[0][0], "first"); - TS_ASSERT_EQUALS(cols[8][2], "last"); + TS_ASSERT_EQUALS(cols[2][8], "last"); } void test_auto_filter() diff --git a/source/tests/ZipFileTestSuite.h b/source/tests/ZipFileTestSuite.h index 3af65427..87a376a0 100644 --- a/source/tests/ZipFileTestSuite.h +++ b/source/tests/ZipFileTestSuite.h @@ -10,34 +10,26 @@ class ZipFileTestSuite : public CxxTest::TestSuite public: ZipFileTestSuite() { - /* - template_zip = "../../source/tests/test_data/packaging/test.zip"; - test_zip = "../../source/tests/test_data/packaging/a.zip"; - existing_xlsx = "../../source/tests/test_data/packaging/existing.xlsx"; - new_xlsx = "../../source/tests/test_data/packaging/new.xlsx"; - - xlnt::file::copy(template_zip, test_zip, true);*/ + existing_xlsx = "/Users/thomas/Development/xlnt/source/tests/test_data/reader/date_1990.xlsx"; + new_xlsx = "/Users/thomas/Development/xlnt/source/tests/test_data/writer/new.xlsx"; } void test_existing_package() { - //xlnt::package package; - //package.open(existing_xlsx, xlnt::file_mode::Open, xlnt::file_access::Read); + xlnt::zip_file package(existing_xlsx, xlnt::file_mode::open, xlnt::file_access::read); } void test_new_package() { - xlnt::zip_file package(new_xlsx, xlnt::file_mode::create, xlnt::file_access::read_write); - - //auto part_1 = package.create_part("workbook.xml", "type"); - //TS_ASSERT_DIFFERS(part_1, nullptr); - - //part_1.write("test"); + { + xlnt::zip_file package(new_xlsx, xlnt::file_mode::create, xlnt::file_access::read_write); + package.flush(); + } } void test_read_text() { - xlnt::zip_file package(test_zip, xlnt::file_mode::open, xlnt::file_access::read_write); + xlnt::zip_file package(existing_xlsx, xlnt::file_mode::open, xlnt::file_access::read); auto contents = package.get_file_contents("a.txt"); TS_ASSERT_EQUALS(contents, "a.txt"); } @@ -45,12 +37,12 @@ public: void test_write_text() { { - xlnt::zip_file package(test_zip, xlnt::file_mode::open, xlnt::file_access::read_write); + xlnt::zip_file package(new_xlsx, xlnt::file_mode::open, xlnt::file_access::read); package.set_file_contents("a.txt", "something else"); } { - xlnt::zip_file package(test_zip, xlnt::file_mode::open, xlnt::file_access::read_write); + xlnt::zip_file package(new_xlsx, xlnt::file_mode::open, xlnt::file_access::write); auto contents = package.get_file_contents("a.txt"); TS_ASSERT_EQUALS(contents, "something else"); } @@ -58,7 +50,7 @@ public: void test_read_xml() { - xlnt::zip_file package(test_zip, xlnt::file_mode::open, xlnt::file_access::read_write); + xlnt::zip_file package(existing_xlsx, xlnt::file_mode::open, xlnt::file_access::read); auto contents = package.get_file_contents("a.txt"); pugi::xml_document doc; @@ -79,8 +71,6 @@ public: } private: - std::string template_zip; - std::string test_zip; std::string existing_xlsx; std::string new_xlsx; }; diff --git a/source/tests/runner-autogen.cpp b/source/tests/runner-autogen.cpp index fa175fc7..462eb895 100644 --- a/source/tests/runner-autogen.cpp +++ b/source/tests/runner-autogen.cpp @@ -26,7 +26,7 @@ bool suite_CellTestSuite_init = false; static CellTestSuite suite_CellTestSuite; static CxxTest::List Tests_CellTestSuite = { 0, 0 }; -CxxTest::StaticSuiteDescription suiteDescription_CellTestSuite( "../../source/tests/CellTestSuite.h", 9, "CellTestSuite", suite_CellTestSuite, Tests_CellTestSuite ); +CxxTest::StaticSuiteDescription suiteDescription_CellTestSuite( "CellTestSuite.h", 9, "CellTestSuite", suite_CellTestSuite, Tests_CellTestSuite ); static class TestDescription_suite_CellTestSuite_test_coordinates : public CxxTest::RealTestDescription { public: @@ -189,7 +189,7 @@ public: static ChartTestSuite suite_ChartTestSuite; static CxxTest::List Tests_ChartTestSuite = { 0, 0 }; -CxxTest::StaticSuiteDescription suiteDescription_ChartTestSuite( "../../source/tests/ChartTestSuite.h", 8, "ChartTestSuite", suite_ChartTestSuite, Tests_ChartTestSuite ); +CxxTest::StaticSuiteDescription suiteDescription_ChartTestSuite( "ChartTestSuite.h", 8, "ChartTestSuite", suite_ChartTestSuite, Tests_ChartTestSuite ); static class TestDescription_suite_ChartTestSuite_test_write_title : public CxxTest::RealTestDescription { public: @@ -280,7 +280,7 @@ public: static DumpTestSuite suite_DumpTestSuite; static CxxTest::List Tests_DumpTestSuite = { 0, 0 }; -CxxTest::StaticSuiteDescription suiteDescription_DumpTestSuite( "../../source/tests/DumpTestSuite.h", 9, "DumpTestSuite", suite_DumpTestSuite, Tests_DumpTestSuite ); +CxxTest::StaticSuiteDescription suiteDescription_DumpTestSuite( "DumpTestSuite.h", 9, "DumpTestSuite", suite_DumpTestSuite, Tests_DumpTestSuite ); static class TestDescription_suite_DumpTestSuite_test_dump_sheet_title : public CxxTest::RealTestDescription { public: @@ -323,7 +323,7 @@ public: static IterTestSuite suite_IterTestSuite; static CxxTest::List Tests_IterTestSuite = { 0, 0 }; -CxxTest::StaticSuiteDescription suiteDescription_IterTestSuite( "../../source/tests/IterTestSuite.h", 8, "IterTestSuite", suite_IterTestSuite, Tests_IterTestSuite ); +CxxTest::StaticSuiteDescription suiteDescription_IterTestSuite( "IterTestSuite.h", 8, "IterTestSuite", suite_IterTestSuite, Tests_IterTestSuite ); static class TestDescription_suite_IterTestSuite_test_get_dimensions : public CxxTest::RealTestDescription { public: @@ -372,7 +372,7 @@ public: static NamedRangeTestSuite suite_NamedRangeTestSuite; static CxxTest::List Tests_NamedRangeTestSuite = { 0, 0 }; -CxxTest::StaticSuiteDescription suiteDescription_NamedRangeTestSuite( "../../source/tests/NamedRangeTestSuite.h", 8, "NamedRangeTestSuite", suite_NamedRangeTestSuite, Tests_NamedRangeTestSuite ); +CxxTest::StaticSuiteDescription suiteDescription_NamedRangeTestSuite( "NamedRangeTestSuite.h", 8, "NamedRangeTestSuite", suite_NamedRangeTestSuite, Tests_NamedRangeTestSuite ); static class TestDescription_suite_NamedRangeTestSuite_test_split : public CxxTest::RealTestDescription { public: @@ -463,7 +463,7 @@ public: static NullableTestSuite suite_NullableTestSuite; static CxxTest::List Tests_NullableTestSuite = { 0, 0 }; -CxxTest::StaticSuiteDescription suiteDescription_NullableTestSuite( "../../source/tests/NullableTestSuite.h", 8, "NullableTestSuite", suite_NullableTestSuite, Tests_NullableTestSuite ); +CxxTest::StaticSuiteDescription suiteDescription_NullableTestSuite( "NullableTestSuite.h", 8, "NullableTestSuite", suite_NullableTestSuite, Tests_NullableTestSuite ); static class TestDescription_suite_NullableTestSuite_test_has_value : public CxxTest::RealTestDescription { public: @@ -500,7 +500,7 @@ public: static NumberFormatTestSuite suite_NumberFormatTestSuite; static CxxTest::List Tests_NumberFormatTestSuite = { 0, 0 }; -CxxTest::StaticSuiteDescription suiteDescription_NumberFormatTestSuite( "../../source/tests/NumberFormatTestSuite.h", 8, "NumberFormatTestSuite", suite_NumberFormatTestSuite, Tests_NumberFormatTestSuite ); +CxxTest::StaticSuiteDescription suiteDescription_NumberFormatTestSuite( "NumberFormatTestSuite.h", 8, "NumberFormatTestSuite", suite_NumberFormatTestSuite, Tests_NumberFormatTestSuite ); static class TestDescription_suite_NumberFormatTestSuite_test_convert_date_to_julian : public CxxTest::RealTestDescription { public: @@ -603,7 +603,7 @@ public: static PasswordHashTestSuite suite_PasswordHashTestSuite; static CxxTest::List Tests_PasswordHashTestSuite = { 0, 0 }; -CxxTest::StaticSuiteDescription suiteDescription_PasswordHashTestSuite( "../../source/tests/PasswordHashTestSuite.h", 8, "PasswordHashTestSuite", suite_PasswordHashTestSuite, Tests_PasswordHashTestSuite ); +CxxTest::StaticSuiteDescription suiteDescription_PasswordHashTestSuite( "PasswordHashTestSuite.h", 8, "PasswordHashTestSuite", suite_PasswordHashTestSuite, Tests_PasswordHashTestSuite ); static class TestDescription_suite_PasswordHashTestSuite_test_hasher : public CxxTest::RealTestDescription { public: @@ -622,7 +622,7 @@ public: static PropsTestSuite suite_PropsTestSuite; static CxxTest::List Tests_PropsTestSuite = { 0, 0 }; -CxxTest::StaticSuiteDescription suiteDescription_PropsTestSuite( "../../source/tests/PropsTestSuite.h", 8, "PropsTestSuite", suite_PropsTestSuite, Tests_PropsTestSuite ); +CxxTest::StaticSuiteDescription suiteDescription_PropsTestSuite( "PropsTestSuite.h", 8, "PropsTestSuite", suite_PropsTestSuite, Tests_PropsTestSuite ); static class TestDescription_suite_PropsTestSuite_test_1 : public CxxTest::RealTestDescription { public: @@ -671,7 +671,7 @@ public: static ReadTestSuite suite_ReadTestSuite; static CxxTest::List Tests_ReadTestSuite = { 0, 0 }; -CxxTest::StaticSuiteDescription suiteDescription_ReadTestSuite( "../../source/tests/ReadTestSuite.h", 8, "ReadTestSuite", suite_ReadTestSuite, Tests_ReadTestSuite ); +CxxTest::StaticSuiteDescription suiteDescription_ReadTestSuite( "ReadTestSuite.h", 8, "ReadTestSuite", suite_ReadTestSuite, Tests_ReadTestSuite ); static class TestDescription_suite_ReadTestSuite_test_read_standalone_worksheet : public CxxTest::RealTestDescription { public: @@ -804,7 +804,7 @@ public: static StringsTestSuite suite_StringsTestSuite; static CxxTest::List Tests_StringsTestSuite = { 0, 0 }; -CxxTest::StaticSuiteDescription suiteDescription_StringsTestSuite( "../../source/tests/StringsTestSuite.h", 8, "StringsTestSuite", suite_StringsTestSuite, Tests_StringsTestSuite ); +CxxTest::StaticSuiteDescription suiteDescription_StringsTestSuite( "StringsTestSuite.h", 8, "StringsTestSuite", suite_StringsTestSuite, Tests_StringsTestSuite ); static class TestDescription_suite_StringsTestSuite_test_create_string_table : public CxxTest::RealTestDescription { public: @@ -835,7 +835,7 @@ public: static StyleTestSuite suite_StyleTestSuite; static CxxTest::List Tests_StyleTestSuite = { 0, 0 }; -CxxTest::StaticSuiteDescription suiteDescription_StyleTestSuite( "../../source/tests/StyleTestSuite.h", 8, "StyleTestSuite", suite_StyleTestSuite, Tests_StyleTestSuite ); +CxxTest::StaticSuiteDescription suiteDescription_StyleTestSuite( "StyleTestSuite.h", 8, "StyleTestSuite", suite_StyleTestSuite, Tests_StyleTestSuite ); static class TestDescription_suite_StyleTestSuite_test_create_style_table : public CxxTest::RealTestDescription { public: @@ -932,7 +932,7 @@ public: static ThemeTestSuite suite_ThemeTestSuite; static CxxTest::List Tests_ThemeTestSuite = { 0, 0 }; -CxxTest::StaticSuiteDescription suiteDescription_ThemeTestSuite( "../../source/tests/ThemeTestSuite.h", 8, "ThemeTestSuite", suite_ThemeTestSuite, Tests_ThemeTestSuite ); +CxxTest::StaticSuiteDescription suiteDescription_ThemeTestSuite( "ThemeTestSuite.h", 8, "ThemeTestSuite", suite_ThemeTestSuite, Tests_ThemeTestSuite ); static class TestDescription_suite_ThemeTestSuite_test_write_theme : public CxxTest::RealTestDescription { public: @@ -945,7 +945,7 @@ public: static UnicodeTestSuite suite_UnicodeTestSuite; static CxxTest::List Tests_UnicodeTestSuite = { 0, 0 }; -CxxTest::StaticSuiteDescription suiteDescription_UnicodeTestSuite( "../../source/tests/UnicodeTestSuite.h", 8, "UnicodeTestSuite", suite_UnicodeTestSuite, Tests_UnicodeTestSuite ); +CxxTest::StaticSuiteDescription suiteDescription_UnicodeTestSuite( "UnicodeTestSuite.h", 8, "UnicodeTestSuite", suite_UnicodeTestSuite, Tests_UnicodeTestSuite ); static class TestDescription_suite_UnicodeTestSuite_test_read_workbook_with_unicode_character : public CxxTest::RealTestDescription { public: @@ -958,7 +958,7 @@ public: static WorkbookTestSuite suite_WorkbookTestSuite; static CxxTest::List Tests_WorkbookTestSuite = { 0, 0 }; -CxxTest::StaticSuiteDescription suiteDescription_WorkbookTestSuite( "../../source/tests/WorkbookTestSuite.h", 8, "WorkbookTestSuite", suite_WorkbookTestSuite, Tests_WorkbookTestSuite ); +CxxTest::StaticSuiteDescription suiteDescription_WorkbookTestSuite( "WorkbookTestSuite.h", 8, "WorkbookTestSuite", suite_WorkbookTestSuite, Tests_WorkbookTestSuite ); static class TestDescription_suite_WorkbookTestSuite_test_get_active_sheet : public CxxTest::RealTestDescription { public: @@ -1187,7 +1187,7 @@ public: static WorksheetTestSuite suite_WorksheetTestSuite; static CxxTest::List Tests_WorksheetTestSuite = { 0, 0 }; -CxxTest::StaticSuiteDescription suiteDescription_WorksheetTestSuite( "../../source/tests/WorksheetTestSuite.h", 9, "WorksheetTestSuite", suite_WorksheetTestSuite, Tests_WorksheetTestSuite ); +CxxTest::StaticSuiteDescription suiteDescription_WorksheetTestSuite( "WorksheetTestSuite.h", 9, "WorksheetTestSuite", suite_WorksheetTestSuite, Tests_WorksheetTestSuite ); static class TestDescription_suite_WorksheetTestSuite_test_new_worksheet : public CxxTest::RealTestDescription { public: @@ -1362,7 +1362,7 @@ public: static WriteTestSuite suite_WriteTestSuite; static CxxTest::List Tests_WriteTestSuite = { 0, 0 }; -CxxTest::StaticSuiteDescription suiteDescription_WriteTestSuite( "../../source/tests/WriteTestSuite.h", 9, "WriteTestSuite", suite_WriteTestSuite, Tests_WriteTestSuite ); +CxxTest::StaticSuiteDescription suiteDescription_WriteTestSuite( "WriteTestSuite.h", 9, "WriteTestSuite", suite_WriteTestSuite, Tests_WriteTestSuite ); static class TestDescription_suite_WriteTestSuite_test_write_empty_workbook : public CxxTest::RealTestDescription { public: @@ -1495,7 +1495,7 @@ public: static ZipFileTestSuite suite_ZipFileTestSuite; static CxxTest::List Tests_ZipFileTestSuite = { 0, 0 }; -CxxTest::StaticSuiteDescription suiteDescription_ZipFileTestSuite( "../../source/tests/ZipFileTestSuite.h", 8, "ZipFileTestSuite", suite_ZipFileTestSuite, Tests_ZipFileTestSuite ); +CxxTest::StaticSuiteDescription suiteDescription_ZipFileTestSuite( "ZipFileTestSuite.h", 8, "ZipFileTestSuite", suite_ZipFileTestSuite, Tests_ZipFileTestSuite ); static class TestDescription_suite_ZipFileTestSuite_test_existing_package : public CxxTest::RealTestDescription { public: diff --git a/source/xlnt.cpp b/source/xlnt.cpp index 04ae85cd..c306983e 100644 --- a/source/xlnt.cpp +++ b/source/xlnt.cpp @@ -480,7 +480,7 @@ struct cell_struct cell_struct(worksheet_struct *ws, int column, int row) : type(cell::type::null), parent_worksheet(ws), column(column), row(row), - hyperlink_rel("hyperlink") + hyperlink_rel("invalid", "") { } @@ -566,7 +566,7 @@ std::string cell::get_value() const int cell::get_row() const { - return root_->row; + return root_->row + 1; } std::string cell::get_column() const @@ -669,12 +669,14 @@ void cell::set_explicit_value(const std::string &value, type data_type) case type::null: return; case type::numeric: root_->numeric_value = std::stod(value); return; case type::string: root_->string_value = value; return; + default: throw std::runtime_error("bad enum"); } } -void cell::set_hyperlink(const std::string &hyperlink) +void cell::set_hyperlink(const std::string &url) { - root_->hyperlink_rel = worksheet(root_->parent_worksheet).create_relationship(hyperlink); + root_->type = type::hyperlink; + root_->hyperlink_rel = worksheet(root_->parent_worksheet).create_relationship("hyperlink", url); } void cell::set_merged(bool merged) @@ -863,7 +865,15 @@ bool cell::operator==(std::nullptr_t) const bool cell::operator==(const std::string &comparand) const { - return root_->type == cell::type::string && root_->string_value == comparand; + if(root_->type == type::hyperlink) + { + return root_->hyperlink_rel.get_target_uri() == comparand; + } + if(root_->type == type::string) + { + return root_->string_value == comparand; + } + return false; } bool cell::operator==(const char *comparand) const @@ -1100,7 +1110,7 @@ struct worksheet_struct int highest = 0; for(auto cell : cell_map_) { - highest = (std::max)(highest, cell.second.get_row()); + highest = (std::max)(highest, cell.second.get_row() - 1); } return highest; } @@ -1180,9 +1190,10 @@ struct worksheet_struct return r; } - relationship create_relationship(const std::string &relationship_type) + relationship create_relationship(const std::string &relationship_type, const std::string &target_uri) { - relationships_.push_back(relationship(relationship_type)); + std::string r_id = "rId" + std::to_string(relationships_.size() + 1); + relationships_.push_back(relationship(relationship_type, r_id, target_uri)); return relationships_.back(); } @@ -1284,7 +1295,19 @@ struct worksheet_struct xlnt::range columns() { - throw std::runtime_error("not implemented"); + int max_row = get_highest_row(); + xlnt::range cols; + for(int col_idx = 0; col_idx < get_highest_column(); col_idx++) + { + cols.push_back(std::vector()); + std::string col = xlnt::cell::get_column_letter(col_idx + 1); + std::string range_string = col + "1:" + col + std::to_string(max_row + 1); + for(auto row : range(range_string, 0, 0)) + { + cols.back().push_back(row[0]); + } + } + return cols; } void operator=(const worksheet_struct &other) = delete; @@ -1405,9 +1428,9 @@ std::vector worksheet::get_relationships() return root_->relationships_; } -relationship worksheet::create_relationship(const std::string &relationship_type) +relationship worksheet::create_relationship(const std::string &relationship_type, const std::string &target_uri) { - return root_->create_relationship(relationship_type); + return root_->create_relationship(relationship_type, target_uri); } //void worksheet::add_chart(chart chart); diff --git a/source/xlnt.h b/source/xlnt.h index 7efa61b7..d8b1da78 100644 --- a/source/xlnt.h +++ b/source/xlnt.h @@ -405,11 +405,12 @@ public: theme }; - relationship(const std::string &type) : source_uri_(""), target_uri_("") + relationship(const std::string &type, const std::string &r_id = "", const std::string &target_uri = "") : id_(r_id), source_uri_(""), target_uri_(target_uri) { if(type == "hyperlink") { type_ = type::hyperlink; + target_mode_ = target_mode::external; } } @@ -775,7 +776,8 @@ public: date, formula, boolean, - error + error, + hyperlink }; static const std::unordered_map ErrorCodes; @@ -974,7 +976,7 @@ public: std::string calculate_dimension() const; xlnt::range range(const std::string &range_string); xlnt::range range(const std::string &range_string, int row_offset, int column_offset); - relationship create_relationship(const std::string &relationship_type); + relationship create_relationship(const std::string &relationship_type, const std::string &target_uri); //void add_chart(chart chart); void merge_cells(const std::string &range_string); void merge_cells(int start_row, int start_column, int end_row, int end_column);