Fix throwing exception when duplicate shared strings occur.

This commit is contained in:
胡剑波 2020-08-25 15:08:20 +08:00
parent 3c7122a78c
commit e8dd38d0d6
7 changed files with 24 additions and 26 deletions

View File

@ -760,11 +760,6 @@ public:
/// </summary>
std::size_t add_shared_string(const rich_text &shared, bool allow_duplicates = false);
/// <summary>
/// Returns a reference to the shared string ordered by id
/// </summary>
const std::map<std::size_t, rich_text> &shared_strings_by_id() const;
/// <summary>
/// Returns a reference to the shared string related to the specified index
/// </summary>
@ -774,13 +769,13 @@ public:
/// Returns a reference to the shared strings being used by cells
/// in this workbook.
/// </summary>
std::unordered_map<rich_text, std::size_t, rich_text_hash> &shared_strings();
std::vector<rich_text> &shared_strings();
/// <summary>
/// Returns a reference to the shared strings being used by cells
/// in this workbook.
/// </summary>
const std::unordered_map<rich_text, std::size_t, rich_text_hash> &shared_strings() const;
const std::vector<rich_text> &shared_strings() const;
// Thumbnail

View File

@ -118,7 +118,7 @@ struct workbook_impl
std::list<worksheet_impl> worksheets_;
std::unordered_map<rich_text, std::size_t, rich_text_hash> shared_strings_ids_;
std::map<std::size_t, rich_text> shared_strings_values_;
std::vector<rich_text> shared_strings_values_;
optional<stylesheet> stylesheet_;

View File

@ -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"));
}

View File

@ -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");
}

View File

@ -1348,32 +1348,25 @@ const manifest &workbook::manifest() const
return d_->manifest_;
}
const std::map<std::size_t, rich_text> &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<rich_text, std::size_t, rich_text_hash> &workbook::shared_strings()
std::vector<rich_text> &workbook::shared_strings()
{
return d_->shared_strings_ids_;
return d_->shared_strings_values_;
}
const std::unordered_map<rich_text, std::size_t, rich_text_hash> &workbook::shared_strings() const
const std::vector<rich_text> &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;
}

Binary file not shown.

View File

@ -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;