Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support hms writing #601

Merged
merged 9 commits into from
May 8, 2023
Merged

Support hms writing #601

merged 9 commits into from
May 8, 2023

Conversation

JanMarvin
Copy link
Owner

Currently this

library(openxlsx2)
hms_time <- hms::as_hms("12:13:14")
wb <- wb_workbook()$add_worksheet()$add_data(x = hms_time, colNames = FALSE)

leads to a corrupt xlsx file. This is because we treat hms_time as a simple numeric. Unfortunately there appears to be some under the hood conversion that results in us writing 12:13:14 into a field where the open xml standard allows only numerical values.

To avoid a dependency on hms (unfortunately there is no such function in R base), we return hms columns if hms is loaded and strings otherwise.

library(openxlsx2)

## with hms
library(hms)

x <- hms::as_hms("12:13:14")

wb <- wb_workbook()$add_worksheet()$add_data(x = x, colNames = FALSE)

z <- wb_to_df(wb, colNames = FALSE, convert = TRUE)
z
#>          A
#> 1 12:13:14
class(z$A)
#> [1] "hms"      "difftime"
attr(z, "tt")
#>   A
#> 1 h

## without hms
detach("package:hms", unload = TRUE)

x <- hms::as_hms("12:13:14")

wb <- wb_workbook()$add_worksheet()$add_data(x = x, colNames = FALSE)

z <- wb_to_df(wb, colNames = FALSE, convert = FALSE)
z
#>          A
#> 1 12:13:14
class(z$A)
#> [1] "character"
attr(z, "tt")
#>   A
#> 1 h

@JanMarvin JanMarvin merged commit 825dd5d into main May 8, 2023
9 checks passed
@JanMarvin JanMarvin deleted the difftime branch May 8, 2023 21:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant