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"); 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*/) void xlsx_producer::write_shared_string_table(const relationship & /*rel*/)
{ {
static const auto &xmlns = constants::ns("spreadsheetml"); static const auto &xmlns = constants::ns("spreadsheetml");
@ -854,79 +921,8 @@ void xlsx_producer::write_shared_string_table(const relationship & /*rel*/)
for (const auto &string : source_.shared_strings_by_id()) 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"); write_start_element(xmlns, "si");
write_rich_text(xmlns, string.second);
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_end_element(xmlns, "si"); write_end_element(xmlns, "si");
} }
@ -2603,8 +2599,7 @@ void xlsx_producer::write_worksheet(const relationship &rel)
case cell::type::inline_string: case cell::type::inline_string:
write_start_element(xmlns, "is"); write_start_element(xmlns, "is");
// TODO: make a write_rich_text method and use that here write_rich_text(xmlns, cell.value<xlnt::rich_text>());
write_element(xmlns, "t", cell.value<std::string>());
write_end_element(xmlns, "is"); write_end_element(xmlns, "is");
break; break;
@ -3067,65 +3062,11 @@ void xlsx_producer::write_comments(const relationship & /*rel*/, worksheet ws, c
write_attribute("ref", cell_ref.to_string()); write_attribute("ref", cell_ref.to_string());
auto author_id = authors.at(cell_comment.author()); auto author_id = authors.at(cell_comment.author());
write_attribute("authorId", author_id); write_attribute("authorId", author_id);
write_start_element(xmlns, "text"); write_start_element(xmlns, "text");
write_rich_text(xmlns, cell_comment.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_end_element(xmlns, "text"); write_end_element(xmlns, "text");
write_end_element(xmlns, "comment"); write_end_element(xmlns, "comment");
} }

View File

@ -45,6 +45,7 @@ class fill;
class font; class font;
class path; class path;
class relationship; class relationship;
class rich_text;
class streaming_workbook_writer; class streaming_workbook_writer;
class variant; class variant;
class workbook; class workbook;
@ -138,7 +139,7 @@ private:
/// we're trying to match. /// we're trying to match.
/// </summary> /// </summary>
std::string write_bool(bool boolean) const; std::string write_bool(bool boolean) const;
void write_relationships(const std::vector<xlnt::relationship> &relationships, const path &part); void write_relationships(const std::vector<xlnt::relationship> &relationships, const path &part);
void write_color(const xlnt::color &color); void write_color(const xlnt::color &color);
void write_border(const xlnt::border &b); void write_border(const xlnt::border &b);
@ -146,12 +147,13 @@ private:
void write_font(const xlnt::font &f); void write_font(const xlnt::font &f);
void write_table_styles(); void write_table_styles();
void write_colors(const std::vector<xlnt::color> &colors); 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> 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_start_element(ns, name);
write_characters(value); write_characters(value, preserve_whitespace);
write_end_element(ns, name); write_end_element(ns, name);
} }
@ -192,7 +194,7 @@ private:
/// A reference to the workbook which is the object of read/write operations. /// A reference to the workbook which is the object of read/write operations.
/// </summary> /// </summary>
const workbook &source_; const workbook &source_;
std::unique_ptr<ozstream> archive_; std::unique_ptr<ozstream> archive_;
std::unique_ptr<xml::serializer> current_part_serializer_; std::unique_ptr<xml::serializer> current_part_serializer_;
std::unique_ptr<std::streambuf> current_part_streambuf_; std::unique_ptr<std::streambuf> current_part_streambuf_;