mirror of
https://github.com/tfussell/xlnt.git
synced 2024-03-22 13:11:17 +08:00
add support for one cell with different formatted text nodes.
Eg, In one cell the text is abcdef, where abc and def have different format (e.g., different colours or fonts). The the sharedString.xml in xlsx have multiple r node in si node: ```xml <si> <r> <t>abc</t> </r> <r> <rPr><sz val="10"/><color rgb="FFFF0000"/><rFont val="微软雅黑"/><charset val="134"/></rPr> <t>def</t> </r> </si> ``` Currently `shared_strings_serializer::read_shared_strings()` only reads the first r node, and should consider as a bug. This commit fixes this bug by read all `r.t.text` values in one `si` node and concat the text into one single string.
This commit is contained in:
parent
720edc143f
commit
c18110e282
|
@ -68,9 +68,18 @@ bool shared_strings_serializer::read_shared_strings(const xml_document &xml, std
|
||||||
{
|
{
|
||||||
strings.push_back(si_node.get_child("t").get_text());
|
strings.push_back(si_node.get_child("t").get_text());
|
||||||
}
|
}
|
||||||
else if (si_node.has_child("r"))
|
else if (si_node.has_child("r")) // possible multiple text entities.
|
||||||
{
|
{
|
||||||
strings.push_back(si_node.get_child("r").get_child("t").get_text());
|
std::string text;
|
||||||
|
for (const auto& r_node : si_node.get_children())
|
||||||
|
{
|
||||||
|
if (r_node.get_name() == "r" && r_node.has_child("t"))
|
||||||
|
{
|
||||||
|
std::cout << r_node.get_child("t").get_text() << std::endl;
|
||||||
|
text += r_node.get_child("t").get_text();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
strings.push_back(std::move(text));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -184,6 +184,20 @@ public:
|
||||||
TS_ASSERT_EQUALS(val, "Donald");
|
TS_ASSERT_EQUALS(val, "Donald");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void test_read_shared_strings_multiple_r_nodes()
|
||||||
|
{
|
||||||
|
auto path = PathHelper::GetDataDirectory("/reader/shared_strings-multiple_r_nodes.xlsx");
|
||||||
|
|
||||||
|
xlnt::workbook wb;
|
||||||
|
xlnt::excel_serializer serializer(wb);
|
||||||
|
|
||||||
|
serializer.load_workbook(path);
|
||||||
|
|
||||||
|
auto ws = wb["Sheet1"];
|
||||||
|
auto val = ws.get_cell("A1").get_value<std::string>();
|
||||||
|
TS_ASSERT_EQUALS(val, "abcdef");
|
||||||
|
}
|
||||||
|
|
||||||
xlnt::workbook date_mac_1904()
|
xlnt::workbook date_mac_1904()
|
||||||
{
|
{
|
||||||
auto path = PathHelper::GetDataDirectory("/reader/date_1904.xlsx");
|
auto path = PathHelper::GetDataDirectory("/reader/date_1904.xlsx");
|
||||||
|
|
BIN
tests/data/reader/shared_strings-multiple_r_nodes.xlsx
Normal file
BIN
tests/data/reader/shared_strings-multiple_r_nodes.xlsx
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user