diff --git a/include/xlnt/workbook/workbook.hpp b/include/xlnt/workbook/workbook.hpp index 00ad3710..275363fb 100644 --- a/include/xlnt/workbook/workbook.hpp +++ b/include/xlnt/workbook/workbook.hpp @@ -760,11 +760,6 @@ public: /// std::size_t add_shared_string(const rich_text &shared, bool allow_duplicates = false); - /// - /// Returns a reference to the shared string ordered by id - /// - const std::map &shared_strings_by_id() const; - /// /// Returns a reference to the shared string related to the specified index /// @@ -774,13 +769,13 @@ public: /// Returns a reference to the shared strings being used by cells /// in this workbook. /// - std::unordered_map &shared_strings(); + std::vector &shared_strings(); /// /// Returns a reference to the shared strings being used by cells /// in this workbook. /// - const std::unordered_map &shared_strings() const; + const std::vector &shared_strings() const; // Thumbnail diff --git a/source/detail/implementations/workbook_impl.hpp b/source/detail/implementations/workbook_impl.hpp index 710e13e6..76308872 100644 --- a/source/detail/implementations/workbook_impl.hpp +++ b/source/detail/implementations/workbook_impl.hpp @@ -118,7 +118,7 @@ struct workbook_impl std::list worksheets_; std::unordered_map shared_strings_ids_; - std::map shared_strings_values_; + std::vector shared_strings_values_; optional stylesheet_; diff --git a/source/detail/serialization/xlsx_consumer.cpp b/source/detail/serialization/xlsx_consumer.cpp index 62d9c418..ede14048 100644 --- a/source/detail/serialization/xlsx_consumer.cpp +++ b/source/detail/serialization/xlsx_consumer.cpp @@ -2088,7 +2088,7 @@ void xlsx_consumer::read_shared_string_table() { expect_start_element(qn("spreadsheetml", "si"), xml::content::complex); auto rt = read_rich_text(qn("spreadsheetml", "si")); - target_.add_shared_string(rt); + target_.add_shared_string(rt, true); expect_end_element(qn("spreadsheetml", "si")); } diff --git a/source/detail/serialization/xlsx_producer.cpp b/source/detail/serialization/xlsx_producer.cpp index e6683a31..db8279af 100644 --- a/source/detail/serialization/xlsx_producer.cpp +++ b/source/detail/serialization/xlsx_producer.cpp @@ -944,12 +944,12 @@ void xlsx_producer::write_shared_string_table(const relationship & /*rel*/) } write_attribute("count", string_count); - write_attribute("uniqueCount", source_.shared_strings_by_id().size()); + write_attribute("uniqueCount", source_.shared_strings().size()); - for (const auto &string : source_.shared_strings_by_id()) + for (const auto &text : source_.shared_strings()) { write_start_element(xmlns, "si"); - write_rich_text(xmlns, string.second); + write_rich_text(xmlns, text); write_end_element(xmlns, "si"); } diff --git a/source/workbook/workbook.cpp b/source/workbook/workbook.cpp index 32217224..11c7afa0 100644 --- a/source/workbook/workbook.cpp +++ b/source/workbook/workbook.cpp @@ -1348,32 +1348,25 @@ const manifest &workbook::manifest() const return d_->manifest_; } -const std::map &workbook::shared_strings_by_id() const -{ - return d_->shared_strings_values_; -} - const rich_text &workbook::shared_strings(std::size_t index) const { - auto it = d_->shared_strings_values_.find(index); - - if (it != d_->shared_strings_values_.end()) + if (index < d_->shared_strings_values_.size()) { - return it->second; + return d_->shared_strings_values_.at(index); } static rich_text empty; return empty; } -std::unordered_map &workbook::shared_strings() +std::vector &workbook::shared_strings() { - return d_->shared_strings_ids_; + return d_->shared_strings_values_; } -const std::unordered_map &workbook::shared_strings() const +const std::vector &workbook::shared_strings() const { - return d_->shared_strings_ids_; + return d_->shared_strings_values_; } std::size_t workbook::add_shared_string(const rich_text &shared, bool allow_duplicates) @@ -1392,7 +1385,7 @@ std::size_t workbook::add_shared_string(const rich_text &shared, bool allow_dupl auto sz = d_->shared_strings_ids_.size(); d_->shared_strings_ids_[shared] = sz; - d_->shared_strings_values_[sz] = shared; + d_->shared_strings_values_.push_back(shared); return sz; } diff --git a/tests/data/Issue494_shared_string.xlsx b/tests/data/Issue494_shared_string.xlsx new file mode 100755 index 00000000..87d8a17d Binary files /dev/null and b/tests/data/Issue494_shared_string.xlsx differ diff --git a/tests/workbook/workbook_test_suite.cpp b/tests/workbook/workbook_test_suite.cpp index 1c3ab1b5..be17357e 100644 --- a/tests/workbook/workbook_test_suite.cpp +++ b/tests/workbook/workbook_test_suite.cpp @@ -68,6 +68,7 @@ public: register_test(test_load_file); register_test(test_Issue279); register_test(test_Issue353); + register_test(test_Issue494); } void test_active_sheet() @@ -509,5 +510,14 @@ public: xlnt_assert_equals(ws.row_properties(1).spans.get(), "1:8"); xlnt_assert_equals(ws.row_properties(17).spans.get(), "2:7"); } + + void test_Issue494() + { + xlnt::workbook wb; + wb.load(path_helper::test_file("Issue494_shared_string.xlsx")); + auto ws = wb.active_sheet(); + xlnt_assert_equals(ws.cell(2, 1).to_string(), "V1.00"); + xlnt_assert_equals(ws.cell(2, 2).to_string(), "V1.00"); + } }; static workbook_test_suite x;