Skip to content

Commit

Permalink
Fixes issue #18 by making some attributes related to definedNames opt…
Browse files Browse the repository at this point in the history
…ional, as specified by ECMA-376.
  • Loading branch information
Laurențiu Leahu-Vlăducu committed Aug 29, 2024
1 parent 5055ba0 commit 04590c3
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
4 changes: 2 additions & 2 deletions source/detail/serialization/defined_name.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ namespace detail {
struct defined_name
{
std::string name;
std::size_t sheet_id;
bool hidden;
optional<std::size_t> sheet_id;
bool hidden = false;
std::string value;
};

Expand Down
6 changes: 4 additions & 2 deletions source/detail/serialization/xlsx_consumer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2083,8 +2083,10 @@ void xlsx_consumer::read_office_document(const std::string &content_type) // CT_

defined_name name;
name.name = parser().attribute("name");
name.sheet_id = parser().attribute<std::size_t>("localSheetId");
name.hidden = false;
if (parser().attribute_present("localSheetId"))
{
name.sheet_id = parser().attribute<std::size_t>("localSheetId");
}
if (parser().attribute_present("hidden"))
{
name.hidden = is_true(parser().attribute("hidden"));
Expand Down
8 changes: 6 additions & 2 deletions source/detail/serialization/xlsx_producer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -660,15 +660,19 @@ void xlsx_producer::write_workbook(const relationship &rel)
if (!defined_names.empty())
{
write_start_element(xmlns, "definedNames");
for (auto name : defined_names)
for (const auto & name : defined_names)
{
write_start_element(xmlns, "definedName");
write_attribute("name", name.name);
if (name.hidden)
{
write_attribute("hidden", write_bool(true));
}
write_attribute("localSheetId", std::to_string(name.sheet_id - 1)); // 0-indexed for some reason

if (name.sheet_id.is_set())
{
write_attribute("localSheetId", std::to_string(name.sheet_id.get() - 1)); // 0-indexed for some reason
}
write_characters(name.value);
write_end_element(xmlns, "definedName");
}
Expand Down

0 comments on commit 04590c3

Please sign in to comment.