Skip to content

Commit 4683e56

Browse files
authored
[wb_to_df] fix date1904 conversion (#737)
1 parent 3661327 commit 4683e56

File tree

5 files changed

+44
-7
lines changed

5 files changed

+44
-7
lines changed

NEWS.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Fixes
44

5+
* fix date1904 detection in `wb_to_df()`. Previous results from this somewhat rare file type were using a wrong timezone origin.
56
* corrections in vignettes
67
* fixes for loading workbooks with threaded comments
78
* fixes for loading workbooks with embeddings other than docx

R/read.R

+7-4
Original file line numberDiff line numberDiff line change
@@ -366,8 +366,11 @@ wb_to_df <- function(
366366
}
367367
}
368368

369+
origin <- get_date_origin(wb)
370+
369371
# dates
370372
if (!is.null(cc$c_s)) {
373+
371374
# if a cell is t="s" the content is a sst and not da date
372375
if (detect_dates && missing(types)) {
373376
cc$is_string <- FALSE
@@ -376,7 +379,7 @@ wb_to_df <- function(
376379

377380
if (any(sel <- cc$c_s %in% xlsx_date_style)) {
378381
sel <- sel & !cc$is_string & cc$v != ""
379-
cc$val[sel] <- suppressWarnings(as.character(convert_date(cc$v[sel])))
382+
cc$val[sel] <- suppressWarnings(as.character(convert_date(cc$v[sel], origin = origin)))
380383
cc$typ[sel] <- "d"
381384
}
382385

@@ -393,7 +396,7 @@ wb_to_df <- function(
393396

394397
if (any(sel <- cc$c_s %in% xlsx_posix_style)) {
395398
sel <- sel & !cc$is_string & cc$v != ""
396-
cc$val[sel] <- suppressWarnings(as.character(convert_datetime(cc$v[sel])))
399+
cc$val[sel] <- suppressWarnings(as.character(convert_datetime(cc$v[sel], origin = origin)))
397400
cc$typ[sel] <- "p"
398401
}
399402
}
@@ -570,8 +573,8 @@ wb_to_df <- function(
570573
# convert "#NUM!" to "NaN" -- then converts to NaN
571574
# maybe consider this an option to instead return NA?
572575
if (length(nums)) z[nums] <- lapply(z[nums], function(i) as.numeric(replace(i, i == "#NUM!", "NaN")))
573-
if (length(dtes)) z[dtes] <- lapply(z[dtes], date_conv)
574-
if (length(poxs)) z[poxs] <- lapply(z[poxs], datetime_conv)
576+
if (length(dtes)) z[dtes] <- lapply(z[dtes], date_conv, origin = origin)
577+
if (length(poxs)) z[poxs] <- lapply(z[poxs], datetime_conv, origin = origin)
575578
if (length(logs)) z[logs] <- lapply(z[logs], as.logical)
576579
if (isNamespaceLoaded("hms")) z[difs] <- lapply(z[difs], hms_conv)
577580
} else {

R/wb_load.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ wb_load <- function(
341341
}
342342

343343
workbookPr <- xml_node(workbook_xml, "workbook", "workbookPr")
344-
if (!data_only && length(workbookPr)) {
344+
if (length(workbookPr)) { # needed for date1904 detection
345345
wb$workbook$workbookPr <- workbookPr
346346
}
347347

tests/testthat/test-date_time_conversion.R

+26
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,29 @@ test_that("custom classes are treated independently", {
9898
expect_equal(exp, got)
9999

100100
})
101+
102+
test_that("date 1904 workbooks to df work", {
103+
104+
DATE <- as.Date("2015-02-07") + -10:10
105+
POSIX <- as.POSIXct("2022-03-02 19:27:35") + -10:10
106+
time <- data.frame(DATE, POSIX)
107+
108+
wb <- wb_workbook()
109+
wb$add_worksheet()$add_data(x = time)
110+
exp <- wb_to_df(wb)
111+
112+
wb <- wb_workbook()
113+
wb$workbook$workbookPr <- '<workbookPr date1904="true"/>'
114+
wb$add_worksheet()$add_data(x = time)
115+
got <- wb_to_df(wb)
116+
117+
expect_equal(exp, got)
118+
119+
wb <- wb_workbook()
120+
wb$workbook$workbookPr <- '<workbookPr date1904="1"/>'
121+
wb$add_worksheet()$add_data(x = time)
122+
got <- wb_to_df(wb)
123+
124+
expect_equal(exp, got)
125+
126+
})

tests/testthat/test-named_regions.R

+9-2
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,11 @@ test_that("Missing rows in named regions", {
145145

146146
## create region
147147
wb$add_data(sheet = 1, x = iris[1:11, ], startCol = 1, startRow = 1)
148-
delete_data(wb, sheet = 1, cols = 1:2, rows = c(6, 6))
148+
expect_warning(
149+
delete_data(wb, sheet = 1, cols = 1:2, rows = c(6, 6)),
150+
"'delete_data' is deprecated."
151+
)
152+
149153

150154
expect_warning(
151155
wb$add_named_region(
@@ -224,7 +228,10 @@ test_that("Missing columns in named regions", {
224228

225229
## create region
226230
wb$add_data(sheet = 1, x = iris[1:11, ], startCol = 1, startRow = 1)
227-
delete_data(wb, sheet = 1, cols = 2, rows = 1:12)
231+
expect_warning(
232+
delete_data(wb, sheet = 1, cols = 2, rows = 1:12),
233+
"'delete_data' is deprecated."
234+
)
228235

229236
wb$add_named_region(
230237
sheet = 1,

0 commit comments

Comments
 (0)