diff --git a/NEWS.md b/NEWS.md index 3c73d89a..aecceff4 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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). diff --git a/src/XlsxWorkBook.h b/src/XlsxWorkBook.h index df357b85..ac06edcd 100644 --- a/src/XlsxWorkBook.h +++ b/src/XlsxWorkBook.h @@ -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(&stylesXml[0]); diff --git a/tests/testthat/sheets/no-styles-or-sharedStrings-parts.xlsx b/tests/testthat/sheets/no-styles-or-sharedStrings-parts.xlsx new file mode 100644 index 00000000..54392384 Binary files /dev/null and b/tests/testthat/sheets/no-styles-or-sharedStrings-parts.xlsx differ diff --git a/tests/testthat/test-read-excel.R b/tests/testthat/test-read-excel.R index fefbd8be..820a82d0 100644 --- a/tests/testthat/test-read-excel.R +++ b/tests/testthat/test-read-excel.R @@ -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)) + })