Skip to content

Commit

Permalink
[load] read file with d3p1 namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
JanMarvin committed Dec 11, 2024
1 parent e949006 commit b8f2cd2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
23 changes: 22 additions & 1 deletion R/wb_load.R
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,13 @@ wb_load <- function(
wb <- wb_workbook()
wb$path <- file

grep_xml <- function(pattern, perl = TRUE, value = TRUE, ...) {
# There is one known file in #1194. this file has lower case folders, while
# the references in the file are the usual camel case.
needs_lower <- ifelse(any(grepl("\\[content_types\\].xml$", xmlFiles)), TRUE, FALSE)

grep_xml <- function(pattern, perl = TRUE, value = TRUE, to_lower = needs_lower, ...) {
# targets xmlFiles; has presents
if (to_lower) pattern <- tolower(pattern)
grep(pattern, xmlFiles, perl = perl, value = value, ...)
}

Expand Down Expand Up @@ -423,6 +428,22 @@ wb_load <- function(
sheets <- xml_attr(workbook_xml, "workbook", "sheets", "sheet")
sheets <- rbindlist(sheets)

if (any(grepl("d3p1", nams <- names(sheets)))) {
msg <- paste0(
"The `{%s}` namespace(s) has been removed from the xml files, for example:\n",
"\t<%s:ID> changed to:\n",
"\t<r:ID>\n",
"See 'Details' in ?openxlsx2::wb_load() for more information."
)
warning(sprintf(msg, "d3p1", "d3p1"))
nams <- stringi::stri_replace_all_fixed(
str = nams,
pattern = c("d3p1:", ":d3p1"),
replacement = c("r:", "")
)
names(sheets) <- nams
}

## Some veryHidden sheets do not have a sheet content and their rId is empty.
## Such sheets need to be filtered out because otherwise their sheet names
## occur in the list of all sheet names, leading to a wrong association
Expand Down
12 changes: 12 additions & 0 deletions tests/testthat/test-read_sources.R
Original file line number Diff line number Diff line change
Expand Up @@ -470,3 +470,15 @@ test_that("file extension handling works", {
expect_silent(wb_save(wb, file = tempfile(fileext = ".XLSM")))

})


test_that("loading d3p1 file works", {

fl <- testfile_path("gh_issue_1194.xlsx")
df <- wb_to_df(wb)

exp <- c(1347, 31)
got <- dim(df)
expect_equal(exp, got)

})

0 comments on commit b8f2cd2

Please sign in to comment.