Skip to content

Commit

Permalink
PR #505: accept .xlsx that lack a "styles" part (#528)
Browse files Browse the repository at this point in the history
* Accept XLSX files that do not contain a "styles" part; fixes #506

An XLSX file that does not contain a "styles" part triggers this error:

  > read_excel("foo.xlsx", sheet="Sheet1")
  Error in sheets_fun(path) :
    Evaluation error: Couldn't find '' in 'foo.xlsx'.

To fix this, check for the presence of the part in cacheDateFormats() in
the same way that cacheStringTable() does.

* Rename test sheet; code style

* Beef up the test a bit

* Add NEWS bullet
  • Loading branch information
jennybc authored Dec 12, 2018
1 parent bd1160f commit e9d324f
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 0 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

## Other changes

* xlsx files that do not have a "Styles" part can now be read (#505, #506 @jt6)

* Path handling:

- All paths are passed through `normalizePath()` (#498, #499, new behaviour for xlsx but not xls) and `enc2native()` (#370).
Expand Down
4 changes: 4 additions & 0 deletions src/XlsxWorkBook.h
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,10 @@ class XlsxWorkBook {
}

void cacheDateFormats() {
if (!zip_has_file(path_, rel_.part("styles"))) {
return;
}

std::string stylesXml = zip_buffer(path_, rel_.part("styles"));
rapidxml::xml_document<> styles;
styles.parse<rapidxml::parse_strip_xml_namespaces>(&stylesXml[0]);
Expand Down
Binary file not shown.
8 changes: 8 additions & 0 deletions tests/testthat/test-read-excel.R
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,11 @@ test_that("non-ASCII filenames can be read", {
expect_error_free(read_xls(tricky_xls_file))
expect_error_free(read_xlsx(tricky_xlsx_file))
})

test_that("styles and sharedStrings parts can be absent", {
expect_error_free(
df <- read_xlsx(test_sheet("no-styles-or-sharedStrings-parts.xlsx"))
)
expect_identical(df$Language[1], "german")
expect_true(all(df$Age > 0))
})

0 comments on commit e9d324f

Please sign in to comment.