Skip to content

Commit

Permalink
Fixes #72
Browse files Browse the repository at this point in the history
  • Loading branch information
deschen1 authored and JanMarvin committed Aug 11, 2024
1 parent 4c35f10 commit a76db4b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# openxlsx (development version)

* Fixed warning on `dataValidation(..., type = "list")` ([#342](https://github.com/ycphs/openxlsx/issues/342))
* Added optional argument to `loadWorkbook` to decide if empty/blank cells should be converted to NA_character_ (the default) or left blank as is

## Improvements

Expand Down
7 changes: 5 additions & 2 deletions R/loadWorkbook.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#' @param file A path to an existing .xlsx or .xlsm file
#' @param xlsxFile alias for file
#' @param isUnzipped Set to TRUE if the xlsx file is already unzipped
#' @param na.convert Should empty/blank cells be converted to `NA_character_`. Defaults to TRUE.
#' @description loadWorkbook returns a workbook object conserving styles and
#' formatting of the original .xlsx file.
#' @return Workbook object.
Expand All @@ -25,7 +26,7 @@
#' saveWorkbook(wb, "loadExample.xlsx", overwrite = TRUE)
#' }
#'
loadWorkbook <- function(file, xlsxFile = NULL, isUnzipped = FALSE) {
loadWorkbook <- function(file, xlsxFile = NULL, isUnzipped = FALSE, na.convert = TRUE) {

## If this is a unzipped workbook, skip the temp dir stuff
if (isUnzipped) {
Expand Down Expand Up @@ -264,7 +265,9 @@ loadWorkbook <- function(file, xlsxFile = NULL, isUnzipped = FALSE) {
vals <- getNodes(xml = sharedStrings, tagIn = "<si>")

if ("<si><t/></si>" %in% vals) {
vals[vals == "<si><t/></si>"] <- "<si><t>NA</t></si>"
if (na.convert == TRUE){
vals[vals == "<si><t/></si>"] <- "<si><t>NA</t></si>"
}
Encoding(vals) <- "UTF-8"
attr(vals, "uniqueCount") <- uniqueCount - 1L
} else {
Expand Down

0 comments on commit a76db4b

Please sign in to comment.