don't use worksheet id as the sheet index since it is an arbitrary value

This commit is contained in:
Thomas Fussell 2016-07-26 21:57:35 -04:00
parent 3a9fda8bfe
commit 6707736fa8
2 changed files with 19 additions and 18 deletions

View File

@ -321,28 +321,26 @@ void excel_serializer::write_data(bool /*as_template*/)
void excel_serializer::write_worksheets() void excel_serializer::write_worksheets()
{ {
std::size_t index = 0; std::size_t index = 1;
for (auto ws : workbook_) for (const auto ws : workbook_)
{ {
for (auto relationship : workbook_.get_relationships()) auto target = "worksheets/sheet" + std::to_string(index++) + ".xml";
for (const auto &rel : workbook_.get_relationships())
{ {
if (relationship.get_type() == relationship::type::worksheet && if (rel.get_target_uri() != target) continue;
workbook::index_from_ws_filename(relationship.get_target_uri()) == index)
{
worksheet_serializer serializer_(ws);
std::string ws_filename = (relationship.get_target_uri().substr(0, 3) != "xl/" ? "xl/" : "") + relationship.get_target_uri();
std::ostringstream ss;
pugi::xml_document worksheet_xml;
serializer_.write_worksheet(worksheet_xml);
worksheet_xml.save(ss);
archive_.writestr(ws_filename, ss.str());
break; worksheet_serializer serializer_(ws);
} std::string ws_filename = (rel.get_target_uri().substr(0, 3) != "xl/" ? "xl/" : "") + rel.get_target_uri();
std::ostringstream ss;
pugi::xml_document worksheet_xml;
serializer_.write_worksheet(worksheet_xml);
worksheet_xml.save(ss);
archive_.writestr(ws_filename, ss.str());
break;
} }
index++;
} }
} }

View File

@ -277,10 +277,11 @@ void workbook_serializer::write_workbook(pugi::xml_document &xml) const
auto sheets_node = root_node.append_child("sheets"); auto sheets_node = root_node.append_child("sheets");
auto defined_names_node = root_node.append_child("definedNames"); auto defined_names_node = root_node.append_child("definedNames");
std::size_t index = 1;
for (const auto ws : workbook_) for (const auto ws : workbook_)
{ {
auto target = "worksheets/sheet" + std::to_string(ws.get_id()) + ".xml"; auto target = "worksheets/sheet" + std::to_string(index++) + ".xml";
for (const auto &rel : workbook_.get_relationships()) for (const auto &rel : workbook_.get_relationships())
{ {
@ -301,6 +302,8 @@ void workbook_serializer::write_workbook(pugi::xml_document &xml) const
"'" + ws.get_title() + "'!" + range_reference::make_absolute(ws.get_auto_filter()).to_string(); "'" + ws.get_title() + "'!" + range_reference::make_absolute(ws.get_auto_filter()).to_string();
defined_name_node.text().set(name.c_str()); defined_name_node.text().set(name.c_str());
} }
break;
} }
} }