Skip to content

Commit

Permalink
Support inline formatted strings
Browse files Browse the repository at this point in the history
Formatted strings are already supported as part of the strings table, so
this change handles them the same way when they are inline.
  • Loading branch information
nacnudus committed Dec 21, 2023
1 parent f36ade0 commit 1477038
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/xlsxcell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@ void xlsxcell::cacheValue(
) {
// 'v' for 'value' is either literal (numeric) or an index into a string table
rapidxml::xml_node<>* v = cell->first_node("v");
rapidxml::xml_node<>* is = cell->first_node("is");

std::string vvalue;
if (v != NULL) {
vvalue = v->value();
SET_STRING_ELT(book.content_, i, Rf_mkCharCE(vvalue.c_str(), CE_UTF8));
} else {
book.is_blank_[i] = true;
}

// 't' for 'type' defines the meaning of 'v' for value
Expand All @@ -100,15 +100,18 @@ void xlsxcell::cacheValue(

if (t != NULL && tvalue == "inlineStr") {
book.data_type_[i] = "character";
rapidxml::xml_node<>* is = cell->first_node("is");
if (is != NULL) { // Get the inline string if it's really there
// Parse it as though it's a simple string
std::string inlineString;
parseString(is, inlineString); // value is modified in place
// Also parse it as though it's a formatted string
SET_STRING_ELT(book.character_, i, Rf_mkCharCE(inlineString.c_str(), CE_UTF8));
book.character_formatted_[i] = parseFormattedString(is, book.styles_);
}
return;
} else if (v == NULL) {
// Can't now be an inline string (tested above)
book.is_blank_[i] = true;
book.data_type_[i] = "blank";
return;
} else if (t == NULL || tvalue == "n") {
Expand Down
Binary file added tests/testthat/inline-formatted-string.xlsx
Binary file not shown.
10 changes: 10 additions & 0 deletions tests/testthat/test-inline-strings.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ test_that("can read sheets with inlineStr", {
expect_equal(x[14], "RQ11610")
})

test_that("can read sheets with formatted inline strings", {
# Original source: http://our.componentone.com/wp-content/uploads/2011/12/TestExcel.xlsx
# These appear to come from LibreOffice 4.2.7.2.
cells <- xlsx_cells("inline-formatted-string.xlsx")
index <- 64
expect_equal(cells$is_blank[index], FALSE)
expect_equal(cells$data_type[index], "character")
expect_failure(expect_equal(cells$character_formatted[index], NULL))
})

test_that("does not crash on phonetic strings", {
# https://github.com/nacnudus/tidyxl/issues/30
expect_error(xlsx_cells("phonetic.xlsx"), NA)
Expand Down

0 comments on commit 1477038

Please sign in to comment.