", next_relship)
- }
-
- invisible(self)
+ wb_add_drawing_impl(
+ self = self,
+ private = private,
+ sheet = sheet,
+ xml = xml,
+ dims = dims
+ )
},
#' @description Add xml drawing
@@ -3717,43 +1157,15 @@ wbWorkbook <- R6::R6Class(
add_chart_xml = function(
sheet = current_sheet(),
xml,
- dims = "A1:H8"
+ dims = "A1:H8"
) {
-
- dims_list <- strsplit(dims, ":")[[1]]
- cols <- col2int(dims_list)
- rows <- as.numeric(gsub("\\D+", "", dims_list))
-
- sheet <- private$get_sheet_index(sheet)
-
- next_chart <- NROW(self$charts) + 1
-
- chart <- data.frame(
- chart = xml,
- colors = colors1_xml,
- style = styleplot_xml,
- rels = chart1_rels_xml(next_chart),
- chartEx = "",
- relsEx = ""
- )
-
- self$charts <- rbind(self$charts, chart)
-
- len_drawing <- length(xml_node_name(self$drawings[[sheet]], "xdr:wsDr")) + 1L
-
- from <- c(cols[1] - 1L, rows[1] - 1L)
- to <- c(cols[2], rows[2])
-
- # create drawing. add it to self$drawings, the worksheet and rels
- self$add_drawing(
- sheet = sheet,
- xml = drawings(len_drawing, from, to),
- dims = dims
+ wb_add_chart_xml_impl(
+ self = self,
+ private = private,
+ sheet = sheet,
+ xml = xml,
+ dims = dims
)
-
- self$drawings_rels[[sheet]] <- drawings_rels(self$drawings_rels[[sheet]], next_chart)
-
- invisible(self)
},
#' @description Add mschart chart to the workbook
@@ -3766,68 +1178,25 @@ wbWorkbook <- R6::R6Class(
dims = "B2:H8",
graph
) {
-
- requireNamespace("mschart")
- assert_class(graph, "ms_chart")
-
- sheetname <- private$get_sheet_name(sheet)
-
- # format.ms_chart is exported in mschart >= 0.4
- out_xml <- read_xml(
- format(
- graph,
- sheetname = sheetname,
- id_x = "64451212",
- id_y = "64453248"
- ),
- pointer = FALSE
+ wb_add_mschart_impl(
+ self = self,
+ private = private,
+ sheet = sheet,
+ dims = dims,
+ graph = graph
)
-
- # write the chart data to the workbook
- if (inherits(graph$data_series, "wb_data")) {
- self$
- add_chart_xml(sheet = sheet, xml = out_xml, dims = dims)
- } else {
- self$
- add_data(x = graph$data_series)$
- add_chart_xml(sheet = sheet, xml = out_xml, dims = dims)
- }
},
+ ### methods ----
+
#' @description
#' Prints the `wbWorkbook` object
#' @return The `wbWorkbook` object, invisibly; called for its side-effects
print = function() {
- exSheets <- self$get_sheet_names()
- nSheets <- length(exSheets)
- nImages <- length(self$media)
- nCharts <- length(self$charts)
-
- showText <- "A Workbook object.\n"
-
- ## worksheets
- if (nSheets > 0) {
- showText <- c(showText, "\nWorksheets:\n")
- sheetTxt <- sprintf("Sheets: %s", paste(names(exSheets), collapse = " "))
-
- showText <- c(showText, sheetTxt, "\n")
- } else {
- showText <-
- c(showText, "\nWorksheets:\n", "No worksheets attached\n")
- }
-
- if (nSheets > 0) {
- showText <-
- c(showText, sprintf(
- "Write order: %s",
- stri_join(self$sheetOrder, sep = " ", collapse = ", ")
- ))
- }
-
- cat(unlist(showText))
- invisible(self)
+ wb_print_impl(self, private)
},
+ ### protect ---
#' @description
#' Protect a workbook
@@ -3850,41 +1219,18 @@ wbWorkbook <- R6::R6Class(
username = unname(Sys.info()["user"]),
readOnlyRecommended = FALSE
) {
-
- if (!protect) {
- self$workbook$workbookProtection <- NULL
- return(self)
- }
-
- # match.arg() doesn't handle numbers too well
- type <- if (!is.character(type)) as.character(type)
- password <- if (is.null(password)) "" else hashPassword(password)
-
- # TODO: Shall we parse the existing protection settings and preserve all
- # unchanged attributes?
-
- if (fileSharing) {
- self$workbook$fileSharing <- xml_node_create(
- "fileSharing",
- xml_attributes = c(
- userName = username,
- readOnlyRecommended = if (readOnlyRecommended | type == "2") "1",
- reservationPassword = password
- )
- )
- }
-
- self$workbook$workbookProtection <- xml_node_create(
- "workbookProtection",
- xml_attributes = c(
- hashPassword = password,
- lockStructure = toString(as.numeric(lockStructure)),
- lockWindows = toString(as.numeric(lockWindows))
- )
+ wb_protect_impl(
+ self = self,
+ private = private,
+ protect = protect,
+ password = password,
+ lockStructure = lockStructure,
+ lockWindows = lockWindows,
+ type = type,
+ fileSharing = fileSharing,
+ username = username,
+ readOnlyRecommended = readOnlyRecommended
)
-
- self$workbook$apps <- xml_node_create("DocSecurity", type)
- invisible(self)
},
#' @description protect worksheet
@@ -3899,91 +1245,56 @@ wbWorkbook <- R6::R6Class(
#' `"autoFilter"`, `"pivotTables"`, `"objects"`, `"scenarios"`
#' @returns The `wbWorkbook` object
protect_worksheet = function(
- sheet = current_sheet(),
- protect = TRUE,
- password = NULL,
- properties = NULL
+ sheet = current_sheet(),
+ protect = TRUE,
+ password = NULL,
+ properties = NULL
) {
-
- sheet <- wb_validate_sheet(self, sheet)
-
- if (!protect) {
- # initializes as character()
- self$worksheets[[sheet]]$sheetProtection <- character()
- return(self)
- }
-
- all_props <- worksheet_lock_properties()
-
- if (!is.null(properties)) {
- # ensure only valid properties are listed
- properties <- match.arg(properties, all_props, several.ok = TRUE)
- }
-
- properties <- as.character(as.numeric(all_props %in% properties))
- names(properties) <- all_props
-
- if (!is.null(password))
- properties <- c(properties, password = hashPassword(password))
-
- self$worksheets[[sheet]]$sheetProtection <- xml_node_create(
- "sheetProtection",
- xml_attributes = c(
- sheet = "1",
- properties[properties != "0"]
- )
+ wb_protect_worksheet_impl(
+ self = self,
+ private = private,
+ sheet = sheet,
+ protect = protect,
+ password = password,
+ properties = properties
)
-
- invisible(self)
},
-
### creators --------------------------------------------------------------
#' @description Set creator(s)
#' @param creators A character vector of creators to set. Duplicates are
#' ignored.
set_creators = function(creators) {
- private$modify_creators("set", creators)
+ wb_modify_creators_impl(self, private, "set", creators)
},
-
#' @description Add creator(s)
#' @param creators A character vector of creators to add. Duplicates are
#' ignored.
add_creators = function(creators) {
- private$modify_creators("add", creators)
+ wb_modify_creators_impl(self, private, "add", creators)
},
-
#' @description Remove creator(s)
#' @param creators A character vector of creators to remove. All duplicated
#' are removed.
remove_creators = function(creators) {
- private$modify_creators("remove", creators)
+ wb_modify_creators_impl(self, private, "remove", creators)
},
+ ### last modified by ----
#' @description
#' Change the last modified by
#' @param LastModifiedBy A new value
#' @return The `wbWorkbook` object, invisibly
set_last_modified_by = function(LastModifiedBy = NULL) {
- # TODO rename to wb_set_last_modified_by() ?
- if (!is.null(LastModifiedBy)) {
- current_LastModifiedBy <-
- stri_match(self$core, regex = "(.*?)")[1, 2]
- self$core <-
- stri_replace_all_fixed(
- self$core,
- pattern = current_LastModifiedBy,
- replacement = LastModifiedBy
- )
- }
-
- invisible(self)
+ wb_set_last_modified_by_impl(self, private, LastModifiedBy)
},
+ ### page setup ----
+
#' @description page_setup()
#' @param sheet sheet
#' @param orientation orientation
@@ -4020,138 +1331,29 @@ wbWorkbook <- R6::R6Class(
summaryRow = NULL,
summaryCol = NULL
) {
- sheet <- private$get_sheet_index(sheet)
- xml <- self$worksheets[[sheet]]$pageSetup
-
- if (!is.null(orientation)) {
- orientation <- tolower(orientation)
- if (!orientation %in% c("portrait", "landscape")) stop("Invalid page orientation.")
- } else {
- # if length(xml) == 1 then use if () {} else {}
- orientation <- ifelse(grepl("landscape", xml), "landscape", "portrait") ## get existing
- }
-
- if ((scale < 10) || (scale > 400)) {
- stop("Scale must be between 10 and 400.")
- }
-
- if (!is.null(paperSize)) {
- paperSizes <- 1:68
- paperSizes <- paperSizes[!paperSizes %in% 48:49]
- if (!paperSize %in% paperSizes) {
- stop("paperSize must be an integer in range [1, 68]. See ?ws_page_setup details.")
- }
- paperSize <- as.integer(paperSize)
- } else {
- paperSize <- regmatches(xml, regexpr('(?<=paperSize=")[0-9]+', xml, perl = TRUE)) ## get existing
- }
-
- ## Keep defaults on orientation, hdpi, vdpi, paperSize ----
- hdpi <- regmatches(xml, regexpr('(?<=horizontalDpi=")[0-9]+', xml, perl = TRUE))
- vdpi <- regmatches(xml, regexpr('(?<=verticalDpi=")[0-9]+', xml, perl = TRUE))
-
- ## Update ----
- self$worksheets[[sheet]]$pageSetup <- sprintf(
- '',
- paperSize, orientation, scale, as.integer(fitToWidth), as.integer(fitToHeight), hdpi, vdpi
- )
-
- if (fitToHeight || fitToWidth) {
- self$worksheets[[sheet]]$sheetPr <- unique(c(self$worksheets[[sheet]]$sheetPr, ''))
- }
-
- self$worksheets[[sheet]]$pageMargins <-
- sprintf('', left, right, top, bottom, header, footer)
-
- validRow <- function(summaryRow) {
- return(tolower(summaryRow) %in% c("above", "below"))
- }
- validCol <- function(summaryCol) {
- return(tolower(summaryCol) %in% c("left", "right"))
- }
-
- outlinepr <- ""
-
- if (!is.null(summaryRow)) {
-
- if (!validRow(summaryRow)) {
- stop("Invalid \`summaryRow\` option. Must be one of \"Above\" or \"Below\".")
- } else if (tolower(summaryRow) == "above") {
- outlinepr <- ' summaryBelow=\"0\"'
- } else {
- outlinepr <- ' summaryBelow=\"1\"'
- }
- }
-
- if (!is.null(summaryCol)) {
-
- if (!validCol(summaryCol)) {
- stop("Invalid \`summaryCol\` option. Must be one of \"Left\" or \"Right\".")
- } else if (tolower(summaryCol) == "left") {
- outlinepr <- paste0(outlinepr, ' summaryRight=\"0\"')
- } else {
- outlinepr <- paste0(outlinepr, ' summaryRight=\"1\"')
- }
- }
-
- if (!stri_isempty(outlinepr)) {
- self$worksheets[[sheet]]$sheetPr <- unique(c(self$worksheets[[sheet]]$sheetPr, paste0("")))
- }
-
- ## print Titles ----
- if (!is.null(printTitleRows) && is.null(printTitleCols)) {
- if (!is.numeric(printTitleRows)) {
- stop("printTitleRows must be numeric.")
- }
-
- private$create_named_region(
- ref1 = paste0("$", min(printTitleRows)),
- ref2 = paste0("$", max(printTitleRows)),
- name = "_xlnm.Print_Titles",
- sheet = self$get_sheet_names()[[sheet]],
- localSheetId = sheet - 1L
- )
- } else if (!is.null(printTitleCols) && is.null(printTitleRows)) {
- if (!is.numeric(printTitleCols)) {
- stop("printTitleCols must be numeric.")
- }
-
- cols <- int2col(range(printTitleCols))
- private$create_named_region(
- ref1 = paste0("$", cols[1]),
- ref2 = paste0("$", cols[2]),
- name = "_xlnm.Print_Titles",
- sheet = self$get_sheet_names()[[sheet]],
- localSheetId = sheet - 1L
- )
- } else if (!is.null(printTitleCols) && !is.null(printTitleRows)) {
- if (!is.numeric(printTitleRows)) {
- stop("printTitleRows must be numeric.")
- }
-
- if (!is.numeric(printTitleCols)) {
- stop("printTitleCols must be numeric.")
- }
-
- cols <- int2col(range(printTitleCols))
- rows <- range(printTitleRows)
-
- cols <- paste(paste0("$", cols[1]), paste0("$", cols[2]), sep = ":")
- rows <- paste(paste0("$", rows[1]), paste0("$", rows[2]), sep = ":")
- localSheetId <- sheet - 1L
- sheet <- self$get_sheet_names()[[sheet]]
-
- self$workbook$definedNames <- c(
- self$workbook$definedNames,
- sprintf('\'%s\'!%s,\'%s\'!%s', localSheetId, sheet, cols, sheet, rows)
- )
-
- }
-
- invisible(self)
- },
-
- ## header footer ----
+ wb_page_setup_impl(
+ self = self,
+ private = private,
+ sheet = sheet,
+ orientation = orientation,
+ scale = scale,
+ left = left,
+ right = right,
+ top = top,
+ bottom = bottom,
+ header = header,
+ footer = footer,
+ fitToWidth = fitToWidth,
+ fitToHeight = fitToHeight,
+ paperSize = paperSize,
+ printTitleRows = printTitleRows,
+ printTitleCols = printTitleCols,
+ summaryRow = summaryRow,
+ summaryCol = summaryCol
+ )
+ },
+
+ ### header footer ----
#' @description Sets headers and footers
#' @param sheet sheet
@@ -4171,195 +1373,84 @@ wbWorkbook <- R6::R6Class(
firstHeader = NULL,
firstFooter = NULL
) {
- sheet <- private$get_sheet_index(sheet)
-
- if (!is.null(header) && length(header) != 3) {
- stop("header must have length 3 where elements correspond to positions: left, center, right.")
- }
-
- if (!is.null(footer) && length(footer) != 3) {
- stop("footer must have length 3 where elements correspond to positions: left, center, right.")
- }
-
- if (!is.null(evenHeader) && length(evenHeader) != 3) {
- stop("evenHeader must have length 3 where elements correspond to positions: left, center, right.")
- }
-
- if (!is.null(evenFooter) && length(evenFooter) != 3) {
- stop("evenFooter must have length 3 where elements correspond to positions: left, center, right.")
- }
-
- if (!is.null(firstHeader) && length(firstHeader) != 3) {
- stop("firstHeader must have length 3 where elements correspond to positions: left, center, right.")
- }
-
- if (!is.null(firstFooter) && length(firstFooter) != 3) {
- stop("firstFooter must have length 3 where elements correspond to positions: left, center, right.")
- }
-
- # TODO this could probably be moved to the hf assignment
- oddHeader <- headerFooterSub(header)
- oddFooter <- headerFooterSub(footer)
- evenHeader <- headerFooterSub(evenHeader)
- evenFooter <- headerFooterSub(evenFooter)
- firstHeader <- headerFooterSub(firstHeader)
- firstFooter <- headerFooterSub(firstFooter)
-
- hf <- list(
- oddHeader = naToNULLList(oddHeader),
- oddFooter = naToNULLList(oddFooter),
- evenHeader = naToNULLList(evenHeader),
- evenFooter = naToNULLList(evenFooter),
- firstHeader = naToNULLList(firstHeader),
- firstFooter = naToNULLList(firstFooter)
+ wb_set_header_footer_impl(
+ self = self,
+ private = private,
+ sheet = sheet,
+ header = header,
+ footer = footer,
+ evenHeader = evenHeader,
+ evenFooter = evenFooter,
+ firstHeader = firstHeader,
+ firstFooter = firstFooter
)
-
- if (all(lengths(hf) == 0)) {
- hf <- NULL
- }
-
- self$worksheets[[sheet]]$headerFooter <- hf
- invisible(self)
},
+ ### tables ----
+
#' @description get tables
#' @param sheet sheet
#' @returns The sheet tables. `character()` if empty
get_tables = function(sheet = current_sheet()) {
- if (length(sheet) != 1) {
- stop("sheet argument must be length 1")
- }
-
- if (is.null(self$tables)) {
- return(character())
- }
-
- sheet <- private$get_sheet_index(sheet)
- if (is.na(sheet)) stop("No such sheet in workbook")
-
- sel <- self$tables$tab_sheet == sheet & self$tables$tab_act == 1
- tables <- self$tables$tab_name[sel]
- refs <- self$tables$tab_ref[sel]
-
- if (length(tables)) {
- attr(tables, "refs") <- refs
- }
-
- return(tables)
+ wb_get_tables_impl(
+ self = self,
+ private = private,
+ sheet = sheet
+ )
},
-
#' @description remove tables
#' @param sheet sheet
#' @param table table
#' @returns The `wbWorkbook` object
remove_tables = function(sheet = current_sheet(), table) {
- if (length(table) != 1) {
- stop("table argument must be length 1")
- }
-
- ## delete table object and all data in it
- sheet <- private$get_sheet_index(sheet)
-
- if (!table %in% self$tables$tab_name) {
- stop(sprintf("table '%s' does not exist.", table), call. = FALSE)
- }
-
- ## delete table object (by flagging as deleted)
- inds <- self$tables$tab_sheet %in% sheet & self$tables$tab_name %in% table
- table_name_original <- self$tables$tab_name[inds]
- refs <- self$tables$tab_ref[inds]
-
- self$tables$tab_name[inds] <- paste0(table_name_original, "_openxlsx_deleted")
- self$tables$tab_ref[inds] <- ""
- self$tables$tab_sheet[inds] <- 0
- self$tables$tab_xml[inds] <- ""
- self$tables$tab_act[inds] <- 0
-
- ## delete reference from worksheet to table
- worksheet_table_names <- attr(self$worksheets[[sheet]]$tableParts, "tableName")
- to_remove <- which(worksheet_table_names == table_name_original)
-
- # (1) remove the rId from worksheet_rels
- rm_tab_rId <- rbindlist(xml_attr(self$worksheets[[sheet]]$tableParts[to_remove], "tablePart"))["r:id"]
- ws_rels <- self$worksheets_rels[[sheet]]
- is_rm_table <- grepl(rm_tab_rId, ws_rels)
- self$worksheets_rels[[sheet]] <- ws_rels[!is_rm_table]
-
- # (2) remove the rId from tableParts
- self$worksheets[[sheet]]$tableParts <- self$worksheets[[sheet]]$tableParts[-to_remove]
- attr(self$worksheets[[sheet]]$tableParts, "tableName") <- worksheet_table_names[-to_remove]
-
-
- ## Now delete data from the worksheet
- refs <- strsplit(refs, split = ":")[[1]]
- rows <- as.integer(gsub("[A-Z]", "", refs))
- rows <- seq(from = rows[1], to = rows[2], by = 1)
-
- cols <- col2int(refs)
- cols <- seq(from = cols[1], to = cols[2], by = 1)
-
- ## now delete data
- delete_data(wb = self, sheet = sheet, rows = rows, cols = cols)
- invisible(self)
+ wb_remove_tables_impl(
+ self = self,
+ private = private,
+ sheet = sheet,
+ table = table
+ )
},
+ ### filters ----
+
#' @description add filters
#' @param sheet sheet
#' @param rows rows
#' @param cols cols
#' @returns The `wbWorkbook` object
add_filter = function(sheet = current_sheet(), rows, cols) {
- sheet <- private$get_sheet_index(sheet)
-
- if (length(rows) != 1) {
- stop("row must be a numeric of length 1.")
- }
-
- if (!is.numeric(cols)) {
- cols <- col2int(cols)
- }
-
- self$worksheets[[sheet]]$autoFilter <- sprintf(
- '',
- paste(get_cell_refs(data.frame("x" = c(rows, rows), "y" = c(min(cols), max(cols)))), collapse = ":")
+ wb_add_filter_impl(
+ self = self,
+ private = private,
+ sheet = sheet,
+ rows = rows,
+ cols = cols
)
-
- invisible(self)
},
#' @description remove filters
#' @param sheet sheet
#' @returns The `wbWorkbook` object
remove_filter = function(sheet = current_sheet()) {
- for (s in private$get_sheet_index(sheet)) {
- self$worksheets[[s]]$autoFilter <- character()
- }
-
- invisible(self)
+ wb_remove_filter_impl(self, private, sheet)
},
+ ### grid lines ----
+
#' @description grid lines
#' @param sheet sheet
#' @param show show
#' @param print print
#' @returns The `wbWorkbook` object
grid_lines = function(sheet = current_sheet(), show = FALSE, print = show) {
- sheet <- private$get_sheet_index(sheet)
-
- assert_class(show, "logical")
- assert_class(print, "logical")
-
- ## show
- sv <- self$worksheets[[sheet]]$sheetViews
- sv <- xml_attr_mod(sv, c(showGridLines = as_xml_attr(show)))
- self$worksheets[[sheet]]$sheetViews <- sv
-
- ## print
- if (print)
- self$worksheets[[sheet]]$set_print_options(gridLines = print, gridLinesSet = print)
-
- invisible(self)
+ wb_grid_lines_impl(
+ self = self,
+ private = private,
+ sheet = sheet,
+ show = show,
+ print = print
+ )
},
### named region ----
@@ -4400,83 +1491,35 @@ wbWorkbook <- R6::R6Class(
help = NULL,
hidden = NULL,
localName = NULL,
- publishToServer = NULL,
- statusBar = NULL,
- vbProcedure = NULL,
- workbookParameter = NULL,
- xml = NULL
- ) {
- sheet <- private$get_sheet_index(sheet)
-
- if (!is.numeric(rows)) {
- stop("rows argument must be a numeric/integer vector")
- }
-
- if (!is.numeric(cols)) {
- stop("cols argument must be a numeric/integer vector")
- }
-
- localSheetId <- ""
- if (localSheet) localSheetId <- as.character(sheet)
-
- ## check name doesn't already exist
- ## named region
-
- definedNames <- rbindlist(xml_attr(self$workbook$definedNames, level1 = "definedName"))
- sel1 <- tolower(definedNames$name) == tolower(name)
- sel2 <- definedNames$localSheetId == localSheetId
- if (!is.null(definedNames$localSheetId)) {
- sel <- sel1 & sel2
- } else {
- sel <- sel1
- }
- match_dn <- which(sel)
-
- if (any(match_dn)) {
- if (overwrite)
- self$workbook$definedNames <- self$workbook$definedNames[-match_dn]
- else
- stop(sprintf("Named region with name '%s' already exists! Use overwrite = TRUE if you want to replace it", name))
- } else if (grepl("^[A-Z]{1,3}[0-9]+$", name)) {
- stop("name cannot look like a cell reference.")
- }
-
- cols <- round(cols)
- rows <- round(rows)
-
- startCol <- min(cols)
- endCol <- max(cols)
-
- startRow <- min(rows)
- endRow <- max(rows)
-
- ref1 <- paste0("$", int2col(startCol), "$", startRow)
- ref2 <- paste0("$", int2col(endCol), "$", endRow)
-
- if (localSheetId == "") localSheetId <- NULL
-
- private$create_named_region(
- ref1 = ref1,
- ref2 = ref2,
- name = name,
- sheet = self$sheet_names[sheet],
- localSheetId = localSheetId,
- comment = comment,
- customMenu = customMenu,
- description = description,
- is_function = is_function,
- functionGroupId = functionGroupId,
- help = help,
- hidden = hidden,
- localName = localName,
- publishToServer = publishToServer,
- statusBar = statusBar,
- vbProcedure = vbProcedure,
- workbookParameter = workbookParameter,
- xml = xml
+ publishToServer = NULL,
+ statusBar = NULL,
+ vbProcedure = NULL,
+ workbookParameter = NULL,
+ xml = NULL
+ ) {
+ wb_add_named_region_impl(
+ self = self,
+ private = private,
+ sheet = sheet,
+ cols = cols,
+ rows = rows,
+ name = name,
+ localSheet = localSheet,
+ overwrite = overwrite,
+ comment = comment,
+ customMenu = customMenu,
+ description = description,
+ is_function = is_function,
+ functionGroupId = functionGroupId,
+ help = help,
+ hidden = hidden,
+ localName = localName,
+ publishToServer = publishToServer,
+ statusBar = statusBar,
+ vbProcedure = vbProcedure,
+ workbookParameter = workbookParameter,
+ xml = xml
)
-
- invisible(self)
},
#' @description remove a named region
@@ -4484,64 +1527,29 @@ wbWorkbook <- R6::R6Class(
#' @param name name
#' @returns The `wbWorkbook` object
remove_named_region = function(sheet = current_sheet(), name = NULL) {
- # get all nown defined names
- dn <- get_named_regions(self)
-
- if (is.null(name) && !is.null(sheet)) {
- sheet <- private$get_sheet_index(sheet)
- del <- dn$id[dn$sheet == sheet]
- } else if (!is.null(name) && is.null(sheet)) {
- del <- dn$id[dn$name == name]
- } else {
- sheet <- private$get_sheet_index(sheet)
- del <- dn$id[dn$sheet == sheet & dn$name == name]
- }
-
- if (length(del)) {
- self$workbook$definedNames <- self$workbook$definedNames[-del]
- } else {
- if (!is.null(name))
- warning(sprintf("Cannot find named region with name '%s'", name))
- # do not warn if wb and sheet are selected. wb_delete_named_region is
- # called with every wb_remove_worksheet and would throw meaningless
- # warnings. For now simply assume if no name is defined, that the
- # user does not care, as long as no defined name remains on a sheet.
- }
-
- invisible(self)
+ wb_remove_named_region_impl(
+ self = self,
+ private = private,
+ sheet = sheet,
+ name = name
+ )
},
+ ### order ----
+
#' @description set worksheet order
#' @param sheets sheets
#' @return The `wbWorkbook` object
set_order = function(sheets) {
- sheets <- private$get_sheet_index(sheet = sheets)
-
- if (anyDuplicated(sheets)) {
- stop("`sheets` cannot have duplicates")
- }
-
- if (length(sheets) != length(self$worksheets)) {
- stop(sprintf("Worksheet order must be same length as number of worksheets [%s]", length(self$worksheets)))
- }
-
- if (any(sheets > length(self$worksheets))) {
- stop("Elements of order are greater than the number of worksheets")
- }
-
- self$sheetOrder <- sheets
- invisible(self)
+ wb_set_order_impl(self, private, sheets)
},
- ## sheet visibility ----
+ ### sheet visibility ----
#' @description Get sheet visibility
#' @returns Returns sheet visibility
get_sheet_visibility = function() {
- state <- rep("visible", length(self$workbook$sheets))
- state[grepl("hidden", self$workbook$sheets)] <- "hidden"
- state[grepl("veryHidden", self$workbook$sheets, ignore.case = TRUE)] <- "veryHidden"
- state
+ wb_get_sheet_visibility_impl(self, private)
},
#' @description Set sheet visibility
@@ -4549,44 +1557,15 @@ wbWorkbook <- R6::R6Class(
#' @param sheet sheet
#' @returns The `wbWorkbook` object
set_sheet_visibility = function(sheet = current_sheet(), value) {
- if (length(value) != length(sheet)) {
- stop("`value` and `sheet` must be the same length")
- }
-
- sheet <- private$get_sheet_index(sheet)
-
- value <- tolower(as.character(value))
- value[value %in% "true"] <- "visible"
- value[value %in% "false"] <- "hidden"
- value[value %in% "veryhidden"] <- "veryHidden"
-
- exState0 <- reg_match0(self$workbook$sheets[sheet], '(?<=state=")[^"]+')
- exState <- tolower(exState0)
- exState[exState %in% "true"] <- "visible"
- exState[exState %in% "hidden"] <- "hidden"
- exState[exState %in% "false"] <- "hidden"
- exState[exState %in% "veryhidden"] <- "veryHidden"
-
-
- inds <- which(value != exState)
-
- if (length(inds) == 0) {
- return(invisible(self))
- }
-
- for (i in seq_along(self$worksheets)) {
- self$workbook$sheets[sheet[i]] <- gsub(exState0[i], value[i], self$workbook$sheets[sheet[i]], fixed = TRUE)
- }
-
- if (!any(self$get_sheet_visibility() %in% c("true", "visible"))) {
- warning("A workbook must have atleast 1 visible worksheet. Setting first for visible")
- self$set_sheet_visibility(1, TRUE)
- }
-
- invisible(self)
+ wb_set_sheet_visibility_impl(
+ self = self,
+ private = private,
+ sheet = sheet,
+ value = value
+ )
},
- ## page breaks ----
+ ### page breaks ----
#' @description Add a page break
#' @param sheet sheet
@@ -4594,11 +1573,17 @@ wbWorkbook <- R6::R6Class(
#' @param col col
#' @returns The `wbWorkbook` object
add_page_break = function(sheet = current_sheet(), row = NULL, col = NULL) {
- sheet <- private$get_sheet_index(sheet)
- self$worksheets[[sheet]]$add_page_break(row = row, col = col)
- invisible(self)
+ wb_add_page_breaks_impl(
+ self = self,
+ private = private,
+ sheet = sheet,
+ row = row,
+ col = col
+ )
},
+ ### clean sheet----
+
#' @description clean sheet (remove all values)
#' @param sheet sheet
#' @param numbers remove all numbers
@@ -4607,20 +1592,25 @@ wbWorkbook <- R6::R6Class(
#' @param merged_cells remove all merged_cells
#' @return The `wbWorksheetObject`, invisibly
clean_sheet = function(
- sheet = current_sheet(),
- numbers = TRUE,
- characters = TRUE,
- styles = TRUE,
- merged_cells = TRUE
+ sheet = current_sheet(),
+ numbers = TRUE,
+ characters = TRUE,
+ styles = TRUE,
+ merged_cells = TRUE
) {
- sheet <- private$get_sheet_index(sheet)
- self$worksheets[[sheet]]$clean_sheet(numbers = numbers, characters = characters,
+ wb_clean_sheet_impl(
+ self = self,
+ private = private,
+ sheet = sheet,
+ numbers = numbers,
+ characters = characters,
styles = styles,
merged_cells = merged_cells
)
- invisible(self)
},
+ ### styles ----
+
#' @description create borders for cell region
#' @param sheet a worksheet
#' @param dims dimensions on the worksheet e.g. "A1", "A1:A5", "A1:H5"
@@ -4654,396 +1644,39 @@ wbWorkbook <- R6::R6Class(
#' wb$add_border(1, dims = "A2:K33", inner_vgrid = "thin", inner_vcolor = c(rgb="FF808080"))
#' @return The `wbWorksheetObject`, invisibly
add_border = function(
- sheet = current_sheet(),
- dims = "A1",
- bottom_color = wb_colour(hex = "FF000000"),
- left_color = wb_colour(hex = "FF000000"),
- right_color = wb_colour(hex = "FF000000"),
- top_color = wb_colour(hex = "FF000000"),
- bottom_border = "thin",
- left_border = "thin",
- right_border = "thin",
- top_border = "thin",
- inner_hgrid = NULL,
- inner_hcolor = NULL,
- inner_vgrid = NULL,
- inner_vcolor = NULL
+ sheet = current_sheet(),
+ dims = "A1",
+ bottom_color = wb_colour(hex = "FF000000"),
+ left_color = wb_colour(hex = "FF000000"),
+ right_color = wb_colour(hex = "FF000000"),
+ top_color = wb_colour(hex = "FF000000"),
+ bottom_border = "thin",
+ left_border = "thin",
+ right_border = "thin",
+ top_border = "thin",
+ inner_hgrid = NULL,
+ inner_hcolor = NULL,
+ inner_vgrid = NULL,
+ inner_vcolor = NULL
) {
-
- # TODO merge styles and if a style is already present, only add the newly
- # created border style
-
- # cc <- wb$worksheets[[sheet]]$sheet_data$cc
- # df_s <- as.data.frame(lapply(df, function(x) cc$c_s[cc$r %in% x]))
-
- df <- dims_to_dataframe(dims, fill = TRUE)
- sheet <- private$get_sheet_index(sheet)
-
- private$do_cell_init(sheet, dims)
-
- ### beg border creation
- full_single <- create_border(
- top = top_border, top_color = top_color,
- bottom = bottom_border, bottom_color = bottom_color,
- left = left_border, left_color = left_color,
- right = left_border, right_color = right_color
- )
-
- top_single <- create_border(
- top = top_border, top_color = top_color,
- bottom = inner_hgrid, bottom_color = inner_hcolor,
- left = left_border, left_color = left_color,
- right = left_border, right_color = right_color
- )
-
- middle_single <- create_border(
- top = inner_hgrid, top_color = inner_hcolor,
- bottom = inner_hgrid, bottom_color = inner_hcolor,
- left = left_border, left_color = left_color,
- right = left_border, right_color = right_color
- )
-
- bottom_single <- create_border(
- top = inner_hgrid, top_color = inner_hcolor,
- bottom = bottom_border, bottom_color = bottom_color,
- left = left_border, left_color = left_color,
- right = left_border, right_color = right_color
- )
-
- left_single <- create_border(
- top = top_border, top_color = top_color,
- bottom = bottom_border, bottom_color = bottom_color,
- left = left_border, left_color = left_color,
- right = inner_vgrid, right_color = inner_vcolor
- )
-
- right_single <- create_border(
- top = top_border, top_color = top_color,
- bottom = bottom_border, bottom_color = bottom_color,
- left = inner_vgrid, left_color = inner_vcolor,
- right = right_border, right_color = right_color
- )
-
- center_single <- create_border(
- top = top_border, top_color = top_color,
- bottom = bottom_border, bottom_color = bottom_color,
- left = inner_vgrid, left_color = inner_vcolor,
- right = inner_vgrid, right_color = inner_vcolor
- )
-
- top_left <- create_border(
- top = top_border, top_color = top_color,
- bottom = inner_hgrid, bottom_color = inner_hcolor,
- left = left_border, left_color = left_color,
- right = inner_vgrid, right_color = inner_vcolor
- )
-
- top_right <- create_border(
- top = top_border, top_color = top_color,
- bottom = inner_hgrid, bottom_color = inner_hcolor,
- left = inner_vgrid, left_color = inner_vcolor,
- right = left_border, right_color = right_color
- )
-
- bottom_left <- create_border(
- top = inner_hgrid, top_color = inner_hcolor,
- bottom = bottom_border, bottom_color = bottom_color,
- left = left_border, left_color = left_color,
- right = inner_vgrid, right_color = inner_vcolor
- )
-
- bottom_right <- create_border(
- top = inner_hgrid, top_color = inner_hcolor,
- bottom = bottom_border, bottom_color = bottom_color,
- left = inner_vgrid, left_color = inner_vcolor,
- right = left_border, right_color = right_color
- )
-
- top_center <- create_border(
- top = top_border, top_color = top_color,
- bottom = inner_hgrid, bottom_color = inner_hcolor,
- left = inner_vgrid, left_color = inner_vcolor,
- right = inner_vgrid, right_color = inner_vcolor
- )
-
- bottom_center <- create_border(
- top = inner_hgrid, top_color = inner_hcolor,
- bottom = bottom_border, bottom_color = bottom_color,
- left = inner_vgrid, left_color = inner_vcolor,
- right = inner_vgrid, right_color = inner_vcolor
- )
-
- middle_left <- create_border(
- top = inner_hgrid, top_color = inner_hcolor,
- bottom = inner_hgrid, bottom_color = inner_hcolor,
- left = left_border, left_color = left_color,
- right = inner_vgrid, right_color = inner_vcolor
- )
-
- middle_right <- create_border(
- top = inner_hgrid, top_color = inner_hcolor,
- bottom = inner_hgrid, bottom_color = inner_hcolor,
- left = inner_vgrid, left_color = inner_vcolor,
- right = right_border, right_color = right_color
+ wb_add_border_impl(
+ self = self,
+ private = private,
+ sheet = sheet,
+ dims = dims,
+ bottom_color = bottom_color,
+ left_color = left_color,
+ right_color = right_color,
+ top_color = top_color,
+ bottom_border = bottom_border,
+ left_border = left_border,
+ right_border = right_border,
+ top_border = top_border,
+ inner_hgrid = inner_hgrid,
+ inner_hcolor = inner_hcolor,
+ inner_vgrid = inner_vgrid,
+ inner_vcolor = inner_vcolor
)
-
- inner_cell <- create_border(
- top = inner_hgrid, top_color = inner_hcolor,
- bottom = inner_hgrid, bottom_color = inner_hcolor,
- left = inner_vgrid, left_color = inner_vcolor,
- right = inner_vgrid, right_color = inner_vcolor
- )
- ### end border creation
-
- #
- # /* top_single */
- # /* middle_single */
- # /* bottom_single */
- #
-
- # /* left_single --- center_single --- right_single */
-
- #
- # /* top_left --- top_center --- top_right */
- # /* - - */
- # /* - - */
- # /* - - */
- # /* left_middle right_middle */
- # /* - - */
- # /* - - */
- # /* - - */
- # /* left_bottom - bottom_center - bottom_right */
- #
-
- ## beg cell references
- if (ncol(df) == 1 && nrow(df) == 1)
- dim_full_single <- df[1, 1]
-
- if (ncol(df) == 1 && nrow(df) >= 2) {
- dim_top_single <- df[1, 1]
- dim_bottom_single <- df[nrow(df), 1]
- if (nrow(df) >= 3) {
- mid <- df[, 1]
- dim_middle_single <- mid[!mid %in% c(dim_top_single, dim_bottom_single)]
- }
- }
-
- if (ncol(df) >= 2 && nrow(df) == 1) {
- dim_left_single <- df[1, 1]
- dim_right_single <- df[1, ncol(df)]
- if (ncol(df) >= 3) {
- ctr <- df[1, ]
- dim_center_single <- ctr[!ctr %in% c(dim_left_single, dim_right_single)]
- }
- }
-
- if (ncol(df) >= 2 && nrow(df) >= 2) {
- dim_top_left <- df[1, 1]
- dim_bottom_left <- df[nrow(df), 1]
- dim_top_right <- df[1, ncol(df)]
- dim_bottom_right <- df[nrow(df), ncol(df)]
-
- if (nrow(df) >= 3) {
- top_mid <- df[, 1]
- bottom_mid <- df[, ncol(df)]
-
- dim_middle_left <- top_mid[!top_mid %in% c(dim_top_left, dim_bottom_left)]
- dim_middle_right <- bottom_mid[!bottom_mid %in% c(dim_top_right, dim_bottom_right)]
- }
-
- if (ncol(df) >= 3) {
- top_ctr <- df[1, ]
- bottom_ctr <- df[nrow(df), ]
-
- dim_top_center <- top_ctr[!top_ctr %in% c(dim_top_left, dim_top_right)]
- dim_bottom_center <- bottom_ctr[!bottom_ctr %in% c(dim_bottom_left, dim_bottom_right)]
- }
-
- if (ncol(df) > 2 && nrow(df) > 2) {
- t_row <- 1
- b_row <- nrow(df)
- l_row <- 1
- r_row <- ncol(df)
- dim_inner_cell <- as.character(unlist(df[c(-t_row, -b_row), c(-l_row, -r_row)]))
- }
- }
- ### end cell references
-
- # add some random string to the name. if called multiple times, new
- # styles will be created. We do not look for identical styles, therefor
- # we might create duplicates, but if a single style changes, the rest of
- # the workbook remains valid.
- smp <- random_string()
- s <- function(x) paste0(smp, "s", deparse(substitute(x)), seq_along(x))
- sfull_single <- paste0(smp, "full_single")
- stop_single <- paste0(smp, "full_single")
- sbottom_single <- paste0(smp, "bottom_single")
- smiddle_single <- paste0(smp, "middle_single")
- sleft_single <- paste0(smp, "left_single")
- sright_single <- paste0(smp, "right_single")
- scenter_single <- paste0(smp, "center_single")
- stop_left <- paste0(smp, "top_left")
- stop_right <- paste0(smp, "top_right")
- sbottom_left <- paste0(smp, "bottom_left")
- sbottom_right <- paste0(smp, "bottom_right")
- smiddle_left <- paste0(smp, "middle_left")
- smiddle_right <- paste0(smp, "middle_right")
- stop_center <- paste0(smp, "top_center")
- sbottom_center <- paste0(smp, "bottom_center")
- sinner_cell <- paste0(smp, "inner_cell")
-
- # ncol == 1
- if (ncol(df) == 1) {
-
- # single cell
- if (nrow(df) == 1) {
- self$styles_mgr$add(full_single, sfull_single)
- xf_prev <- get_cell_styles(self, sheet, dims)
- xf_full_single <- set_border(xf_prev, self$styles_mgr$get_border_id(sfull_single))
- self$styles_mgr$add(xf_full_single, xf_full_single)
- self$set_cell_style(sheet, dims, self$styles_mgr$get_xf_id(xf_full_single))
- }
-
- # create top & bottom piece
- if (nrow(df) >= 2) {
-
- # top single
- self$styles_mgr$add(top_single, stop_single)
- xf_prev <- get_cell_styles(self, sheet, dim_top_single)
- xf_top_single <- set_border(xf_prev, self$styles_mgr$get_border_id(stop_single))
- self$styles_mgr$add(xf_top_single, xf_top_single)
- self$set_cell_style(sheet, dim_top_single, self$styles_mgr$get_xf_id(xf_top_single))
-
- # bottom single
- self$styles_mgr$add(bottom_single, sbottom_single)
- xf_prev <- get_cell_styles(self, sheet, dim_bottom_single)
- xf_bottom_single <- set_border(xf_prev, self$styles_mgr$get_border_id(sbottom_single))
- self$styles_mgr$add(xf_bottom_single, xf_bottom_single)
- self$set_cell_style(sheet, dim_bottom_single, self$styles_mgr$get_xf_id(xf_bottom_single))
- }
-
- # create middle piece(s)
- if (nrow(df) >= 3) {
-
- # middle single
- self$styles_mgr$add(middle_single, smiddle_single)
- xf_prev <- get_cell_styles(self, sheet, dim_middle_single)
- xf_middle_single <- set_border(xf_prev, self$styles_mgr$get_border_id(smiddle_single))
- self$styles_mgr$add(xf_middle_single, xf_middle_single)
- self$set_cell_style(sheet, dim_middle_single, self$styles_mgr$get_xf_id(xf_middle_single))
- }
-
- }
-
- # create left and right single row pieces
- if (ncol(df) >= 2 && nrow(df) == 1) {
-
- # left single
- self$styles_mgr$add(left_single, sleft_single)
- xf_prev <- get_cell_styles(self, sheet, dim_left_single)
- xf_left_single <- set_border(xf_prev, self$styles_mgr$get_border_id(sleft_single))
- self$styles_mgr$add(xf_left_single, xf_left_single)
- self$set_cell_style(sheet, dim_left_single, self$styles_mgr$get_xf_id(xf_left_single))
-
- # right single
- self$styles_mgr$add(right_single, sright_single)
- xf_prev <- get_cell_styles(self, sheet, dim_right_single)
- xf_right_single <- set_border(xf_prev, self$styles_mgr$get_border_id(sright_single))
- self$styles_mgr$add(xf_right_single, xf_right_single)
- self$set_cell_style(sheet, dim_right_single, self$styles_mgr$get_xf_id(xf_right_single))
-
- # add single center piece(s)
- if (ncol(df) >= 3) {
-
- # center single
- self$styles_mgr$add(center_single, scenter_single)
- xf_prev <- get_cell_styles(self, sheet, dim_center_single)
- xf_center_single <- set_border(xf_prev, self$styles_mgr$get_border_id(scenter_single))
- self$styles_mgr$add(xf_center_single, xf_center_single)
- self$set_cell_style(sheet, dim_center_single, self$styles_mgr$get_xf_id(xf_center_single))
- }
-
- }
-
- # create left & right - top & bottom corners pieces
- if (ncol(df) >= 2 && nrow(df) >= 2) {
-
- # top left
- self$styles_mgr$add(top_left, stop_left)
- xf_prev <- get_cell_styles(self, sheet, dim_top_left)
- xf_top_left <- set_border(xf_prev, self$styles_mgr$get_border_id(stop_left))
- self$styles_mgr$add(xf_top_left, xf_top_left)
- self$set_cell_style(sheet, dim_top_left, self$styles_mgr$get_xf_id(xf_top_left))
-
- # top right
- self$styles_mgr$add(top_right, stop_right)
- xf_prev <- get_cell_styles(self, sheet, dim_top_right)
- xf_top_right <- set_border(xf_prev, self$styles_mgr$get_border_id(stop_right))
- self$styles_mgr$add(xf_top_right, xf_top_right)
- self$set_cell_style(sheet, dim_top_right, self$styles_mgr$get_xf_id(xf_top_right))
-
- # bottom left
- self$styles_mgr$add(bottom_left, sbottom_left)
- xf_prev <- get_cell_styles(self, sheet, dim_bottom_left)
- xf_bottom_left <- set_border(xf_prev, self$styles_mgr$get_border_id(sbottom_left))
- self$styles_mgr$add(xf_bottom_left, xf_bottom_left)
- self$set_cell_style(sheet, dim_bottom_left, self$styles_mgr$get_xf_id(xf_bottom_left))
-
- # bottom right
- self$styles_mgr$add(bottom_right, sbottom_right)
- xf_prev <- get_cell_styles(self, sheet, dim_bottom_right)
- xf_bottom_right <- set_border(xf_prev, self$styles_mgr$get_border_id(sbottom_right))
- self$styles_mgr$add(xf_bottom_right, xf_bottom_right)
- self$set_cell_style(sheet, dim_bottom_right, self$styles_mgr$get_xf_id(xf_bottom_right))
- }
-
- # create left and right middle pieces
- if (ncol(df) >= 2 && nrow(df) >= 3) {
-
- # middle left
- self$styles_mgr$add(middle_left, smiddle_left)
- xf_prev <- get_cell_styles(self, sheet, dim_middle_left)
- xf_middle_left <- set_border(xf_prev, self$styles_mgr$get_border_id(smiddle_left))
- self$styles_mgr$add(xf_middle_left, xf_middle_left)
- self$set_cell_style(sheet, dim_middle_left, self$styles_mgr$get_xf_id(xf_middle_left))
-
- # middle right
- self$styles_mgr$add(middle_right, smiddle_right)
- xf_prev <- get_cell_styles(self, sheet, dim_middle_right)
- xf_middle_right <- set_border(xf_prev, self$styles_mgr$get_border_id(smiddle_right))
- self$styles_mgr$add(xf_middle_right, xf_middle_right)
- self$set_cell_style(sheet, dim_middle_right, self$styles_mgr$get_xf_id(xf_middle_right))
- }
-
- # create top and bottom center pieces
- if (ncol(df) >= 3 & nrow(df) >= 2) {
-
- # top center
- self$styles_mgr$add(top_center, stop_center)
- xf_prev <- get_cell_styles(self, sheet, dim_top_center)
- xf_top_center <- set_border(xf_prev, self$styles_mgr$get_border_id(stop_center))
- self$styles_mgr$add(xf_top_center, xf_top_center)
- self$set_cell_style(sheet, dim_top_center, self$styles_mgr$get_xf_id(xf_top_center))
-
- # bottom center
- self$styles_mgr$add(bottom_center, sbottom_center)
- xf_prev <- get_cell_styles(self, sheet, dim_bottom_center)
- xf_bottom_center <- set_border(xf_prev, self$styles_mgr$get_border_id(sbottom_center))
- self$styles_mgr$add(xf_bottom_center, xf_bottom_center)
- self$set_cell_style(sheet, dim_bottom_center, self$styles_mgr$get_xf_id(xf_bottom_center))
- }
-
- if (nrow(df) > 2 && ncol(df) > 2) {
-
- # inner cells
- self$styles_mgr$add(inner_cell, sinner_cell)
- xf_prev <- get_cell_styles(self, sheet, dim_inner_cell)
- xf_inner_cell <- set_border(xf_prev, self$styles_mgr$get_border_id(sinner_cell))
- self$styles_mgr$add(xf_inner_cell, xf_inner_cell)
- self$set_cell_style(sheet, dim_inner_cell, self$styles_mgr$get_xf_id(xf_inner_cell))
- }
-
- invisible(self)
},
#' @description provide simple fill function
@@ -5066,47 +1699,25 @@ wbWorkbook <- R6::R6Class(
#' "
#' @return The `wbWorksheetObject`, invisibly
add_fill = function(
- sheet = current_sheet(),
- dims = "A1",
- color = wb_colour(hex = "FFFFFF00"),
- pattern = "solid",
- gradient_fill = "",
- every_nth_col = 1,
- every_nth_row = 1
+ sheet = current_sheet(),
+ dims = "A1",
+ color = wb_colour(hex = "FFFFFF00"),
+ pattern = "solid",
+ gradient_fill = "",
+ every_nth_col = 1,
+ every_nth_row = 1
) {
- sheet <- private$get_sheet_index(sheet)
- private$do_cell_init(sheet, dims)
-
- # dim in dataframe can contain various styles. go cell by cell.
- did <- dims_to_dataframe(dims, fill = TRUE)
- # select a few cols and rows to fill
- cols <- (seq_len(ncol(did)) %% every_nth_col) == 0
- rows <- (seq_len(nrow(did)) %% every_nth_row) == 0
-
- dims <- unname(unlist(did[rows, cols, drop = FALSE]))
-
- cc <- self$worksheets[[sheet]]$sheet_data$cc
- cc <- cc[cc$r %in% dims, ]
- styles <- unique(cc[["c_s"]])
-
- for (style in styles) {
- dim <- cc[cc$c_s == style, "r"]
-
- new_fill <- create_fill(
- gradientFill = gradient_fill,
- patternType = pattern,
- fgColor = color
- )
- self$styles_mgr$add(new_fill, new_fill)
-
- xf_prev <- get_cell_styles(self, sheet, dim[[1]])
- xf_new_fill <- set_fill(xf_prev, self$styles_mgr$get_fill_id(new_fill))
- self$styles_mgr$add(xf_new_fill, xf_new_fill)
- s_id <- self$styles_mgr$get_xf_id(xf_new_fill)
- self$set_cell_style(sheet, dim, s_id)
- }
-
- invisible(self)
+ wb_add_fill_impl(
+ self = self,
+ private = private,
+ sheet = sheet,
+ dims = dims,
+ color = color,
+ pattern = pattern,
+ gradient_fill = gradient_fill,
+ every_nth_col = every_nth_col,
+ every_nth_row = every_nth_row
+ )
},
#' @description provide simple font function
@@ -5132,66 +1743,46 @@ wbWorkbook <- R6::R6Class(
#' wb$add_font("S1", "A1:K1", name = "Arial", color = wb_colour(theme = "4"))
#' @return The `wbWorksheetObject`, invisibly
add_font = function(
- sheet = current_sheet(),
- dims = "A1",
- name = "Calibri",
- color = wb_colour(hex = "FF000000"),
- size = "11",
- bold = "",
- italic = "",
- outline = "",
- strike = "",
- underline = "",
- # fine tuning
- charset = "",
- condense = "",
- extend = "",
- family = "",
- scheme = "",
- shadow = "",
- vertAlign = ""
+ sheet = current_sheet(),
+ dims = "A1",
+ name = "Calibri",
+ color = wb_colour(hex = "FF000000"),
+ size = "11",
+ bold = "",
+ italic = "",
+ outline = "",
+ strike = "",
+ underline = "",
+ # fine tuning
+ charset = "",
+ condense = "",
+ extend = "",
+ family = "",
+ scheme = "",
+ shadow = "",
+ vertAlign = ""
) {
- sheet <- private$get_sheet_index(sheet)
- private$do_cell_init(sheet, dims)
-
- did <- dims_to_dataframe(dims, fill = TRUE)
- dims <- unname(unlist(did))
-
- cc <- self$worksheets[[sheet]]$sheet_data$cc
- cc <- cc[cc$r %in% dims, ]
- styles <- unique(cc[["c_s"]])
-
- for (style in styles) {
- dim <- cc[cc$c_s == style, "r"]
-
- new_font <- create_font(
- b = bold,
- charset = charset,
- color = color,
- condense = condense,
- extend = extend,
- family = family,
- i = italic,
- name = name,
- outline = outline,
- scheme = scheme,
- shadow = shadow,
- strike = strike,
- sz = size,
- u = underline,
- vertAlign = vertAlign
- )
- self$styles_mgr$add(new_font, new_font)
-
- xf_prev <- get_cell_styles(self, sheet, dim[[1]])
- xf_new_font <- set_font(xf_prev, self$styles_mgr$get_font_id(new_font))
-
- self$styles_mgr$add(xf_new_font, xf_new_font)
- s_id <- self$styles_mgr$get_xf_id(xf_new_font)
- self$set_cell_style(sheet, dim, s_id)
- }
-
- invisible(self)
+ wb_add_font_impl(
+ self = self,
+ private = private,
+ sheet = sheet,
+ dims = dims,
+ name = name,
+ color = color,
+ size = size,
+ bold = bold,
+ italic = italic,
+ outline = outline,
+ strike = strike,
+ underline = underline,
+ charset = charset,
+ condense = condense,
+ extend = extend,
+ family = family,
+ scheme = scheme,
+ shadow = shadow,
+ vertAlign = vertAlign
+ )
},
#' @description provide simple number format function
@@ -5203,51 +1794,17 @@ wbWorkbook <- R6::R6Class(
#' wb$add_numfmt("S1", "A1:A33", numfmt = 1)
#' @return The `wbWorksheetObject`, invisibly
add_numfmt = function(
- sheet = current_sheet(),
- dims = "A1",
- numfmt
+ sheet = current_sheet(),
+ dims = "A1",
+ numfmt
) {
- sheet <- private$get_sheet_index(sheet)
- private$do_cell_init(sheet, dims)
-
- did <- dims_to_dataframe(dims, fill = TRUE)
- dims <- unname(unlist(did))
-
- cc <- self$worksheets[[sheet]]$sheet_data$cc
- cc <- cc[cc$r %in% dims, ]
- styles <- unique(cc[["c_s"]])
-
- if (inherits(numfmt, "character")) {
-
- for (style in styles) {
- dim <- cc[cc$c_s == style, "r"]
-
- new_numfmt <- create_numfmt(
- numFmtId = self$styles_mgr$next_numfmt_id(),
- formatCode = numfmt
- )
- self$styles_mgr$add(new_numfmt, new_numfmt)
-
- xf_prev <- get_cell_styles(self, sheet, dim[[1]])
- xf_new_numfmt <- set_numfmt(xf_prev, self$styles_mgr$get_numfmt_id(new_numfmt))
- self$styles_mgr$add(xf_new_numfmt, xf_new_numfmt)
- s_id <- self$styles_mgr$get_xf_id(xf_new_numfmt)
- self$set_cell_style(sheet, dim, s_id)
- }
-
- } else { # format is numeric
- for (style in styles) {
- dim <- cc[cc$c_s == style, "r"]
- xf_prev <- get_cell_styles(self, sheet, dim[[1]])
- xf_new_numfmt <- set_numfmt(xf_prev, numfmt)
- self$styles_mgr$add(xf_new_numfmt, xf_new_numfmt)
- s_id <- self$styles_mgr$get_xf_id(xf_new_numfmt)
- self$set_cell_style(sheet, dim, s_id)
- }
-
- }
-
- invisible(self)
+ wb_add_numfmt_impl(
+ self = self,
+ private = private,
+ sheet = sheet,
+ dims = dims,
+ numfm = numfmt
+ )
},
#' @description provide simple cell style format function
@@ -5289,81 +1846,65 @@ wbWorkbook <- R6::R6Class(
#' wrapText = "1")
#' @return The `wbWorksheetObject`, invisibly
add_cell_style = function(
- sheet = current_sheet(),
- dims = "A1",
- applyAlignment = NULL,
- applyBorder = NULL,
- applyFill = NULL,
- applyFont = NULL,
- applyNumberFormat = NULL,
- applyProtection = NULL,
- borderId = NULL,
- extLst = NULL,
- fillId = NULL,
- fontId = NULL,
- hidden = NULL,
- horizontal = NULL,
- indent = NULL,
- justifyLastLine = NULL,
- locked = NULL,
- numFmtId = NULL,
- pivotButton = NULL,
- quotePrefix = NULL,
- readingOrder = NULL,
- relativeIndent = NULL,
- shrinkToFit = NULL,
- textRotation = NULL,
- vertical = NULL,
- wrapText = NULL,
- xfId = NULL
+ sheet = current_sheet(),
+ dims = "A1",
+ applyAlignment = NULL,
+ applyBorder = NULL,
+ applyFill = NULL,
+ applyFont = NULL,
+ applyNumberFormat = NULL,
+ applyProtection = NULL,
+ borderId = NULL,
+ extLst = NULL,
+ fillId = NULL,
+ fontId = NULL,
+ hidden = NULL,
+ horizontal = NULL,
+ indent = NULL,
+ justifyLastLine = NULL,
+ locked = NULL,
+ numFmtId = NULL,
+ pivotButton = NULL,
+ quotePrefix = NULL,
+ readingOrder = NULL,
+ relativeIndent = NULL,
+ shrinkToFit = NULL,
+ textRotation = NULL,
+ vertical = NULL,
+ wrapText = NULL,
+ xfId = NULL
) {
- sheet <- private$get_sheet_index(sheet)
- private$do_cell_init(sheet, dims)
-
- did <- dims_to_dataframe(dims, fill = TRUE)
- dims <- unname(unlist(did))
-
- cc <- self$worksheets[[sheet]]$sheet_data$cc
- cc <- cc[cc$r %in% dims, ]
- styles <- unique(cc[["c_s"]])
-
- for (style in styles) {
- dim <- cc[cc$c_s == style, "r"]
- xf_prev <- get_cell_styles(self, sheet, dim[[1]])
- xf_new_cellstyle <- set_cellstyle(
- xf_node = xf_prev,
- applyAlignment = applyAlignment,
- applyBorder = applyBorder,
- applyFill = applyFill,
- applyFont = applyFont,
- applyNumberFormat = applyNumberFormat,
- applyProtection = applyProtection,
- borderId = borderId,
- extLst = extLst,
- fillId = fillId,
- fontId = fontId,
- hidden = hidden,
- horizontal = horizontal,
- indent = indent,
- justifyLastLine = justifyLastLine,
- locked = locked,
- numFmtId = numFmtId,
- pivotButton = pivotButton,
- quotePrefix = quotePrefix,
- readingOrder = readingOrder,
- relativeIndent = relativeIndent,
- shrinkToFit = shrinkToFit,
- textRotation = textRotation,
- vertical = vertical,
- wrapText = wrapText,
- xfId = xfId
- )
- self$styles_mgr$add(xf_new_cellstyle, xf_new_cellstyle)
- s_id <- self$styles_mgr$get_xf_id(xf_new_cellstyle)
- self$set_cell_style(sheet, dim, s_id)
- }
-
- invisible(self)
+ wb_add_cell_style_impl(
+ self = self,
+ private = private,
+ sheet = sheet,
+ dims = dims,
+ applyAlignment = applyAlignment,
+ applyBorder = applyBorder,
+ applyFill = applyFill,
+ applyFont = applyFont,
+ applyNumberFormat = applyNumberFormat,
+ applyProtection = applyProtection,
+ borderId = borderId,
+ extLst = extLst,
+ fillId = fillId,
+ fontId = fontId,
+ hidden = hidden,
+ horizontal = horizontal,
+ indent = indent,
+ justifyLastLine = justifyLastLine,
+ locked = locked,
+ numFmtId = numFmtId,
+ pivotButton = pivotButton,
+ quotePrefix = quotePrefix,
+ readingOrder = readingOrder,
+ relativeIndent = relativeIndent,
+ shrinkToFit = shrinkToFit,
+ textRotation = textRotation,
+ vertical = vertical,
+ wrapText = wrapText,
+ xfId = xfId
+ )
},
#' @description get sheet style
@@ -5371,20 +1912,12 @@ wbWorkbook <- R6::R6Class(
#' @param dims dims
#' @returns a character vector of cell styles
get_cell_style = function(sheet = current_sheet(), dims) {
-
- if (length(dims) == 1 && grepl(":", dims))
- dims <- dims_to_dataframe(dims, fill = TRUE)
- sheet <- private$get_sheet_index(sheet)
-
- # This alters the workbook
- temp <- self$clone()$.__enclos_env__$private$do_cell_init(sheet, dims)
-
- # if a range is passed (e.g. "A1:B2") we need to get every cell
- dims <- unname(unlist(dims))
-
- # TODO check that cc$r is alway valid. not sure atm
- sel <- temp$worksheets[[sheet]]$sheet_data$cc$r %in% dims
- temp$worksheets[[sheet]]$sheet_data$cc$c_s[sel]
+ wb_get_cell_style_impl(
+ self = self,
+ private = private,
+ sheet = sheet,
+ dims = dims
+ )
},
#' @description set sheet style
@@ -5393,106 +1926,28 @@ wbWorkbook <- R6::R6Class(
#' @param style style
#' @return The `wbWorksheetObject`, invisibly
set_cell_style = function(sheet = current_sheet(), dims, style) {
-
- if (length(dims) == 1 && grepl(":|;", dims))
- dims <- dims_to_dataframe(dims, fill = TRUE)
- sheet <- private$get_sheet_index(sheet)
-
- private$do_cell_init(sheet, dims)
-
- # if a range is passed (e.g. "A1:B2") we need to get every cell
- dims <- unname(unlist(dims))
-
- sel <- self$worksheets[[sheet]]$sheet_data$cc$r %in% dims
-
- self$worksheets[[sheet]]$sheet_data$cc$c_s[sel] <- style
-
- invisible(self)
+ wb_set_cell_style_impl(
+ self = self,
+ private = private,
+ sheet = sheet,
+ dims = dims,
+ style = style
+ )
},
#' @description clone style from one sheet to another
#' @param from the worksheet you are cloning
#' @param to the worksheet the style is applied to
clone_sheet_style = function(from = current_sheet(), to) {
-
- id_org <- private$get_sheet_index(from)
- id_new <- private$get_sheet_index(to)
-
- org_style <- self$worksheets[[id_org]]$sheet_data$cc
- wb_style <- self$worksheets[[id_new]]$sheet_data$cc
-
- # only clone styles from sheets with cc
- if (is.null(org_style)) {
- message("'from' has no sheet data styles to clone")
- } else {
-
- if (is.null(wb_style)) # if null, create empty dataframe
- wb_style <- create_char_dataframe(names(org_style), n = 0)
-
- # remove all values
- org_style <- org_style[c("r", "row_r", "c_r", "c_s")]
-
- # do not merge c_s and do not create duplicates
- merged_style <- merge(org_style,
- wb_style[-which(names(wb_style) == "c_s")],
- all = TRUE)
- merged_style[is.na(merged_style)] <- ""
- merged_style <- merged_style[!duplicated(merged_style["r"]), ]
-
- # will be ordere on save
- self$worksheets[[id_new]]$sheet_data$cc <- merged_style
-
- }
-
-
- # copy entire attributes from original sheet to new sheet
- org_rows <- self$worksheets[[id_org]]$sheet_data$row_attr
- new_rows <- self$worksheets[[id_new]]$sheet_data$row_attr
-
- if (is.null(org_style)) {
- message("'from' has no row styles to clone")
- } else {
-
- if (is.null(new_rows))
- new_rows <- create_char_dataframe(names(org_rows), n = 0)
-
- # only add the row information, nothing else
- merged_rows <- merge(org_rows,
- new_rows["r"],
- all = TRUE)
- merged_rows[is.na(merged_rows)] <- ""
- merged_rows <- merged_rows[!duplicated(merged_rows["r"]), ]
- ordr <- ordered(order(as.integer(merged_rows$r)))
- merged_rows <- merged_rows[ordr, ]
-
- self$worksheets[[id_new]]$sheet_data$row_attr <- merged_rows
- }
-
- self$worksheets[[id_new]]$cols_attr <-
- self$worksheets[[id_org]]$cols_attr
-
- self$worksheets[[id_new]]$dimension <-
- self$worksheets[[id_org]]$dimension
-
- self$worksheets[[id_new]]$mergeCells <-
- self$worksheets[[id_org]]$mergeCells
-
- invisible(self)
+ wb_clone_sheet_style_impl(self, private, from, to)
},
-
#' @description apply sparkline to worksheet
#' @param sheet the worksheet you are using
#' @param sparklines sparkline created by `create_sparkline()`
- add_sparklines = function(
- sheet = current_sheet(),
- sparklines
- ) {
- sheet <- private$get_sheet_index(sheet)
- self$worksheets[[sheet]]$add_sparklines(sparklines)
- invisible(self)
+ add_sparklines = function(sheet = current_sheet(), sparklines) {
+ wb_add_sparklines_impl(self, private, sheet, sparklines)
}
-
),
## private ----
@@ -5695,27 +2150,6 @@ wbWorkbook <- R6::R6Class(
invisible(self)
},
- modify_creators = function(method = c("add", "set", "remove"), value) {
- method <- match.arg(method)
- assert_class(value, "character")
-
- if (any(!has_chr(value))) {
- stop("all creators must contain characters without NAs", call. = FALSE)
- }
-
- value <- switch(
- method,
- add = unique(c(self$creator, value)),
- set = unique(value),
- remove = setdiff(self$creator, value)
- )
-
- self$creator <- value
- # core is made on initialization
- private$generate_base_core()
- invisible(self)
- },
-
get_worksheet = function(sheet) {
self$worksheets[[private$get_sheet_index(sheet)]]
},
@@ -5790,10 +2224,10 @@ wbWorkbook <- R6::R6Class(
## write vml output
write_file(
- head = '',
- body = pxml(vml_xml),
- tail = '',
- fl = file.path(dir, sprintf("vmlDrawing%s.vml", i))
+ head = '',
+ body = pxml(vml_xml),
+ tail = '',
+ fl = file.path(dir, sprintf("vmlDrawing%s.vml", i))
)
## vml drawing
@@ -6130,16 +2564,16 @@ wbWorkbook <- R6::R6Class(
},
do_conditional_formatting = function(
- sheet,
- startRow,
- endRow,
- startCol,
- endCol,
- dxfId,
- formula,
- type,
- values,
- params
+ sheet,
+ startRow,
+ endRow,
+ startCol,
+ endCol,
+ dxfId,
+ formula,
+ type,
+ values,
+ params
) {
# TODO consider defaults for logicals
# TODO rename: setConditionFormatting? Or addConditionalFormatting
@@ -6295,9 +2729,9 @@ wbWorkbook <- R6::R6Class(
seq_along(self$workbook.xml.rels),
function(i) {
gsub('(?<=Relationship Id="rId)[0-9]+',
- i,
- self$workbook.xml.rels[[i]],
- perl = TRUE
+ i,
+ self$workbook.xml.rels[[i]],
+ perl = TRUE
)
}
)
@@ -6306,19 +2740,19 @@ wbWorkbook <- R6::R6Class(
if (length(self$metadata)) {
self$append("workbook.xml.rels",
- sprintf(
- '',
- 1L + length(self$workbook.xml.rels)
- )
+ sprintf(
+ '',
+ 1L + length(self$workbook.xml.rels)
+ )
)
}
if (!is.null(self$vbaProject)) {
self$append("workbook.xml.rels",
- sprintf(
- '',
- 1L + length(self$workbook.xml.rels)
- )
+ sprintf(
+ '',
+ 1L + length(self$workbook.xml.rels)
+ )
)
}
diff --git a/R/onUnload.R b/R/onUnload.R
deleted file mode 100644
index cef02ed63..000000000
--- a/R/onUnload.R
+++ /dev/null
@@ -1,3 +0,0 @@
-.onUnload <- function(libpath) {
- library.dynam.unload("openxlsx2", libpath) # nocov
-}
diff --git a/R/openxlsx2.R b/R/openxlsx2.R
index d92b802e3..247d1d25f 100644
--- a/R/openxlsx2.R
+++ b/R/openxlsx2.R
@@ -7,7 +7,7 @@
#' or chains, `openxlsx2` allows to break many new ground.
#'
#' @name openxlsx2
-#' @docType package
+#' @docType packages
#' @useDynLib openxlsx2, .registration=TRUE
#'
#' @import Rcpp
@@ -64,6 +64,28 @@
#'
NULL
+# TODO export op.openxlsx2
+op.openxlsx2 <- list(
+ openxlsx2.borderColour = "black",
+ openxlsx2.borderStyle = "thin",
+ openxlsx2.dateFormat = "mm/dd/yyy",
+ openxlsx2.datetimeFormat = "yyyy-mm-dd hh:mm:ss",
+ openxlsx2.na.strings = "#N/A",
+ openxlsx2.numFmt = NULL,
+ openxlsx2.paperSize = 9,
+ openxlsx2.orientation = "portrait",
+ openxlsx2.sheet.default_name = "Sheet ",
+ openxlsx2.rightToLeft = NULL
+)
+
+.onLoad <- function(libname, pkgname) {
+ options(op.openxlsx2[names(op.openxlsx2) %out% names(options())])
+}
+
+.onUnload <- function(libpath) {
+ library.dynam.unload("openxlsx2", libpath) # nocov
+}
+
# matches enum celltype
openxlsx2_celltype <- c(
short_date = 0,
diff --git a/R/wb_functions.R b/R/wb_functions.R
index a15299209..836dcb328 100644
--- a/R/wb_functions.R
+++ b/R/wb_functions.R
@@ -7,7 +7,6 @@
#' }
#' @export
dims_to_dataframe <- function(dims, fill = FALSE) {
-
if (grepl(";", dims)) {
dims <- unlist(strsplit(dims, ";"))
}
@@ -311,7 +310,7 @@ wb_to_df <- function(
rows = NULL,
cols = NULL,
detectDates = TRUE,
- na.strings = "#N/A",
+ na.strings = getOption("openxlsx2.na.strings", "#N/A"),
na.numbers = NA,
fillMergedCells = FALSE,
dims,
diff --git a/R/write.R b/R/write.R
index 505ac474b..ece037969 100644
--- a/R/write.R
+++ b/R/write.R
@@ -8,13 +8,19 @@
#' @param cell the cell you want to update in Excel connotation e.g. "A1"
#' @param colNames if TRUE colNames are passed down
#' @param removeCellStyle keep the cell style?
-#' @param na.strings optional na.strings argument. if missing #N/A is used. If NULL no cell value is written, if character or numeric this is written (even if NA is part of numeric data)
+#' @param na.strings na.strings argument. If NULL no cell value is written, if character or numeric this is written (even if NA is part of numeric data)
#'
#' @keywords internal
#' @noRd
-update_cell <- function(x, wb, sheet, cell, colNames = FALSE,
- removeCellStyle = FALSE, na.strings) {
-
+update_cell <- function(
+ x,
+ wb,
+ sheet,
+ cell,
+ colNames = FALSE,
+ removeCellStyle = FALSE,
+ na.strings = getOption("openxlsx2.na.strings", "#N/A")
+ ) {
sheet_id <- wb$validate_sheet(sheet)
dims <- dims_to_dataframe(cell, fill = TRUE)
@@ -82,10 +88,6 @@ update_cell <- function(x, wb, sheet, cell, colNames = FALSE,
wb$worksheets[[sheet_id]]$dimension <- paste0("")
}
- if (missing(na.strings)) {
- na.strings <- NULL
- }
-
if (removeCellStyle) {
cell_style <- "c_s"
} else {
@@ -133,7 +135,7 @@ nmfmt_df <- function(x) {
#' @param startCol col to place it
#' @param applyCellStyle apply styles when writing on the sheet
#' @param removeCellStyle keep the cell style?
-#' @param na.strings optional na.strings argument. if missing #N/A is used. If NULL no cell value is written, if character or numeric this is written (even if NA is part of numeric data)
+#' @param na.strings na.strings argument. If NULL no cell value is written, if character or numeric this is written (even if NA is part of numeric data)
#' @param data_table logical. if `TRUE` and `rowNames = TRUE`, do not write the cell containing `"_rowNames_"`
#' @details
#' The string `"_openxlsx_NA"` is reserved for `openxlsx2`. If the data frame
@@ -160,19 +162,17 @@ write_data2 <- function(
wb,
sheet,
data,
- name = NULL,
- colNames = TRUE,
- rowNames = FALSE,
- startRow = 1,
- startCol = 1,
- applyCellStyle = TRUE,
+ name = NULL,
+ colNames = TRUE,
+ rowNames = FALSE,
+ startRow = 1,
+ startCol = 1,
+ applyCellStyle = TRUE,
removeCellStyle = FALSE,
- na.strings,
- data_table = FALSE
+ na.strings = getOption("openxlsx2.na.strings", "#N/A"),
+ data_table = FALSE
) {
- if (missing(na.strings)) na.strings <- substitute()
-
is_data_frame <- FALSE
#### prepare the correct data formats for openxml
dc <- openxlsx2_type(data)
@@ -576,32 +576,32 @@ write_data2 <- function(
#' @param name If not NULL, a named region is defined.
#' @param applyCellStyle apply styles when writing on the sheet
#' @param removeCellStyle if writing into existing cells, should the cell style be removed?
-#' @param na.strings optional na.strings argument. if missing #N/A is used. If NULL no cell value is written, if character or numeric this is written (even if NA is part of numeric data)
+#' @param na.strings na.strings argument. If NULL no cell value is written, if character or numeric this is written (even if NA is part of numeric data)
#' @noRd
write_data_table <- function(
wb,
sheet,
x,
- startCol = 1,
- startRow = 1,
+ startCol = 1,
+ startRow = 1,
dims,
- array = FALSE,
- xy = NULL,
- colNames = TRUE,
- rowNames = FALSE,
- tableStyle = "TableStyleLight9",
- tableName = NULL,
- withFilter = TRUE,
- sep = ", ",
- firstColumn = FALSE,
- lastColumn = FALSE,
- bandedRows = TRUE,
- bandedCols = FALSE,
- name = NULL,
- applyCellStyle = TRUE,
+ array = FALSE,
+ xy = NULL,
+ colNames = TRUE,
+ rowNames = FALSE,
+ tableStyle = "TableStyleLight9",
+ tableName = NULL,
+ withFilter = TRUE,
+ sep = ", ",
+ firstColumn = FALSE,
+ lastColumn = FALSE,
+ bandedRows = TRUE,
+ bandedCols = FALSE,
+ name = NULL,
+ applyCellStyle = TRUE,
removeCellStyle = FALSE,
- data_table = FALSE,
- na.strings
+ data_table = FALSE,
+ na.strings = getOption("openxlsx2.na.strings", "#N/A")
) {
op <- openxlsx2_options()
@@ -625,8 +625,6 @@ write_data_table <- function(
startRow <- min(dims[[2]])
}
- if (missing(na.strings)) na.strings <- substitute()
-
## common part ---------------------------------------------------------------
if ((!is.character(sep)) || (length(sep) != 1))
stop("sep must be a character vector of length 1")
@@ -853,7 +851,7 @@ write_data_table <- function(
#' @param name If not NULL, a named region is defined.
#' @param applyCellStyle apply styles when writing on the sheet
#' @param removeCellStyle if writing into existing cells, should the cell style be removed?
-#' @param na.strings optional na.strings argument. if missing #N/A is used. If NULL no cell value is written, if character or numeric this is written (even if NA is part of numeric data)
+#' @param na.strings na.strings argument. If NULL no cell value is written, if character or numeric this is written (even if NA is part of numeric data)
#' @seealso [write_datatable()]
#' @export write_data
#' @details Formulae written using write_formula to a Workbook object will not get picked up by read_xlsx().
@@ -918,47 +916,45 @@ write_data <- function(
wb,
sheet,
x,
- startCol = 1,
- startRow = 1,
- dims = rowcol_to_dims(startRow, startCol),
- array = FALSE,
- xy = NULL,
- colNames = TRUE,
- rowNames = FALSE,
- withFilter = FALSE,
- sep = ", ",
- name = NULL,
- applyCellStyle = TRUE,
+ startCol = 1,
+ startRow = 1,
+ dims = rowcol_to_dims(startRow, startCol),
+ array = FALSE,
+ xy = NULL,
+ colNames = TRUE,
+ rowNames = FALSE,
+ withFilter = FALSE,
+ sep = ", ",
+ name = NULL,
+ applyCellStyle = TRUE,
removeCellStyle = FALSE,
- na.strings
+ na.strings = getOption("openxlsx2.na.strings", "#N/A")
) {
- if (missing(na.strings)) na.strings <- substitute()
-
write_data_table(
- wb = wb,
- sheet = sheet,
- x = x,
- startCol = startCol,
- startRow = startRow,
- dims = dims,
- array = array,
- xy = xy,
- colNames = colNames,
- rowNames = rowNames,
- tableStyle = NULL,
- tableName = NULL,
- withFilter = withFilter,
- sep = sep,
- firstColumn = FALSE,
- lastColumn = FALSE,
- bandedRows = FALSE,
- bandedCols = FALSE,
- name = name,
- applyCellStyle = applyCellStyle,
+ wb = wb,
+ sheet = sheet,
+ x = x,
+ startCol = startCol,
+ startRow = startRow,
+ dims = dims,
+ array = array,
+ xy = xy,
+ colNames = colNames,
+ rowNames = rowNames,
+ tableStyle = NULL,
+ tableName = NULL,
+ withFilter = withFilter,
+ sep = sep,
+ firstColumn = FALSE,
+ lastColumn = FALSE,
+ bandedRows = FALSE,
+ bandedCols = FALSE,
+ name = name,
+ applyCellStyle = applyCellStyle,
removeCellStyle = removeCellStyle,
- data_table = FALSE,
- na.strings = na.strings
+ data_table = FALSE,
+ na.strings = na.strings
)
}
@@ -1058,12 +1054,12 @@ write_formula <- function(
wb,
sheet,
x,
- startCol = 1,
- startRow = 1,
- dims = rowcol_to_dims(startRow, startCol),
- array = FALSE,
- xy = NULL,
- applyCellStyle = TRUE,
+ startCol = 1,
+ startRow = 1,
+ dims = rowcol_to_dims(startRow, startCol),
+ array = FALSE,
+ xy = NULL,
+ applyCellStyle = TRUE,
removeCellStyle = FALSE
) {
assert_class(x, "character")
@@ -1078,17 +1074,17 @@ write_formula <- function(
}
write_data(
- wb = wb,
- sheet = sheet,
- x = dfx,
- startCol = startCol,
- startRow = startRow,
- dims = dims,
- array = array,
- xy = xy,
- colNames = FALSE,
- rowNames = FALSE,
- applyCellStyle = applyCellStyle,
+ wb = wb,
+ sheet = sheet,
+ x = dfx,
+ startCol = startCol,
+ startRow = startRow,
+ dims = dims,
+ array = array,
+ xy = xy,
+ colNames = FALSE,
+ rowNames = FALSE,
+ applyCellStyle = applyCellStyle,
removeCellStyle = removeCellStyle
)
}
@@ -1123,7 +1119,7 @@ write_formula <- function(
#' @param bandedCols logical. If TRUE, the columns are colour banded
#' @param applyCellStyle apply styles when writing on the sheet
#' @param removeCellStyle if writing into existing cells, should the cell style be removed?
-#' @param na.strings optional na.strings argument. if missing #N/A is used. If NULL no cell value is written, if character or numeric this is written (even if NA is part of numeric data)
+#' @param na.strings na.strings argument. If NULL no cell value is written, if character or numeric this is written (even if NA is part of numeric data)
#' @details columns of x with class Date/POSIXt, currency, accounting,
#' hyperlink, percentage are automatically styled as dates, currency, accounting,
#' hyperlinks, percentages respectively.
@@ -1220,50 +1216,47 @@ write_datatable <- function(
wb,
sheet,
x,
- startCol = 1,
- startRow = 1,
- dims = rowcol_to_dims(startRow, startCol),
- xy = NULL,
- colNames = TRUE,
- rowNames = FALSE,
- tableStyle = "TableStyleLight9",
- tableName = NULL,
- withFilter = TRUE,
- sep = ", ",
- firstColumn = FALSE,
- lastColumn = FALSE,
- bandedRows = TRUE,
- bandedCols = FALSE,
- applyCellStyle = TRUE,
+ startCol = 1,
+ startRow = 1,
+ dims = rowcol_to_dims(startRow, startCol),
+ xy = NULL,
+ colNames = TRUE,
+ rowNames = FALSE,
+ tableStyle = "TableStyleLight9",
+ tableName = NULL,
+ withFilter = TRUE,
+ sep = ", ",
+ firstColumn = FALSE,
+ lastColumn = FALSE,
+ bandedRows = TRUE,
+ bandedCols = FALSE,
+ applyCellStyle = TRUE,
removeCellStyle = FALSE,
- na.strings
+ na.strings = getOption("openxlsx2.na.strings", "#N/A")
) {
-
- if (missing(na.strings)) na.strings <- substitute()
-
write_data_table(
- wb = wb,
- sheet = sheet,
- x = x,
- startCol = startCol,
- startRow = startRow,
- dims = dims,
- array = FALSE,
- xy = xy,
- colNames = colNames,
- rowNames = rowNames,
- tableStyle = tableStyle,
- tableName = tableName,
- withFilter = withFilter,
- sep = sep,
- firstColumn = firstColumn,
- lastColumn = lastColumn,
- bandedRows = bandedRows,
- bandedCols = bandedCols,
- name = NULL,
- data_table = TRUE,
- applyCellStyle = applyCellStyle,
+ wb = wb,
+ sheet = sheet,
+ x = x,
+ startCol = startCol,
+ startRow = startRow,
+ dims = dims,
+ array = FALSE,
+ xy = xy,
+ colNames = colNames,
+ rowNames = rowNames,
+ tableStyle = tableStyle,
+ tableName = tableName,
+ withFilter = withFilter,
+ sep = sep,
+ firstColumn = firstColumn,
+ lastColumn = lastColumn,
+ bandedRows = bandedRows,
+ bandedCols = bandedCols,
+ name = NULL,
+ data_table = TRUE,
+ applyCellStyle = applyCellStyle,
removeCellStyle = removeCellStyle,
- na.strings = na.strings
+ na.strings = na.strings
)
}
diff --git a/man/openxlsx2.Rd b/man/openxlsx2.Rd
index 2159aacd5..32bad49c6 100644
--- a/man/openxlsx2.Rd
+++ b/man/openxlsx2.Rd
@@ -1,6 +1,6 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/openxlsx2.R
-\docType{package}
+\docType{packages}
\name{openxlsx2}
\alias{openxlsx2}
\title{xlsx reading, writing and editing.}
diff --git a/man/wbWorkbook.Rd b/man/wbWorkbook.Rd
index 7c739438b..5e8df1d0a 100644
--- a/man/wbWorkbook.Rd
+++ b/man/wbWorkbook.Rd
@@ -554,7 +554,7 @@ add data
sep = ", ",
applyCellStyle = TRUE,
removeCellStyle = FALSE,
- na.strings
+ na.strings = getOption("openxlsx2.na.strings", "#N/A")
)}\if{html}{\out{}}
}
@@ -621,7 +621,7 @@ add a data table
bandedCols = FALSE,
applyCellStyle = TRUE,
removeCellStyle = FALSE,
- na.strings
+ na.strings = getOption("openxlsx2.na.strings", "#N/A")
)}\if{html}{\out{}}
}
@@ -718,7 +718,7 @@ add formula
\subsection{Method \code{add_style()}}{
add style
\subsection{Usage}{
-\if{html}{\out{}}\preformatted{wbWorkbook$add_style(style = NULL, style_name = NULL)}\if{html}{\out{
}}
+\if{html}{\out{}}\preformatted{wbWorkbook$add_style(style = NULL, style_name = substitute(style))}\if{html}{\out{
}}
}
\subsection{Arguments}{
diff --git a/man/wb_add_data_table.Rd b/man/wb_add_data_table.Rd
index 95f2a6d03..f22407bc3 100644
--- a/man/wb_add_data_table.Rd
+++ b/man/wb_add_data_table.Rd
@@ -24,7 +24,7 @@ wb_add_data_table(
bandedCols = FALSE,
applyCellStyle = TRUE,
removeCellStyle = FALSE,
- na.strings
+ na.strings = getOption("openxlsx2.na.strings", "#N/A")
)
}
\arguments{
diff --git a/man/wb_add_style.Rd b/man/wb_add_style.Rd
index e1f61572a..a0e57d14f 100644
--- a/man/wb_add_style.Rd
+++ b/man/wb_add_style.Rd
@@ -4,7 +4,7 @@
\alias{wb_add_style}
\title{add style to workbook}
\usage{
-wb_add_style(wb, style = NULL, style_name = NULL)
+wb_add_style(wb, style = NULL, style_name = substitute(style))
}
\arguments{
\item{wb}{workbook}
diff --git a/man/wb_to_df.Rd b/man/wb_to_df.Rd
index 0ae5d424c..ff4208802 100644
--- a/man/wb_to_df.Rd
+++ b/man/wb_to_df.Rd
@@ -16,7 +16,7 @@ wb_to_df(
rows = NULL,
cols = NULL,
detectDates = TRUE,
- na.strings = "#N/A",
+ na.strings = getOption("openxlsx2.na.strings", "#N/A"),
na.numbers = NA,
fillMergedCells = FALSE,
dims,
diff --git a/man/write_data.Rd b/man/write_data.Rd
index 9f01332b1..e37a5d079 100644
--- a/man/write_data.Rd
+++ b/man/write_data.Rd
@@ -21,7 +21,7 @@ wb_add_data(
sep = ", ",
applyCellStyle = TRUE,
removeCellStyle = FALSE,
- na.strings
+ na.strings = getOption("openxlsx2.na.strings", "#N/A")
)
write_data(
@@ -40,7 +40,7 @@ write_data(
name = NULL,
applyCellStyle = TRUE,
removeCellStyle = FALSE,
- na.strings
+ na.strings = getOption("openxlsx2.na.strings", "#N/A")
)
}
\arguments{
@@ -76,7 +76,7 @@ write_data(
\item{removeCellStyle}{if writing into existing cells, should the cell style be removed?}
-\item{na.strings}{optional na.strings argument. if missing #N/A is used. If NULL no cell value is written, if character or numeric this is written (even if NA is part of numeric data)}
+\item{na.strings}{na.strings argument. If NULL no cell value is written, if character or numeric this is written (even if NA is part of numeric data)}
}
\value{
A clone of `wb``
diff --git a/man/write_data2.Rd b/man/write_data2.Rd
index ab3d4d7d3..f90d4eb0d 100644
--- a/man/write_data2.Rd
+++ b/man/write_data2.Rd
@@ -15,7 +15,7 @@ write_data2(
startCol = 1,
applyCellStyle = TRUE,
removeCellStyle = FALSE,
- na.strings,
+ na.strings = getOption("openxlsx2.na.strings", "#N/A"),
data_table = FALSE
)
}
@@ -40,7 +40,7 @@ write_data2(
\item{removeCellStyle}{keep the cell style?}
-\item{na.strings}{optional na.strings argument. if missing #N/A is used. If NULL no cell value is written, if character or numeric this is written (even if NA is part of numeric data)}
+\item{na.strings}{na.strings argument. If NULL no cell value is written, if character or numeric this is written (even if NA is part of numeric data)}
\item{data_table}{logical. if \code{TRUE} and \code{rowNames = TRUE}, do not write the cell containing \code{"_rowNames_"}}
}
diff --git a/man/write_datatable.Rd b/man/write_datatable.Rd
index 02eeddb8f..3984101cc 100644
--- a/man/write_datatable.Rd
+++ b/man/write_datatable.Rd
@@ -24,7 +24,7 @@ write_datatable(
bandedCols = FALSE,
applyCellStyle = TRUE,
removeCellStyle = FALSE,
- na.strings
+ na.strings = getOption("openxlsx2.na.strings", "#N/A")
)
}
\arguments{
@@ -72,7 +72,7 @@ A vector of the form c(startCol, startRow)}
\item{removeCellStyle}{if writing into existing cells, should the cell style be removed?}
-\item{na.strings}{optional na.strings argument. if missing #N/A is used. If NULL no cell value is written, if character or numeric this is written (even if NA is part of numeric data)}
+\item{na.strings}{na.strings argument. If NULL no cell value is written, if character or numeric this is written (even if NA is part of numeric data)}
}
\description{
Write to a worksheet and format as an Excel table
diff --git a/tests/testthat/test-class-color.R b/tests/testthat/test-class-color.R
index fa41432bd..e34a83c92 100644
--- a/tests/testthat/test-class-color.R
+++ b/tests/testthat/test-class-color.R
@@ -12,6 +12,6 @@ test_that("get()", {
got <- wb_colour("black")
expect_true(is_wbColour(got))
- expect_equal(exp, got)
+ expect_equal(got, exp)
})
diff --git a/tests/testthat/test-class-comment.R b/tests/testthat/test-class-comment.R
index 1a41545c3..c339877d2 100644
--- a/tests/testthat/test-class-comment.R
+++ b/tests/testthat/test-class-comment.R
@@ -130,6 +130,6 @@ test_that("print comment", {
exp <- "Author: Marco Polo\nText:\n Marco Polo:\nthis is another comment\n\nStyle:\n\n\n\n\nFont name: Calibri\nFont size: 11\nFont colour: #000000\n\n"
got <- capture_output(print(c2), print = TRUE)
- expect_equal(exp, got)
+ expect_equal(got, exp)
})
diff --git a/tests/testthat/test-class-hyperlink.R b/tests/testthat/test-class-hyperlink.R
index 9b9664177..5c5b5692a 100644
--- a/tests/testthat/test-class-hyperlink.R
+++ b/tests/testthat/test-class-hyperlink.R
@@ -38,6 +38,6 @@ test_that("formulas with hyperlinks works", {
exp <- "=IF(2017 = VALUE(A1), HYPERLINK(\"github.com\",\"github.com\"), A1)"
got <- wb$worksheets[[1]]$sheet_data$cc["12", "f"]
- expect_equal(exp, got)
+ expect_equal(got, exp)
})
diff --git a/tests/testthat/test-class-workbook-wrappers.R b/tests/testthat/test-class-workbook-wrappers.R
index e3abdcbeb..3de0f0ec9 100644
--- a/tests/testthat/test-class-workbook-wrappers.R
+++ b/tests/testthat/test-class-workbook-wrappers.R
@@ -182,14 +182,10 @@ test_that("wb_add_plot() is a wrapper", {
})
test_that("wb_add_drawing is a wrapper", {
-
fl <- system.file("extdata", "loadExample.xlsx", package = "openxlsx2")
- wb <- wb_load(file = fl)
-
- xml <- wb$drawings[[2]]
-
- wb <- wb_workbook()$add_worksheet()
-
+ xml <- wb_load(file = fl)$drawings[[2]]
+ wb <- wb_workbook()
+ wb$add_worksheet()
expect_wrapper("add_drawing", wb = wb, params = list(xml = xml))
})
@@ -464,25 +460,16 @@ test_that("wb_add_sparklines() is a wrapper", {
# wb_add_style() ----------------------------------------------------------
test_that("wb_add_style() is a wrapper", {
-
# with name
style <- create_numfmt(numFmtId = "165", formatCode = "#.#")
wb <- wb_workbook()
- expect_wrapper(
- "add_style",
- wb = wb,
- params = list(style = style, style_name = "numfmt")
- )
+ params <- list(style = style, style_name = "numfmt")
+ expect_wrapper("add_style", wb = wb, params = params)
# without name
wb <- wb_workbook()
numfmt <- create_numfmt(numFmtId = "165", formatCode = "#.#")
- expect_wrapper(
- "add_style",
- wb = wb,
- params = list(style = numfmt)
- )
-
+ expect_wrapper("add_style", wb = wb, params = list(style = numfmt))
})
diff --git a/tests/testthat/test-class-workbook.R b/tests/testthat/test-class-workbook.R
index 0cb687418..8a57ce0ac 100644
--- a/tests/testthat/test-class-workbook.R
+++ b/tests/testthat/test-class-workbook.R
@@ -61,7 +61,7 @@ test_that("wb_set_col_widths", {
""
)
got <- wb$worksheets[[1]]$cols_attr
- expect_equal(exp, got)
+ expect_equal(got, exp)
})
@@ -108,7 +108,7 @@ test_that("$set_sheet_names() and $get_sheet_names() work", {
exp <- c(`Sheet 1` = "Sheet 1")
got <- wb$get_sheet_names()
- expect_equal(exp, got)
+ expect_equal(got, exp)
})
# data validation ---------------------------------------------------------
@@ -158,7 +158,7 @@ test_that("data validation", {
"79"
)
got <- wb$worksheets[[1]]$dataValidations
- expect_equal(exp, got)
+ expect_equal(got, exp)
exp <- c(
@@ -166,14 +166,14 @@ test_that("data validation", {
"42369.768518518542370.2314814815"
)
got <- wb$worksheets[[2]]$dataValidations
- expect_equal(exp, got)
+ expect_equal(got, exp)
exp <- c(
"'Sheet 4'!$A$1:$A$10"
)
got <- wb$worksheets[[3]]$dataValidations
- expect_equal(exp, got)
+ expect_equal(got, exp)
wb$save(temp)
@@ -206,7 +206,7 @@ test_that("data validation", {
"'Sheet 4'!$A$1:$A$10"
)
got <- wb2$worksheets[[3]]$dataValidations
- expect_equal(exp, got)
+ expect_equal(got, exp)
### tests if conditions
@@ -221,7 +221,7 @@ test_that("data validation", {
exp <- "19"
got <- wb$worksheets[[1]]$dataValidations
- expect_equal(exp, got)
+ expect_equal(got, exp)
# to many values
@@ -297,7 +297,7 @@ test_that("data validation", {
exp <- "0"
got <- wb$worksheets[[1]]$dataValidations
- expect_equal(exp, got)
+ expect_equal(got, exp)
# add custom data
wb <- wb_workbook()$
@@ -308,7 +308,7 @@ test_that("data validation", {
exp <- "A1=B1"
got <- wb$worksheets[[1]]$dataValidations
- expect_equal(exp, got)
+ expect_equal(got, exp)
})
@@ -337,7 +337,7 @@ test_that("clone worksheet", {
""
)
got <- wb$worksheets_rels[[5]]
- expect_equal(exp, got)
+ expect_equal(got, exp)
# wb$open()
# clone drawing ---------------------------------------------------------
@@ -394,7 +394,7 @@ test_that("set and remove row heights work", {
class = "data.frame"
)
got <- wb$worksheets[[1]]$sheet_data$row_attr[c(1, 2, 4, 19, 22), c("customHeight", "ht", "r")]
- expect_equal(exp, got)
+ expect_equal(got, exp)
## remove row heights
wb$remove_row_heights(rows = 1:21)
@@ -408,7 +408,7 @@ test_that("set and remove row heights work", {
class = "data.frame"
)
got <- wb$worksheets[[1]]$sheet_data$row_attr[c(1, 2, 4, 19, 22), c("customHeight", "ht", "r")]
- expect_equal(exp, got)
+ expect_equal(got, exp)
expect_warning(
wb$add_worksheet()$remove_row_heights(rows = 1:3),
@@ -532,7 +532,7 @@ test_that("add_drawing works", {
""
)
got <- wb$drawings_rels
- expect_equal(exp, got)
+ expect_equal(got, exp)
# write data starting at B2
@@ -551,6 +551,6 @@ test_that("add_drawing works", {
character(0)
)
got <- wb$worksheets_rels
- expect_equal(exp, got)
+ expect_equal(got, exp)
})
diff --git a/tests/testthat/test-class-worksheet.R b/tests/testthat/test-class-worksheet.R
index 5831338c3..bd1abfe47 100644
--- a/tests/testthat/test-class-worksheet.R
+++ b/tests/testthat/test-class-worksheet.R
@@ -23,7 +23,7 @@ test_that("test data validation list and sparklines", {
"'Sheet 1'!A3:K3L3'Sheet 1'!A4:K4L4"
)
got <- wb$worksheets[[1]]$extLst
- expect_equal(exp, got)
+ expect_equal(got, exp)
})
@@ -64,7 +64,7 @@ test_that("set_sheetview", {
exp <- ""
got <- wb$worksheets[[1]]$sheetViews
- expect_equal(exp, got)
+ expect_equal(got, exp)
exp <- ""
@@ -72,14 +72,14 @@ test_that("set_sheetview", {
wb <- wb_workbook()$add_worksheet()
got <- wb$worksheets[[1]]$sheetViews
- expect_equal(exp, got)
+ expect_equal(got, exp)
options("openxlsx2.rightToLeft" = "1")
wb <- wb_workbook()$add_worksheet()
got <- wb$worksheets[[1]]$sheetViews
- expect_equal(exp, got)
+ expect_equal(got, exp)
})
@@ -95,16 +95,16 @@ test_that("print options work", {
exp <- character()
got <- wb$worksheets[[1]]$printOptions
- expect_equal(exp, got)
+ expect_equal(got, exp)
exp <- ""
got <- wb$worksheets[[2]]$printOptions
- expect_equal(exp, got)
+ expect_equal(got, exp)
wb$save(temp)
wb <- wb_load(temp)
got <- wb$worksheets[[2]]$printOptions
- expect_equal(exp, got)
+ expect_equal(got, exp)
})
diff --git a/tests/testthat/test-conditional_formatting.R b/tests/testthat/test-conditional_formatting.R
index ee0aa7dc8..9cb2c40e0 100644
--- a/tests/testthat/test-conditional_formatting.R
+++ b/tests/testthat/test-conditional_formatting.R
@@ -561,7 +561,7 @@ test_that("extend dataBar tests", {
`K1:K11` = "{F7189283-14F7-4DE0-9601-54DE9DB40005}"
)
got <- wb$worksheets[[1]]$conditionalFormatting
- expect_equal(exp, got)
+ expect_equal(got, exp)
exp <- read_xml("
@@ -573,7 +573,7 @@ test_that("extend dataBar tests", {
05K1:K11
", pointer = FALSE)
got <- wb$worksheets[[1]]$extLst
- expect_equal(exp, got)
+ expect_equal(got, exp)
})
@@ -592,7 +592,7 @@ test_that("wb_conditional_formatting", {
exp <- c(`A1:A11` = "{F7189283-14F7-4DE0-9601-54DE9DB40000}")
got <- wb$worksheets[[1]]$conditionalFormatting
- expect_equal(exp, got)
+ expect_equal(got, exp)
})
test_that("create dxfs style without font family and size", {
@@ -600,7 +600,7 @@ test_that("create dxfs style without font family and size", {
# a workbook with this style loads, but has no highlighting
exp <- ""
got <- create_dxfs_style()
- expect_equal(exp, got)
+ expect_equal(got, exp)
# most likely what the user expects. change font color and background color
exp <- ""
@@ -608,7 +608,7 @@ test_that("create dxfs style without font family and size", {
font_color = wb_colour(hex = "FF9C0006"),
bgFill = wb_colour(hex = "FFFFC7CE")
)
- expect_equal(exp, got)
+ expect_equal(got, exp)
# the fully fletched old default dxfs style
exp <- ""
@@ -618,6 +618,6 @@ test_that("create dxfs style without font family and size", {
font_color = wb_colour(hex = "FF9C0006"),
bgFill = wb_colour(hex = "FFFFC7CE")
)
- expect_equal(exp, got)
+ expect_equal(got, exp)
})
diff --git a/tests/testthat/test-converters.R b/tests/testthat/test-converters.R
index c5bf455eb..ca56961a0 100644
--- a/tests/testthat/test-converters.R
+++ b/tests/testthat/test-converters.R
@@ -2,7 +2,7 @@ test_that("int2col", {
exp <- LETTERS[1:10]
got <- int2col(1:10)
- expect_equal(exp, got)
+ expect_equal(got, exp)
expect_error(int2col("a"),
"x must be numeric.")
@@ -21,7 +21,7 @@ test_that("get_cell_refs", {
exp <- c("B1", "C2", "D3")
got <- get_cell_refs(data.frame(1:3, 2:4))
- expect_equal(exp, got)
+ expect_equal(got, exp)
expect_error(get_cell_refs(data.frame("a", "a")),
"cellCoords must only contain integers")
diff --git a/tests/testthat/test-helper_functions.R b/tests/testthat/test-helper_functions.R
index 4ff402c7d..13f9fe8e1 100644
--- a/tests/testthat/test-helper_functions.R
+++ b/tests/testthat/test-helper_functions.R
@@ -44,7 +44,7 @@ test_that("openxlsx2_types", {
)
- expect_equal(exp, got)
+ expect_equal(got, exp)
})
@@ -109,11 +109,11 @@ test_that("amp_split & genHeaderFooterNode", {
oddFooter = c("", "&"Times New Roman,Standard"&12Seite &P", "")
)
got <- getHeaderFooterNode(xml)
- expect_equal(exp, got)
+ expect_equal(got, exp)
exp <- xml
got <- genHeaderFooterNode(got)
- expect_equal(exp, got)
+ expect_equal(got, exp)
})
@@ -191,7 +191,7 @@ test_that("add_sparklines", {
', pointer = FALSE)
got <- wb$worksheets[[1]]$extLst
- expect_equal(exp, got)
+ expect_equal(got, exp)
expect_error(
wb$add_sparklines(sparklines = xml_node_create("sparklines", sparklines)),
diff --git a/tests/testthat/test-illegal-characters.R b/tests/testthat/test-illegal-characters.R
index 19cec1389..559e1d871 100644
--- a/tests/testthat/test-illegal-characters.R
+++ b/tests/testthat/test-illegal-characters.R
@@ -2,10 +2,10 @@ test_that("clean_worksheet_name", {
exp <- "test that"
got <- clean_worksheet_name("test\that")
- expect_equal(exp, got)
+ expect_equal(got, exp)
exp <- c("a b", "a b", "a b", "a b", "a b", "a b", "a b")
got <- clean_worksheet_name(c("a\b", "a/b", "a?b", "a*b", "a:b", "a[b", "a]b"))
- expect_equal(exp, got)
+ expect_equal(got, exp)
})
diff --git a/tests/testthat/test-loading_workbook.R b/tests/testthat/test-loading_workbook.R
index 1b8708f13..c744d65c3 100644
--- a/tests/testthat/test-loading_workbook.R
+++ b/tests/testthat/test-loading_workbook.R
@@ -175,13 +175,13 @@ test_that("additional wb tests", {
# showFormula
exp <- data.frame(Var7 = "1/0", row.names = "2")
got <- wb_to_df(wb1, showFormula = TRUE, rows = 1:2, cols = 8)
- expect_equal(exp, got, ignore_attr = TRUE)
+ expect_equal(got, exp, ignore_attr = TRUE)
expect_equal(names(exp), names(got))
# detectDates
exp <- data.frame(Var5 = as.Date("2015-02-07"), row.names = "2")
got <- wb_to_df(wb1, showFormula = TRUE, rows = 1:2, cols = 6)
- expect_equal(exp, got, ignore_attr = TRUE)
+ expect_equal(got, exp, ignore_attr = TRUE)
expect_equal(names(exp), names(got))
# types
@@ -190,7 +190,7 @@ test_that("additional wb tests", {
Var3 = c(1.00, NaN, 1.34, NA))
got <- wb_to_df(wb1, cols = c(1, 4),
types = c("Var1" = 0, "Var3" = 1))[seq_len(4), ]
- expect_equal(exp, got, ignore_attr = TRUE)
+ expect_equal(got, exp, ignore_attr = TRUE)
expect_equal(names(exp), names(got))
})
@@ -307,7 +307,7 @@ test_that("loading slicers works", {
exp <- ""
got <- wb$workbook$calcPr
- expect_equal(exp, got)
+ expect_equal(got, exp)
exp <- c(
"",
@@ -323,7 +323,7 @@ test_that("loading slicers works", {
""
)
got <- wb$workbook.xml.rels
- expect_equal(exp, got)
+ expect_equal(got, exp)
wb <- wb_load(file = fl, calc_chain = FALSE)
got <- wb$workbook.xml.rels
@@ -335,7 +335,7 @@ test_that("loading slicers works", {
exp <- ""
got <- wb$workbook$calcPr
- expect_equal(exp, got)
+ expect_equal(got, exp)
wb <- wb_load(file = fl, calc_chain = FALSE)
@@ -344,7 +344,7 @@ test_that("loading slicers works", {
exp <- character()
got <- wb$calcChain
- expect_equal(exp, got)
+ expect_equal(got, exp)
# check the default once again
wb <- wb_load(file = fl)
@@ -354,7 +354,7 @@ test_that("loading slicers works", {
exp <- character()
got <- wb$calcChain
- expect_equal(exp, got)
+ expect_equal(got, exp)
})
@@ -365,6 +365,6 @@ test_that("vml target is updated on load", {
exp <- ""
got <- wb$worksheets_rels[[4]][2]
- expect_equal(exp, got)
+ expect_equal(got, exp)
})
diff --git a/tests/testthat/test-options.R b/tests/testthat/test-options.R
index 21a71f4c4..13e1895b2 100644
--- a/tests/testthat/test-options.R
+++ b/tests/testthat/test-options.R
@@ -4,7 +4,7 @@ test_that("sheet.default_name", {
exp <- "Sheet 1"
got <- names(wb$get_sheet_names())
- expect_equal(exp, got)
+ expect_equal(got, exp)
op <- options("openxlsx2.sheet.default_name" = "Tabelle")
on.exit(options(op), add = TRUE)
@@ -12,6 +12,6 @@ test_that("sheet.default_name", {
exp <- "Tabelle1"
got <- names(wb$get_sheet_names())
- expect_equal(exp, got)
+ expect_equal(got, exp)
})
diff --git a/tests/testthat/test-pugi_cpp.R b/tests/testthat/test-pugi_cpp.R
index 5e106d6cc..db4bdff36 100644
--- a/tests/testthat/test-pugi_cpp.R
+++ b/tests/testthat/test-pugi_cpp.R
@@ -178,7 +178,7 @@ test_that("getXMLPtr1con", {
xml <- ""
exp <- c("", "", "")
got <- getXMLPtr1con(read_xml(xml))
- expect_equal(exp, got)
+ expect_equal(got, exp)
})
diff --git a/tests/testthat/test-pugixml.R b/tests/testthat/test-pugixml.R
index eb1a94c42..0e50f679e 100644
--- a/tests/testthat/test-pugixml.R
+++ b/tests/testthat/test-pugixml.R
@@ -3,11 +3,11 @@ test_that("read_xml", {
exp <- ""
got <- read_xml("", empty_tags = FALSE, pointer = FALSE)
- expect_equal(exp, got)
+ expect_equal(got, exp)
exp <- ""
got <- read_xml("", empty_tags = TRUE, pointer = FALSE)
- expect_equal(exp, got)
+ expect_equal(got, exp)
# a pointer
x <- read_xml("")
@@ -65,25 +65,25 @@ test_that("read_xml", {
exp <- " "
got <- readLines(tmp, warn = FALSE)
- expect_equal(exp, got)
+ expect_equal(got, exp)
xml <- ''
exp <- c("", "")
got <- xml_node(xml, "a")
- expect_equal(exp, got)
+ expect_equal(got, exp)
exp <- c("", "")
got <- xml_node(xml, "a", "b")
- expect_equal(exp, got)
+ expect_equal(got, exp)
exp <- ""
got <- xml_node(xml, "a", "b", "c1")
- expect_equal(exp, got)
+ expect_equal(got, exp)
exp <- ""
got <- xml_node(xml, "a", "b", "c2")
- expect_equal(exp, got)
+ expect_equal(got, exp)
})
@@ -275,31 +275,31 @@ test_that("xml_rm_child", {
exp <- "2"
got <- xml_rm_child(xml_node, "b", which = 1)
- expect_equal(exp, got)
+ expect_equal(got, exp)
xml_node <- exp
exp <- "2"
got <- xml_rm_child(xml_node, xml_child, "b", which = 1)
- expect_equal(exp, got)
+ expect_equal(got, exp)
exp <- "2"
got <- xml_rm_child(xml_node, xml_child, level = "b", which = 2)
- expect_equal(exp, got)
+ expect_equal(got, exp)
exp <- "2"
got <- xml_rm_child(xml_node, xml_child, "b", which = 0)
- expect_equal(exp, got)
+ expect_equal(got, exp)
xml_node <- "123"
exp <- "13"
got <- xml_rm_child(xml_node, xml_child, level = c("a", "b"), which = 2)
- expect_equal(exp, got)
+ expect_equal(got, exp)
exp <- ""
got <- xml_rm_child(xml_node, xml_child, level = c("a", "b"), which = 0)
- expect_equal(exp, got)
+ expect_equal(got, exp)
})
@@ -368,13 +368,13 @@ test_that("works with x namespace", {
exp <- ""
got <- read_xml(tmp, pointer = FALSE)
- expect_equal(exp, got)
+ expect_equal(got, exp)
op <- options("openxlsx2.namespace_xml" = "x")
on.exit(options(op), add = TRUE)
exp <- ""
got <- read_xml(tmp, pointer = FALSE)
- expect_equal(exp, got)
+ expect_equal(got, exp)
})
diff --git a/tests/testthat/test-read_sources.R b/tests/testthat/test-read_sources.R
index 564c955a2..e30edebea 100644
--- a/tests/testthat/test-read_sources.R
+++ b/tests/testthat/test-read_sources.R
@@ -122,19 +122,19 @@ test_that("encoding", {
)
}
- expect_equal(exp, got)
+ expect_equal(got, exp)
exp <- "\n äöüß\n ÄÖÜ\n €\n"
got <- paste(capture.output(
read_xml(system.file("extdata", "unicode.xml", package = "openxlsx2"))
), collapse = "\n")
- expect_equal(exp, got)
+ expect_equal(got, exp)
exp <- "äöüßÄÖÜ€"
got <- read_xml(system.file("extdata", "unicode.xml", package = "openxlsx2"),
pointer = FALSE)
- expect_equal(exp, got)
+ expect_equal(got, exp)
})
@@ -148,7 +148,7 @@ test_that("reading charts", {
exp <- c("", "", "", "", "", "", "", "", "", "", "", "", "", "", "")
got <- wb$charts$rels
- expect_equal(exp, got)
+ expect_equal(got, exp)
img <- system.file("extdata", "einstein.jpg", package = "openxlsx2")
@@ -175,7 +175,7 @@ test_that("reading charts", {
""
)
got <- wb$drawings_rels[[20]]
- expect_equal(exp, got)
+ expect_equal(got, exp)
wb$add_worksheet()
@@ -218,11 +218,11 @@ test_that("reading file with macro and custom xml", {
exp <- ""
got <- wb$worksheets[[1]]$sheetPr
- expect_equal(exp, got)
+ expect_equal(got, exp)
exp <- "openxlsx2"
got <- wb$custom
- expect_equal(exp, got)
+ expect_equal(got, exp)
})
test_that("load file with connection", {
@@ -259,7 +259,7 @@ test_that("calcChain is updated", {
exp <- ""
got <- wb$calcChain
- expect_equal(exp, got)
+ expect_equal(got, exp)
expect_silent(wb$save(temp))
@@ -267,7 +267,7 @@ test_that("calcChain is updated", {
exp <- character()
got <- wb$calcChain
- expect_equal(exp, got)
+ expect_equal(got, exp)
expect_silent(wb$save(temp))
@@ -304,12 +304,12 @@ test_that("reading of formControl works", {
""
)
got <- wb$ctrlProps
- expect_equal(exp, got)
+ expect_equal(got, exp)
wb$save(temp)
wb <- wb_load(temp)
got <- wb$ctrlProps
- expect_equal(exp, got)
+ expect_equal(got, exp)
})
diff --git a/tests/testthat/test-save.R b/tests/testthat/test-save.R
index 6097b8a2e..03f29714e 100644
--- a/tests/testthat/test-save.R
+++ b/tests/testthat/test-save.R
@@ -186,11 +186,11 @@ test_that("writing NA, NaN and Inf", {
# we wont get the same input back
exp <- c(NA_character_, "#NUM!", "#NUM!", "#VALUE!")
got <- unname(unlist(wb_to_df(tmp)))
- expect_equal(exp, got)
+ expect_equal(got, exp)
wb$clone_worksheet(old = "Test1", new = "Clone1")$add_data(x = x)$save(tmp)
got <- unname(unlist(wb_to_df(tmp, "Clone1")))
- expect_equal(exp, got)
+ expect_equal(got, exp)
# distinguish between "NA" and NA_character_
x <- data.frame(x = c(NA, "NA"))
@@ -198,11 +198,11 @@ test_that("writing NA, NaN and Inf", {
exp <- c(NA_character_, "NA")
got <- unname(unlist(wb_to_df(tmp, "Test2")))
- expect_equal(exp, got)
+ expect_equal(got, exp)
wb$clone_worksheet(old = "Test2", new = "Clone2")$add_data(x = x)$save(tmp)
got <- unname(unlist(wb_to_df(tmp, "Clone2")))
- expect_equal(exp, got)
+ expect_equal(got, exp)
})
@@ -219,35 +219,34 @@ test_that("writing NA, NaN and Inf", {
exp <- c(NA, "s", "s", "s")
got <- unname(unlist(attr(wb_to_df(tmp, "Test1"), "tt")))
- expect_equal(exp, got)
+ expect_equal(got, exp)
exp <- c("N/A", "#NUM!", "#NUM!", "#VALUE!")
got <- unname(unlist(wb_to_df(tmp, "Test2")))
- expect_equal(exp, got)
+ expect_equal(got, exp)
wb$clone_worksheet("Test1", "Clone1")$add_data(x = x, na.strings = NULL)$save(tmp)
wb$clone_worksheet("Test3", "Clone3")$add_data(x = x, na.strings = "N/A")$save(tmp)
exp <- c(NA, "s", "s", "s")
got <- unname(unlist(attr(wb_to_df(tmp, "Test1"), "tt")))
- expect_equal(exp, got)
+ expect_equal(got, exp)
exp <- c("N/A", "#NUM!", "#NUM!", "#VALUE!")
got <- unname(unlist(wb_to_df(tmp, "Test2")))
- expect_equal(exp, got)
+ expect_equal(got, exp)
})
test_that("write cells without data", {
-
temp <- temp_xlsx()
tmp <- temp_dir()
dat <- as.data.frame(matrix(NA, 2, 2))
- wb <- wb_workbook()$
- add_worksheet()$
- add_data(x = dat, startRow = 2, startCol = 2, na.strings = NULL, colNames = FALSE)
+ wb <- wb_workbook()
+ wb$add_worksheet()
+ wb$add_data(x = dat, startRow = 2, startCol = 2, na.strings = NULL, colNames = FALSE)
wb$worksheets[[1]]$sheet_data$cc$c_t <- ""
@@ -259,35 +258,33 @@ test_that("write cells without data", {
unzip(temp, exdir = tmp)
- exp <- structure(
- list(
- r = c("B2", "C2", "B3", "C3"),
- row_r = c("2", "2", "3", "3"),
- c_r = c("B", "C", "B", "C"),
- c_s = c("", "", "", ""),
- c_t = c("", "", "", ""),
- c_cm = c("", "", "", ""),
- c_ph = c("", "", "", ""),
- c_vm = c("", "", "", ""),
- v = c("", "", "", ""),
- f = c("", "", "", ""),
- f_t = c("", "", "", ""),
- f_ref = c("", "", "", ""),
- f_ca = c("", "", "", ""),
- f_si = c("", "", "", ""),
- is = c("", "", "", ""),
- typ = c("3", "3", "3", "3")
- ),
- row.names = c(NA, 4L),
- class = "data.frame")
+ exp <- data.frame(
+ r = c("B2", "C2", "B3", "C3"),
+ row_r = c("2", "2", "3", "3"),
+ c_r = c("B", "C", "B", "C"),
+ c_s = c("", "", "", ""),
+ c_t = c("", "", "", ""),
+ c_cm = c("", "", "", ""),
+ c_ph = c("", "", "", ""),
+ c_vm = c("", "", "", ""),
+ v = c("", "", "", ""),
+ f = c("", "", "", ""),
+ f_t = c("", "", "", ""),
+ f_ref = c("", "", "", ""),
+ f_ca = c("", "", "", ""),
+ f_si = c("", "", "", ""),
+ is = c("", "", "", ""),
+ typ = c("3", "3", "3", "3"),
+ stringsAsFactors = FALSE
+ )
+
got <- wb$worksheets[[1]]$sheet_data$cc
- expect_equal(exp, got)
+ expect_equal(got, exp)
sheet <- paste0(tmp, "/xl/worksheets/sheet1.xml")
exp <- "|
"
got <- xml_node(sheet, "worksheet", "sheetData")
- expect_equal(exp, got)
-
+ expect_equal(got, exp)
})
test_that("write_xlsx with na.strings", {
@@ -302,15 +299,15 @@ test_that("write_xlsx with na.strings", {
exp <- df
got <- read_xlsx(test)
- expect_equal(exp, got, ignore_attr = TRUE)
+ expect_equal(got, exp, ignore_attr = TRUE)
write_xlsx(df, file = test, na.strings = "N/A")
got <- read_xlsx(test, na.strings = "N/A")
- expect_equal(exp, got, ignore_attr = TRUE)
+ expect_equal(got, exp, ignore_attr = TRUE)
exp$num[exp$num == -99] <- NA
got <- read_xlsx(test, na.strings = "N/A", na.numbers = -99)
- expect_equal(exp, got, ignore_attr = TRUE)
+ expect_equal(got, exp, ignore_attr = TRUE)
})
@@ -341,13 +338,13 @@ test_that("escaping of inlinestrings works", {
exp <- "A & B"
got <- wb_to_df(wb, colNames = FALSE)$A
- expect_equal(exp, got)
+ expect_equal(got, exp)
got <- wb_to_df(temp, colNames = FALSE)$A
- expect_equal(exp, got)
+ expect_equal(got, exp)
wb2 <- wb_load(temp)
got <- wb_to_df(wb2, colNames = FALSE)$A
- expect_equal(exp, got)
+ expect_equal(got, exp)
})
diff --git a/tests/testthat/test-strings_xml.R b/tests/testthat/test-strings_xml.R
index b5857129d..1becde22b 100644
--- a/tests/testthat/test-strings_xml.R
+++ b/tests/testthat/test-strings_xml.R
@@ -98,22 +98,22 @@ test_that("strings_xml", {
exp <- "A & B"
got <- wb_to_df(amp, colNames = FALSE)[1, 1]
- expect_equal(exp, got)
+ expect_equal(got, exp)
# a couple of saves and loads later ...
exp <- "A & B"
got <- wb$worksheets[[1]]$sheet_data$cc$is
- expect_equal(exp, got)
+ expect_equal(got, exp)
wb <- wb_load(amp)
got <- wb$worksheets[[1]]$sheet_data$cc$is
- expect_equal(exp, got)
+ expect_equal(got, exp)
wb$save(amp)
wb <- wb_load(amp)
got <- wb$worksheets[[1]]$sheet_data$cc$is
- expect_equal(exp, got)
+ expect_equal(got, exp)
})
diff --git a/tests/testthat/test-utils.R b/tests/testthat/test-utils.R
index c54b665bf..e3c690a03 100644
--- a/tests/testthat/test-utils.R
+++ b/tests/testthat/test-utils.R
@@ -18,31 +18,31 @@ test_that("dims to col & row and back", {
exp <- list(c("A", "B"), c("1", "2"))
got <- dims_to_rowcol("A1:B2")
- expect_equal(exp, got)
+ expect_equal(got, exp)
exp <- list(1:2, 1:2)
got <- dims_to_rowcol("A1:B2", as_integer = TRUE)
- expect_equal(exp, got)
+ expect_equal(got, exp)
exp <- list(1:2, c(1L))
got <- dims_to_rowcol("A:B", as_integer = TRUE)
- expect_equal(exp, got)
+ expect_equal(got, exp)
exp <- list("A", c("1"))
got <- dims_to_rowcol("A:A", as_integer = FALSE)
- expect_equal(exp, got)
+ expect_equal(got, exp)
exp <- "A1:A1"
got <- rowcol_to_dims(1, "A")
- expect_equal(exp, got)
+ expect_equal(got, exp)
exp <- "A1:A10"
got <- rowcol_to_dims(1:10, 1)
- expect_equal(exp, got)
+ expect_equal(got, exp)
exp <- "E2:J8"
got <- rowcol_to_dims(2:8, 5:10)
- expect_equal(exp, got)
+ expect_equal(got, exp)
})
@@ -53,7 +53,7 @@ test_that("create_char_dataframe", {
got <- create_char_dataframe(colnames = c("x1", "z1"), n = 5)
- expect_equal(exp, got)
+ expect_equal(got, exp)
})
@@ -92,10 +92,10 @@ test_that("relship", {
exp <- ""
got <- relship_no(obj = wb$worksheets_rels[[3]], x = "table")
- expect_equal(exp, got)
+ expect_equal(got, exp)
exp <- "rId2"
got <- get_relship_id(obj = wb$worksheets_rels[[1]], x = "drawing")
- expect_equal(exp, got)
+ expect_equal(got, exp)
})
diff --git a/tests/testthat/test-wb_functions.R b/tests/testthat/test-wb_functions.R
index b77160159..af1250bb4 100644
--- a/tests/testthat/test-wb_functions.R
+++ b/tests/testthat/test-wb_functions.R
@@ -23,7 +23,7 @@ test_that("wb_to_df", {
class = "data.frame"
)
got <- wb_to_df(wb1)
- expect_equal(exp, got, ignore_attr = TRUE)
+ expect_equal(got, exp, ignore_attr = TRUE)
# do not convert first row to colNames
got <- wb_to_df(wb1, colNames = FALSE)
@@ -111,7 +111,7 @@ test_that("wb_to_df", {
rownames(exp) <- seq(2, nrow(exp) + 1)
# read dataset with inlinestr
got <- wb_to_df(wb2)
- expect_equal(exp, got, ignore_attr = TRUE)
+ expect_equal(got, exp, ignore_attr = TRUE)
###########################################################################
@@ -122,12 +122,12 @@ test_that("wb_to_df", {
# read dataset with named_region (returns global first)
exp <- data.frame(A = "S2A1", B = "S2B1")
got <- wb_to_df(wb3, named_region = "MyRange", colNames = FALSE)
- expect_equal(exp, got, ignore_attr = TRUE)
+ expect_equal(got, exp, ignore_attr = TRUE)
# read named_region from sheet
exp <- data.frame(A = "S3A1", B = "S3B1")
got <- wb_to_df(wb3, named_region = "MyRange", sheet = 4, colNames = FALSE)
- expect_equal(exp, got, ignore_attr = TRUE)
+ expect_equal(got, exp, ignore_attr = TRUE)
})
@@ -173,14 +173,14 @@ test_that("dims_to_dataframe", {
class = "data.frame"
)
got <- dims_to_dataframe("A1:A2;C1:C2", fill = TRUE)
- expect_equal(exp, got)
+ expect_equal(got, exp)
got <- dims_to_dataframe("A1;A2;C1;C2", fill = TRUE)
- expect_equal(exp, got)
+ expect_equal(got, exp)
exp <- list(c("A", "B"), "1")
got <- dims_to_rowcol("A1;B1")
- expect_equal(exp, got)
+ expect_equal(got, exp)
})
@@ -195,31 +195,30 @@ test_that("dataframe_to_dims", {
})
test_that("handle 29Feb1900", {
-
dates <- c("1900-02-28", "1900-03-01")
as_date <- as.Date(dates)
as_posix <- as.POSIXct(dates)
exp <- c(59, 61)
got <- conv_to_excel_date(as_date)
- expect_equal(exp, got)
+ expect_equal(got, exp)
got <- conv_to_excel_date(as_posix)
- expect_equal(exp, got)
+ expect_equal(got, exp)
expect_warning(
conv_to_excel_date("x"),
"could not convert x to Excel date. x is of class: character"
)
- wb <- wb_workbook()$
- add_worksheet()$add_data(x = as_date)$
- add_worksheet()$add_data(x = as_posix)
+ wb <- wb_workbook()
+ wb$add_worksheet()$add_data(x = as_date)
+ wb$add_worksheet()$add_data(x = as_posix)
got <- wb_to_df(wb, sheet = 1, colNames = FALSE)$A
- expect_equal(as_date, got)
+ expect_equal(got, as_date)
got <- wb_to_df(wb, sheet = 2, colNames = FALSE)$A
- expect_equal(as_posix, got)
-
+ expect_equal(got, as_posix)
})
+
diff --git a/tests/testthat/test-wb_styles.R b/tests/testthat/test-wb_styles.R
index 369760dc6..74099e3f8 100644
--- a/tests/testthat/test-wb_styles.R
+++ b/tests/testthat/test-wb_styles.R
@@ -1,16 +1,16 @@
test_that("wb_clone_sheet_style", {
-
fl <- system.file("extdata", "oxlsx2_sheet.xlsx", package = "openxlsx2")
- wb <- wb_load(fl)$clone_worksheet("SUM", "clone")
- wb <- wb$clean_sheet(sheet = "clone", numbers = TRUE, characters = TRUE, styles = TRUE, merged_cells = FALSE)
- wb <- wb_clone_sheet_style(wb, "SUM", "clone")
+ wb <- wb_load(fl)
+ wb$clone_worksheet("SUM", "clone")
+ wb$clean_sheet(sheet = "clone", numbers = TRUE, characters = TRUE, styles = TRUE, merged_cells = FALSE)
+ wb$clone_sheet_style("SUM", "clone")
expect_warning(cloneSheetStyle(wb, "SUM", "clone"), "deprecated")
# clone style to empty sheet (creates cells and style)
fl <- system.file("extdata", "oxlsx2_sheet.xlsx", package = "openxlsx2")
wb <- wb_load(fl)$add_worksheet("copy")
- wb <- wb_clone_sheet_style(wb, "SUM", "copy")
+ wb$clone_sheet_style("SUM", "copy")
expect_equal(dim(wb$worksheets[[1]]$sheet_data$cc),
dim(wb$worksheets[[1]]$sheet_data$cc))
expect_equal(dim(wb$worksheets[[1]]$sheet_data$row_attr),
@@ -18,15 +18,18 @@ test_that("wb_clone_sheet_style", {
# clone style to sheet with data
fl <- system.file("extdata", "oxlsx2_sheet.xlsx", package = "openxlsx2")
- wb <- wb_load(fl)$add_worksheet("copy")$add_data(x = mtcars, startRow = 5, startCol = 2)
- wb <- wb_clone_sheet_style(wb, "SUM", "copy")
+ wb <- wb_load(fl)
+ wb$add_worksheet("copy")
+ wb$add_data(x = mtcars, startRow = 5, startCol = 2)
+ wb$clone_sheet_style("SUM", "copy")
expect_equal(c(36, 13), dim(wb$worksheets[[2]]$sheet_data$row_attr))
# clone style on cloned and cleaned worksheet
fl <- system.file("extdata", "oxlsx2_sheet.xlsx", package = "openxlsx2")
- wb <- wb_load(fl)$clone_worksheet("SUM", "clone")
- wb <- wb$clean_sheet(sheet = "clone", numbers = TRUE, characters = TRUE, styles = TRUE, merged_cells = FALSE)
- wb <- wb_clone_sheet_style(wb, "SUM", "clone")
+ wb <- wb_load(fl)
+ wb$clone_worksheet("SUM", "clone")
+ wb$clean_sheet(sheet = "clone", numbers = TRUE, characters = TRUE, styles = TRUE, merged_cells = FALSE)
+ wb$clone_sheet_style("SUM", "clone")
# sort for this test, does not matter later, because we will sort prior to saving
ord <- match(
@@ -41,23 +44,23 @@ test_that("wb_clone_sheet_style", {
# output if copying from empty sheet
fl <- system.file("extdata", "oxlsx2_sheet.xlsx", package = "openxlsx2")
- wb <- wb_load(fl)$add_worksheet("copy")
+ wb <- wb_load(fl)
+ wb$add_worksheet("copy")
expect_message(
expect_message(
- wb <- wb_clone_sheet_style(wb, "copy", "SUM"),
+ wb$clone_sheet_style("copy", "SUM"),
"'from' has no sheet data styles to clone"
),
"'from' has no row styles to clone"
)
-
})
test_that("test add_border()", {
-
wb <- wb_workbook()
- wb$add_worksheet("S1")$add_data("S1", mtcars)
- expect_silent(wb$add_border(1, dims = "A1:K1", left_border = NULL, right_border = NULL, top_border = NULL, bottom_border = "double"))
+ wb$add_worksheet("S1")
+ wb$add_data("S1", mtcars)
+ expect_silent(wb$add_border(dims = "A1:K1", left_border = NULL, right_border = NULL, top_border = NULL, bottom_border = "double"))
# check xf
exp <- c("",
@@ -67,7 +70,7 @@ test_that("test add_border()", {
)
got <- wb$styles_mgr$styles$cellXfs
- expect_equal(exp, got)
+ expect_equal(got, exp)
# check borders
exp <- c("",
@@ -77,7 +80,7 @@ test_that("test add_border()", {
)
got <- wb$styles_mgr$styles$borders
- expect_equal(exp, got)
+ expect_equal(got, exp)
wb <- wb_workbook()
@@ -85,7 +88,7 @@ test_that("test add_border()", {
exp <- c("1", "3", "3", "3", "3", "3", "3", "3", "3", "3", "2")
got <- wb$worksheets[[1]]$sheet_data$cc$c_s
- expect_equal(exp, got)
+ expect_equal(got, exp)
})
@@ -101,7 +104,7 @@ test_that("test add_fill()", {
)
got <- wb$styles_mgr$styles$cellXfs
- expect_equal(exp, got)
+ expect_equal(got, exp)
# check fill
@@ -111,7 +114,7 @@ test_that("test add_fill()", {
)
got <- wb$styles_mgr$styles$fills
- expect_equal(exp, got)
+ expect_equal(got, exp)
# every_nth_col/row
wb <- wb_workbook()
@@ -126,21 +129,21 @@ test_that("test add_fill()", {
)
got <- wb$styles_mgr$styles$fills
- expect_equal(exp, got)
+ expect_equal(got, exp)
exp <- c("",
"",
"")
got <- wb$styles_mgr$styles$cellXfs
- expect_equal(exp, got)
+ expect_equal(got, exp)
# check the actual styles
exp <- c("", "1", "", "1", "", "2", "2", "2", "2", "2", "", "1", "",
"1", "", "2", "2", "2", "2", "2", "", "1", "", "1", "", "2",
"2", "2", "2", "2")
got <- wb$worksheets[[1]]$sheet_data$cc$c_s
- expect_equal(exp, got)
+ expect_equal(got, exp)
wb <- wb_workbook()
@@ -148,7 +151,7 @@ test_that("test add_fill()", {
exp <- rep("1", 8)
got <- wb$worksheets[[1]]$sheet_data$cc$c_s
- expect_equal(exp, got)
+ expect_equal(got, exp)
})
@@ -165,7 +168,7 @@ test_that("test add_font()", {
)
got <- wb$styles_mgr$styles$cellXfs
- expect_equal(exp, got)
+ expect_equal(got, exp)
# check font
@@ -175,20 +178,20 @@ test_that("test add_font()", {
)
got <- wb$styles_mgr$styles$fonts
- expect_equal(exp, got)
+ expect_equal(got, exp)
# check the actual styles
exp <- c("1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "",
"", "", "", "", "", "", "", "")
got <- head(wb$worksheets[[1]]$sheet_data$cc$c_s, 20)
- expect_equal(exp, got)
+ expect_equal(got, exp)
wb <- wb_workbook()
wb$add_worksheet("S1")$add_font("S1", dims = "A1:K1", color = wb_colour(hex = "FFFFFF00"))
exp <- rep("1", 11)
got <- wb$worksheets[[1]]$sheet_data$cc$c_s
- expect_equal(exp, got)
+ expect_equal(got, exp)
})
@@ -206,20 +209,20 @@ test_that("test add_numfmt()", {
)
got <- wb$styles_mgr$styles$cellXfs
- expect_equal(exp, got)
+ expect_equal(got, exp)
# check numfmt
exp <- ""
got <- wb$styles_mgr$styles$numFmts
- expect_equal(exp, got)
+ expect_equal(got, exp)
# check the actual styles
exp <- c("1", "", "", "", "", "2", "", "", "", "", "", "1", "", "",
"", "", "2", "", "", "")
got <- head(wb$worksheets[[1]]$sheet_data$cc$c_s, 20)
- expect_equal(exp, got)
+ expect_equal(got, exp)
wb <- wb_workbook()
@@ -227,7 +230,7 @@ test_that("test add_numfmt()", {
exp <- rep("1", 33)
got <- wb$worksheets[[1]]$sheet_data$cc$c_s
- expect_equal(exp, got)
+ expect_equal(got, exp)
})
@@ -245,13 +248,13 @@ test_that("test add_cell_style()", {
)
got <- wb$styles_mgr$styles$cellXfs
- expect_equal(exp, got)
+ expect_equal(got, exp)
# check the actual styles
exp <- c("1", "", "", "", "", "2", "", "", "", "", "", "1", "", "",
"", "", "2", "", "", "")
got <- head(wb$worksheets[[1]]$sheet_data$cc$c_s, 20)
- expect_equal(exp, got)
+ expect_equal(got, exp)
wb <- wb_workbook()
@@ -259,7 +262,7 @@ test_that("test add_cell_style()", {
exp <- rep("1", 33)
got <- wb$worksheets[[1]]$sheet_data$cc$c_s
- expect_equal(exp, got)
+ expect_equal(got, exp)
###
exp <- ""
@@ -284,45 +287,42 @@ test_that("test add_cell_style()", {
hidden = "1",
locked = "1"
)
- expect_equal(exp, got)
+ expect_equal(got, exp)
})
test_that("add_style", {
-
# without name
num <- create_numfmt(numFmtId = "165", formatCode = "#.#")
- wb <- wb_workbook() %>% wb_add_style(num)
+ wb <- wb_workbook()
+ wb$add_style(num)
exp <- num
got <- wb$styles_mgr$styles$numFmts
- expect_equal(exp, got)
+ expect_equal(got, exp)
exp <- structure(list(typ = "numFmt", id = "165", name = "num"),
row.names = c(NA, -1L),
class = "data.frame")
got <- wb$styles_mgr$numfmt
- expect_equal(exp, got)
+ expect_equal(got, exp)
# with name
wb <- wb_workbook() %>% wb_add_style(num, "num")
exp <- num
got <- wb$styles_mgr$styles$numFmts
- expect_equal(exp, got)
+ expect_equal(got, exp)
exp <- structure(list(typ = "numFmt", id = "165", name = "num"),
row.names = c(NA, -1L),
class = "data.frame")
got <- wb$styles_mgr$numfmt
- expect_equal(exp, got)
-
+ expect_equal(got, exp)
})
test_that("assigning styles to loaded workbook works", {
-
wb <- wb_load(file = system.file("extdata", "oxlsx2_sheet.xlsx", package = "openxlsx2"))
-
# previously it would break on xml_import, because NA was returned
expect_silent(wb$add_font()$add_font())
@@ -331,8 +331,9 @@ test_that("assigning styles to loaded workbook works", {
test_that("get & set cell style(s)", {
# set a style in b1
- wb <- wb_workbook()$add_worksheet()$
- add_numfmt(dims = "B1", numfmt = "#,0")
+ wb <- wb_workbook()
+ wb$add_worksheet()
+ wb$add_numfmt(dims = "B1", numfmt = "#,0")
# get style from b1 to assign it to a1
numfmt <- wb$get_cell_style(dims = "B1")
@@ -374,19 +375,19 @@ test_that("get_cell_styles()", {
exp <- "1"
got <- wb$get_cell_style(dims = "B2")
- expect_equal(exp, got)
+ expect_equal(got, exp)
exp <- ""
got <- get_cell_styles(wb, 1, "B2")
- expect_equal(exp, got)
+ expect_equal(got, exp)
exp <- "3"
got <- wb$get_cell_style(dims = "B3")
- expect_equal(exp, got)
+ expect_equal(got, exp)
exp <- ""
got <- get_cell_styles(wb, 1, "B3")
- expect_equal(exp, got)
+ expect_equal(got, exp)
wb$add_cell_style(dims = "B3:L3",
textRotation = "45",
@@ -396,25 +397,23 @@ test_that("get_cell_styles()", {
exp <- ""
got <- get_cell_styles(wb, 1, "B3")
- expect_equal(exp, got)
+ expect_equal(got, exp)
})
test_that("applyCellStyle works", {
-
- wb <- wb_workbook()$
- add_worksheet()$
- add_fill(dims = "B2:G8", color = wb_colour("yellow"))$
- add_data(dims = "C3", x = Sys.Date())$
- add_data(dims = "E3", x = Sys.Date(), applyCellStyle = FALSE)$
- add_data(dims = "E5", x = Sys.Date(), removeCellStyle = TRUE)$
- add_data(dims = "A1", x = Sys.Date())
+ wb <- wb_workbook()
+ wb$add_worksheet()
+ wb$add_fill(dims = "B2:G8", color = wb_colour("yellow"))
+ wb$add_data(dims = "C3", x = Sys.Date())
+ wb$add_data(dims = "E3", x = Sys.Date(), applyCellStyle = FALSE)
+ wb$add_data(dims = "E5", x = Sys.Date(), removeCellStyle = TRUE)
+ wb$add_data(dims = "A1", x = Sys.Date())
cc <- wb$worksheets[[1]]$sheet_data$cc
exp <- c("3", "2", "1", "3")
got <- cc[cc$r %in% c("A1", "C3", "E3", "E5"), "c_s"]
- expect_equal(exp, got)
-
+ expect_equal(got, exp)
})
test_that("style names are xml", {
@@ -468,7 +467,7 @@ test_that("style names are xml", {
extLst = NULL
)
got <- wb$styles_mgr$styles
- expect_equal(exp, got)
+ expect_equal(got, exp)
})
diff --git a/tests/testthat/test-write.R b/tests/testthat/test-write.R
index db9947a6e..cf335726f 100644
--- a/tests/testthat/test-write.R
+++ b/tests/testthat/test-write.R
@@ -1,5 +1,4 @@
test_that("write_formula", {
-
set.seed(123)
df <- data.frame(C = rnorm(10), D = rnorm(10))
@@ -16,43 +15,41 @@ test_that("write_formula", {
# write data add array formula later
wb <- wb_workbook()
- wb <- wb_add_worksheet(wb, "df")
+ wb$add_worksheet("df")
wb$add_data("df", df, startCol = "C")
- write_formula(wb, "df", startCol = "E", startRow = "2",
- x = "SUM(C2:C11*D2:D11)",
- array = TRUE)
+ wb$add_formula("df", startCol = "E", startRow = "2",
+ x = "SUM(C2:C11*D2:D11)",
+ array = TRUE)
cc <- wb$worksheets[[1]]$sheet_data$cc
got <- cc[cc$row_r == "2" & cc$c_r == "E", ]
expect_equal(exp[1:16], got[1:16])
-
rownames(exp) <- 1L
# write formula first add data later
wb <- wb_workbook()
- wb <- wb_add_worksheet(wb, "df")
- write_formula(wb, "df", startCol = "E", startRow = "2",
- x = "SUM(C2:C11*D2:D11)",
- array = TRUE)
+ wb$add_worksheet("df")
+ wb$add_formula("df", startCol = "E", startRow = "2",
+ x = "SUM(C2:C11*D2:D11)",
+ array = TRUE)
wb$add_data("df", df, startCol = "C")
cc <- wb$worksheets[[1]]$sheet_data$cc
got <- cc[cc$row_r == "2" & cc$c_r == "E", ]
expect_equal(exp[1:11], got[1:11])
-
})
test_that("silent with numfmt option", {
-
wb <- wb_workbook()
wb$add_worksheet("S1")
wb$add_worksheet("S2")
wb$add_data_table("S1", x = iris)
expect_warning(
- wb$add_data_table("S2",
- x = mtcars, xy = c("B", 3), rowNames = TRUE,
- tableStyle = "TableStyleLight9")
+ wb$add_data_table(
+ "S2", x = mtcars, xy = c("B", 3), rowNames = TRUE,
+ tableStyle = "TableStyleLight9"
+ )
)
# [1:4] to ignore factor
@@ -65,7 +62,6 @@ test_that("silent with numfmt option", {
attr(got, "types") <- NULL
expect_equal(mtcars, got)
expect_equal(rownames(mtcars), rownames(got))
-
})
test_that("test options", {
@@ -77,7 +73,6 @@ test_that("test options", {
# adding data to the worksheet should not alter the global options
expect_equal(ops, ops2)
-
})
test_that("missing x is caught early [246]", {
@@ -88,10 +83,11 @@ test_that("missing x is caught early [246]", {
})
test_that("update_cells", {
-
## exactly the same
data <- mtcars
- wb <- wb_workbook()$add_worksheet()$add_data(x = data)
+ wb <- wb_workbook()
+ wb$add_worksheet()
+ wb$add_data(x = data)
cc1 <- wb$worksheets[[1]]$sheet_data$cc
wb$add_data(x = data)
@@ -101,57 +97,58 @@ test_that("update_cells", {
## write na.strings
data <- matrix(NA, 2, 2)
- wb <- wb_workbook()$add_worksheet()$add_data(x = data)$add_data(x = data, na.strings = "N/A")
+ wb <- wb_workbook()
+ wb$add_worksheet()
+ wb$add_data(x = data)
+ wb$add_data(x = data, na.strings = "N/A")
exp <- c("V1", "V2", "N/A")
got <- unique(wb$worksheets[[1]]$sheet_data$cc$is)
- expect_equal(exp, got)
+ expect_equal(got, exp)
### write logical
xlsxFile <- system.file("extdata", "readTest.xlsx", package = "openxlsx2")
wb1 <- wb_load(xlsxFile)
data <- head(wb_to_df(wb1, sheet = 3))
- wb <- wb_workbook()$add_worksheet()$add_data(x = data)$add_data(x = data)
+ wb <- wb_workbook()
+ wb$add_worksheet()
+ wb$add_data(x = data)
exp <- c("inlineStr", "", "b", "e")
got <- unique(wb$worksheets[[1]]$sheet_data$cc$c_t)
- expect_equal(exp, got)
-
+ expect_equal(got, exp)
set.seed(123)
df <- data.frame(C = rnorm(10), D = rnorm(10))
- wb <- wb_workbook()$
- add_worksheet("df")$
- add_data(x = df, startCol = "C")
+ wb <- wb_workbook()
+ wb$add_worksheet("df")
+ wb$add_data(x = df, startCol = "C")
# TODO add_formula()
- write_formula(wb, "df", startCol = "E", startRow = "2",
- x = "SUM(C2:C11*D2:D11)",
- array = TRUE)
- write_formula(wb, "df", x = "C3 + D3", startCol = "E", startRow = 3)
+ wb$add_formula("df", startCol = "E", startRow = "2",
+ x = "SUM(C2:C11*D2:D11)",
+ array = TRUE)
+ wb$add_formula("df", x = "C3 + D3", startCol = "E", startRow = 3)
x <- c(google = "https://www.google.com")
class(x) <- "hyperlink"
wb$add_data(sheet = "df", x = x, startCol = "E", startRow = 4)
-
exp <- structure(
list(c_t = c("", "str", "str"),
f = c("SUM(C2:C11*D2:D11)", "C3 + D3", "=HYPERLINK(\"https://www.google.com\")"),
f_t = c("array", "", "")),
row.names = c("23", "110", "111"), class = "data.frame")
got <- wb$worksheets[[1]]$sheet_data$cc[c(5, 8, 11), c("c_t", "f", "f_t")]
- expect_equal(exp, got)
-
+ expect_equal(got, exp)
})
test_that("write dims", {
-
# create a workbook
- wb <- wb_workbook()$
- add_worksheet()$add_data(dims = "B2:C3", x = matrix(1:4, 2, 2), colNames = FALSE)$
- add_worksheet()$add_data_table(dims = "B:C", x = as.data.frame(matrix(1:4, 2, 2)))$
- add_worksheet()$add_formula(dims = "B3", x = "42")
+ wb <- wb_workbook()
+ wb$add_worksheet()$add_data(dims = "B2:C3", x = matrix(1:4, 2, 2), colNames = FALSE)
+ wb$add_worksheet()$add_data_table(dims = "B:C", x = as.data.frame(matrix(1:4, 2, 2)))
+ wb$add_worksheet()$add_formula(dims = "B3", x = "42")
s1 <- wb_to_df(wb, 1, colNames = FALSE)
s2 <- wb_to_df(wb, 2, colNames = FALSE)
@@ -164,11 +161,9 @@ test_that("write dims", {
expect_equal(colnames(s1), c("B", "C"))
expect_equal(colnames(s2), c("B", "C"))
expect_equal(colnames(s3), c("B"))
-
})
test_that("write data.table class", {
-
df <- mtcars
class(df) <- c("data.table", "data.frame")
@@ -179,29 +174,19 @@ test_that("write data.table class", {
})
test_that("update cell(s)", {
-
xlsxFile <- system.file("extdata", "update_test.xlsx", package = "openxlsx2")
wb <- wb_load(xlsxFile)
# update Cells D4:D6 with 1:3
- wb <- wb_add_data(x = c(1:3),
- wb = wb, sheet = "Sheet1", dims = "D4:D6")
-
+ wb$add_data(x = c(1:3), sheet = "Sheet1", dims = "D4:D6")
# update Cells B3:D3 (names())
- wb <- wb_add_data(x = c("x", "y", "z"),
- wb = wb, sheet = "Sheet1", dims = "B3:D3")
-
+ wb$add_data(x = c("x", "y", "z"), sheet = "Sheet1", dims = "B3:D3")
# update D4 again (single value this time)
- wb <- wb_add_data(x = 7,
- wb = wb, sheet = "Sheet1", dims = "D4")
-
+ wb$add_data(x = 7, sheet = "Sheet1", dims = "D4")
# add new column on the left of the existing workbook
- wb <- wb_add_data(x = 7,
- wb = wb, sheet = "Sheet1", dims = "A4")
-
+ wb$add_data(x = 7, sheet = "Sheet1", dims = "A4")
# add new row on the end of the existing workbook
- wb <- wb_add_data(x = 7,
- wb = wb, sheet = "Sheet1", dims = "A9")
+ wb$add_data(x = 7, sheet = "Sheet1", dims = "A9")
exp <- structure(
list(c(7, NA, NA, NA, NA, 7),
@@ -213,14 +198,14 @@ test_that("update cell(s)", {
class = "data.frame")
got <- wb_to_df(wb)
- expect_equal(exp, got, ignore_attr = TRUE)
+ expect_equal(got, exp, ignore_attr = TRUE)
####
- wb <- wb_workbook()$
- add_worksheet()$
- add_fill(dims = "B2:G8", color = wb_colour("yellow"))$
- add_data(dims = "C3", x = Sys.Date())$
- add_data(dims = "E4", x = Sys.Date(), removeCellStyle = TRUE)
+ wb <- wb_workbook()
+ wb$add_worksheet()
+ wb$add_fill(dims = "B2:G8", color = wb_colour("yellow"))
+ wb$add_data(dims = "C3", x = Sys.Date())
+ wb$add_data(dims = "E4", x = Sys.Date(), removeCellStyle = TRUE)
exp <- structure(list(r = c("B2", "C2", "D2", "E2", "F2", "G2"),
row_r = c("2", "2", "2", "2", "2", "2"),
c_r = c("B", "C", "D", "E", "F", "G"),
@@ -240,14 +225,13 @@ test_that("update cell(s)", {
row.names = c("1", "8", "17", "114", "121", "128"),
class = "data.frame")
got <- head(wb$worksheets[[1]]$sheet_data$cc)
- expect_equal(exp, got)
-
+ expect_equal(got, exp)
})
test_that("write_rownames", {
- wb <- wb_workbook()$
- add_worksheet()$add_data(x = mtcars, rowNames = TRUE)$
- add_worksheet()$add_data_table(x = mtcars, rowNames = TRUE)
+ wb <- wb_workbook()
+ wb$add_worksheet()$add_data(x = mtcars, rowNames = TRUE)
+ wb$add_worksheet()$add_data_table(x = mtcars, rowNames = TRUE)
exp <- structure(
list(A = c(NA, "Mazda RX4"), B = c("mpg", "21")),
@@ -260,7 +244,7 @@ test_that("write_rownames", {
types = c(A = 0, B = 0)
)
got <- wb_to_df(wb, 1, dims = "A1:B2", colNames = FALSE)
- expect_equal(exp, got)
+ expect_equal(got, exp)
exp <- structure(
list(A = c("_rowNames_", "Mazda RX4"), B = c("mpg", "21")),
@@ -273,27 +257,16 @@ test_that("write_rownames", {
types = c(A = 0, B = 0)
)
got <- wb_to_df(wb, 2, dims = "A1:B2", colNames = FALSE)
- expect_equal(exp, got)
-
+ expect_equal(got, exp)
})
test_that("NA works as expected", {
-
- wb <- wb_workbook()$
- add_worksheet("Sheet1")$
- add_data(
- dims = "A1",
- x = NA,
- na.strings = NULL
- )$
- add_data(
- dims = "A2",
- x = NA_character_,
- na.strings = NULL
- )
+ wb <- wb_workbook()
+ wb$add_worksheet("Sheet1")
+ wb$add_data(dims = "A1", x = NA, na.strings = NULL)
+ wb$add_data(dims = "A2", x = NA_character_, na.strings = NULL)
exp <- c(NA_real_, NA_real_)
got <- wb_to_df(wb, colNames = FALSE)$A
- expect_equal(exp, got)
-
+ expect_equal(got, exp)
})
diff --git a/tests/testthat/test-writing_posixct.R b/tests/testthat/test-writing_posixct.R
index f3cc9d3d2..0fc12bf06 100644
--- a/tests/testthat/test-writing_posixct.R
+++ b/tests/testthat/test-writing_posixct.R
@@ -56,20 +56,20 @@ test_that("Writing mixed EDT/EST Posixct with write_data & write_datatable", {
# compare sheet 1
exp <- df$timeval
got <- wb_s1$timeval
- expect_equal(exp, got, tolerance = 10 ^ -10, ignore_attr = "tzone")
+ expect_equal(got, exp, tolerance = 10 ^ -10, ignore_attr = "tzone")
exp <- df$timetxt
got <- wb_s1$timetxt
- expect_equal(exp, got)
+ expect_equal(got, exp)
# compare sheet 2
exp <- df$timeval
got <- wb_s2$timeval
- expect_equal(exp, got, tolerance = 10 ^ -10, ignore_attr = "tzone")
+ expect_equal(got, exp, tolerance = 10 ^ -10, ignore_attr = "tzone")
exp <- df$timetxt
got <- wb_s2$timetxt
- expect_equal(exp, got)
+ expect_equal(got, exp)
options("openxlsx2.datetimeFormat" = "yyyy-mm-dd hh:mm:ss")
})