Skip to content

Commit

Permalink
Fixes ycphs#72
Browse files Browse the repository at this point in the history
  • Loading branch information
deschen1 committed Jun 17, 2022
1 parent 3ceeb84 commit beae41b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# openxlsx (development version)

* Added optional argument to `loadWorkbook` to decide if empty/blank cells should be converted to NA_character_ (the default) or left blank as is

* Add type blanks and not blanks to conditional formatting ([#311](https://github.com/ycphs/openxlsx/pull/311))

# openxlsx 4.2.5
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 beae41b

Please sign in to comment.