Added write_rich_text method to xlsx_producer

This eliminates some duplicate code as reported in #165
This commit is contained in:
Kostas Dizas 2018-09-11 14:06:18 +01:00
parent 10c5781e6d
commit 61639c7275
2 changed files with 78 additions and 135 deletions

View File

@ -813,6 +813,73 @@ void xlsx_producer::write_pivot_table(const relationship & /*rel*/)
write_end_element(constants::ns("spreadsheetml"), "pivotTableDefinition");
}
void xlsx_producer::write_rich_text(const std::string &ns, const xlnt::rich_text &text)
{
if (text.runs().size() == 1 && !text.runs().at(0).second.is_set())
{
write_start_element(ns, "t");
write_characters(text.plain_text(), text.runs().front().preserve_space);
write_end_element(ns, "t");
return;
}
for (const auto &run : text.runs())
{
write_start_element(ns, "r");
if (run.second.is_set())
{
write_start_element(ns, "rPr");
if (run.second.get().bold())
{
write_start_element(ns, "b");
write_end_element(ns, "b");
}
if (run.second.get().has_size())
{
write_start_element(ns, "sz");
write_attribute("val", run.second.get().size());
write_end_element(ns, "sz");
}
if (run.second.get().has_color())
{
write_start_element(ns, "color");
write_color(run.second.get().color());
write_end_element(ns, "color");
}
if (run.second.get().has_name())
{
write_start_element(ns, "rFont");
write_attribute("val", run.second.get().name());
write_end_element(ns, "rFont");
}
if (run.second.get().has_family())
{
write_start_element(ns, "family");
write_attribute("val", run.second.get().family());
write_end_element(ns, "family");
}
if (run.second.get().has_scheme())
{
write_start_element(ns, "scheme");
write_attribute("val", run.second.get().scheme());
write_end_element(ns, "scheme");
}
write_end_element(ns, "rPr");
}
write_element(ns, "t", run.first, run.preserve_space);
write_end_element(ns, "r");
}
}
void xlsx_producer::write_shared_string_table(const relationship & /*rel*/)
{
static const auto &xmlns = constants::ns("spreadsheetml");
@ -853,80 +920,9 @@ void xlsx_producer::write_shared_string_table(const relationship & /*rel*/)
write_attribute("uniqueCount", source_.shared_strings_by_id().size());
for (const auto &string : source_.shared_strings_by_id())
{
if (string.second.runs().size() == 1 && !string.second.runs().at(0).second.is_set())
{
write_start_element(xmlns, "si");
write_start_element(xmlns, "t");
write_characters(string.second.plain_text(), string.second.runs().front().preserve_space);
write_end_element(xmlns, "t");
write_end_element(xmlns, "si");
continue;
}
write_start_element(xmlns, "si");
for (const auto &run : string.second.runs())
{
write_start_element(xmlns, "r");
if (run.second.is_set())
{
write_start_element(xmlns, "rPr");
if (run.second.get().bold())
{
write_start_element(xmlns, "b");
write_end_element(xmlns, "b");
}
if (run.second.get().has_size())
{
write_start_element(xmlns, "sz");
write_attribute("val", run.second.get().size());
write_end_element(xmlns, "sz");
}
if (run.second.get().has_color())
{
write_start_element(xmlns, "color");
write_color(run.second.get().color());
write_end_element(xmlns, "color");
}
if (run.second.get().has_name())
{
write_start_element(xmlns, "rFont");
write_attribute("val", run.second.get().name());
write_end_element(xmlns, "rFont");
}
if (run.second.get().has_family())
{
write_start_element(xmlns, "family");
write_attribute("val", run.second.get().family());
write_end_element(xmlns, "family");
}
if (run.second.get().has_scheme())
{
write_start_element(xmlns, "scheme");
write_attribute("val", run.second.get().scheme());
write_end_element(xmlns, "scheme");
}
write_end_element(xmlns, "rPr");
}
write_start_element(xmlns, "t");
write_characters(run.first, run.preserve_space);
write_end_element(xmlns, "t");
write_end_element(xmlns, "r");
}
write_rich_text(xmlns, string.second);
write_end_element(xmlns, "si");
}
@ -2603,8 +2599,7 @@ void xlsx_producer::write_worksheet(const relationship &rel)
case cell::type::inline_string:
write_start_element(xmlns, "is");
// TODO: make a write_rich_text method and use that here
write_element(xmlns, "t", cell.value<std::string>());
write_rich_text(xmlns, cell.value<xlnt::rich_text>());
write_end_element(xmlns, "is");
break;
@ -3067,65 +3062,11 @@ void xlsx_producer::write_comments(const relationship & /*rel*/, worksheet ws, c
write_attribute("ref", cell_ref.to_string());
auto author_id = authors.at(cell_comment.author());
write_attribute("authorId", author_id);
write_start_element(xmlns, "text");
for (const auto &run : cell_comment.text().runs())
{
write_start_element(xmlns, "r");
if (run.second.is_set())
{
write_start_element(xmlns, "rPr");
if (run.second.get().bold())
{
write_start_element(xmlns, "b");
write_end_element(xmlns, "b");
}
if (run.second.get().has_size())
{
write_start_element(xmlns, "sz");
write_attribute("val", run.second.get().size());
write_end_element(xmlns, "sz");
}
if (run.second.get().has_color())
{
write_start_element(xmlns, "color");
write_color(run.second.get().color());
write_end_element(xmlns, "color");
}
if (run.second.get().has_name())
{
write_start_element(xmlns, "rFont");
write_attribute("val", run.second.get().name());
write_end_element(xmlns, "rFont");
}
if (run.second.get().has_family())
{
write_start_element(xmlns, "family");
write_attribute("val", run.second.get().family());
write_end_element(xmlns, "family");
}
if (run.second.get().has_scheme())
{
write_start_element(xmlns, "scheme");
write_attribute("val", run.second.get().scheme());
write_end_element(xmlns, "scheme");
}
write_end_element(xmlns, "rPr");
}
write_element(xmlns, "t", run.first);
write_end_element(xmlns, "r");
}
write_rich_text(xmlns, cell_comment.text());
write_end_element(xmlns, "text");
write_end_element(xmlns, "comment");
}

View File

@ -45,6 +45,7 @@ class fill;
class font;
class path;
class relationship;
class rich_text;
class streaming_workbook_writer;
class variant;
class workbook;
@ -146,12 +147,13 @@ private:
void write_font(const xlnt::font &f);
void write_table_styles();
void write_colors(const std::vector<xlnt::color> &colors);
void write_rich_text(const std::string &ns, const xlnt::rich_text &text);
template<typename T>
void write_element(const std::string &ns, const std::string &name, T value)
void write_element(const std::string &ns, const std::string &name, T value, bool preserve_whitespace = false)
{
write_start_element(ns, name);
write_characters(value);
write_characters(value, preserve_whitespace);
write_end_element(ns, name);
}