From 9ede473137cec6dee0c2fae756672d59fd38a452 Mon Sep 17 00:00:00 2001 From: Jan Marvin Garbuszus Date: Wed, 19 Jul 2023 21:56:12 +0200 Subject: [PATCH] dims for * wb_merge_cells * wb_unmerge_cells --- R/class-workbook-wrappers.R | 15 ++++----- R/class-workbook.R | 50 +++++++++++++++++++++++++--- man/wbWorkbook.Rd | 12 ++++--- man/wb_add_conditional_formatting.Rd | 4 --- man/wb_merge_cells.Rd | 10 +++--- 5 files changed, 65 insertions(+), 26 deletions(-) diff --git a/R/class-workbook-wrappers.R b/R/class-workbook-wrappers.R index 978c74612..72eb0c5d1 100644 --- a/R/class-workbook-wrappers.R +++ b/R/class-workbook-wrappers.R @@ -481,9 +481,8 @@ wb_copy_cells <- function( #' #' @param wb A workbook object #' @param sheet A name or index of a worksheet -#' @param cols,rows Column and row specifications to merge on. Note: `min()` and -#' `max()` of each vector are provided for specs. Skipping rows or columns is -#' not recognized. +#' @param dims worksheet cells +#' @param ... additional arguments #' #' @examples #' # Create a new workbook @@ -517,16 +516,16 @@ NULL #' @export #' @rdname wb_merge_cells -wb_merge_cells <- function(wb, sheet = current_sheet(), rows = NULL, cols = NULL) { +wb_merge_cells <- function(wb, sheet = current_sheet(), dims = NULL, ...) { assert_workbook(wb) - wb$clone()$merge_cells(sheet = sheet, rows = rows, cols = cols) + wb$clone()$merge_cells(sheet = sheet, dims = dims, ... = ...) } #' @export #' @rdname wb_merge_cells -wb_unmerge_cells <- function(wb, sheet = current_sheet(), rows = NULL, cols = NULL) { +wb_unmerge_cells <- function(wb, sheet = current_sheet(), dims = NULL, ...) { assert_workbook(wb) - wb$clone()$unmerge_cells(sheet = sheet, rows = rows, cols = cols) + wb$clone()$unmerge_cells(sheet = sheet, dims = dims, ... = ...) } @@ -3095,8 +3094,6 @@ wb_add_form_control <- function( #' @param wb A workbook object #' @param sheet A name or index of a worksheet #' @param dims A cell or cell range like "A1" or "A1:B2" -#' @param cols Columns to apply conditional formatting to -#' @param rows Rows to apply conditional formatting to #' @param rule The condition under which to apply the formatting. See examples. #' @param style A style to apply to those cells that satisfy the rule. Default is 'font_color = "FF9C0006"' and 'bgFill = "FFFFC7CE"' #' @param type The type of conditional formatting rule to apply. diff --git a/R/class-workbook.R b/R/class-workbook.R index 60888e170..af7cabd54 100644 --- a/R/class-workbook.R +++ b/R/class-workbook.R @@ -3536,9 +3536,30 @@ wbWorkbook <- R6::R6Class( #' @description #' Set cell merging for a sheet #' @param sheet sheet - #' @param rows,cols Row and column specifications. + #' @param dims worksheet cells + #' @param ... additional arguments #' @return The `wbWorkbook` object, invisibly - merge_cells = function(sheet = current_sheet(), rows = NULL, cols = NULL) { + merge_cells = function(sheet = current_sheet(), dims = NULL, ...) { + + cols <- list(...)[["cols"]] + rows <- list(...)[["rows"]] + + if (!is.null(rows) && !is.null(cols)) { + + if (length(cols) > 2 && any(diff(cols) != 1)) + warning("cols > 2, will create range from min to max.") + + if (getOption("openxlsx2.soon_deprecated", default = FALSE)) + .Deprecated(old = "cols/rows", new = "dims", package = "openxlsx2") + + dims <- rowcol_to_dims(rows, cols) + } + + ddims <- dims_to_rowcol(dims) + + rows <- ddims[[2]] + cols <- ddims[[1]] + sheet <- private$get_sheet_index(sheet) self$worksheets[[sheet]]$merge_cells( rows = rows, @@ -3550,9 +3571,30 @@ wbWorkbook <- R6::R6Class( #' @description #' Removes cell merging for a sheet #' @param sheet sheet - #' @param rows,cols Row and column specifications. + #' @param dims worksheet cells + #' @param ... additional arguments #' @return The `wbWorkbook` object, invisibly - unmerge_cells = function(sheet = current_sheet(), rows = NULL, cols = NULL) { + unmerge_cells = function(sheet = current_sheet(), dims = NULL, ...) { + + cols <- list(...)[["cols"]] + rows <- list(...)[["rows"]] + + if (!is.null(rows) && !is.null(cols)) { + + if (length(cols) > 2 && any(diff(cols) != 1)) + warning("cols > 2, will create range from min to max.") + + if (getOption("openxlsx2.soon_deprecated", default = FALSE)) + .Deprecated(old = "cols/rows", new = "dims", package = "openxlsx2") + + dims <- rowcol_to_dims(rows, cols) + } + + ddims <- dims_to_rowcol(dims) + + rows <- ddims[[2]] + cols <- ddims[[1]] + sheet <- private$get_sheet_index(sheet) self$worksheets[[sheet]]$unmerge_cells( rows = rows, diff --git a/man/wbWorkbook.Rd b/man/wbWorkbook.Rd index 5222a5455..e0060b0be 100644 --- a/man/wbWorkbook.Rd +++ b/man/wbWorkbook.Rd @@ -1554,7 +1554,7 @@ Adds data validation \subsection{Method \code{merge_cells()}}{ Set cell merging for a sheet \subsection{Usage}{ -\if{html}{\out{
}}\preformatted{wbWorkbook$merge_cells(sheet = current_sheet(), rows = NULL, cols = NULL)}\if{html}{\out{
}} +\if{html}{\out{
}}\preformatted{wbWorkbook$merge_cells(sheet = current_sheet(), dims = NULL, ...)}\if{html}{\out{
}} } \subsection{Arguments}{ @@ -1562,7 +1562,9 @@ Set cell merging for a sheet \describe{ \item{\code{sheet}}{sheet} -\item{\code{rows, cols}}{Row and column specifications.} +\item{\code{dims}}{worksheet cells} + +\item{\code{...}}{additional arguments} } \if{html}{\out{}} } @@ -1576,7 +1578,7 @@ The \code{wbWorkbook} object, invisibly \subsection{Method \code{unmerge_cells()}}{ Removes cell merging for a sheet \subsection{Usage}{ -\if{html}{\out{
}}\preformatted{wbWorkbook$unmerge_cells(sheet = current_sheet(), rows = NULL, cols = NULL)}\if{html}{\out{
}} +\if{html}{\out{
}}\preformatted{wbWorkbook$unmerge_cells(sheet = current_sheet(), dims = NULL, ...)}\if{html}{\out{
}} } \subsection{Arguments}{ @@ -1584,7 +1586,9 @@ Removes cell merging for a sheet \describe{ \item{\code{sheet}}{sheet} -\item{\code{rows, cols}}{Row and column specifications.} +\item{\code{dims}}{worksheet cells} + +\item{\code{...}}{additional arguments} } \if{html}{\out{}} } diff --git a/man/wb_add_conditional_formatting.Rd b/man/wb_add_conditional_formatting.Rd index 2e114c48b..0580b0fa6 100644 --- a/man/wb_add_conditional_formatting.Rd +++ b/man/wb_add_conditional_formatting.Rd @@ -35,10 +35,6 @@ wb_add_conditional_formatting( \item{params}{Additional parameters passed. See \strong{Details} for more} \item{...}{additional arguments} - -\item{cols}{Columns to apply conditional formatting to} - -\item{rows}{Rows to apply conditional formatting to} } \description{ Add conditional formatting to cells diff --git a/man/wb_merge_cells.Rd b/man/wb_merge_cells.Rd index d131eeb63..d007aca68 100644 --- a/man/wb_merge_cells.Rd +++ b/man/wb_merge_cells.Rd @@ -5,18 +5,18 @@ \alias{wb_unmerge_cells} \title{Worksheet cell merging} \usage{ -wb_merge_cells(wb, sheet = current_sheet(), rows = NULL, cols = NULL) +wb_merge_cells(wb, sheet = current_sheet(), dims = NULL, ...) -wb_unmerge_cells(wb, sheet = current_sheet(), rows = NULL, cols = NULL) +wb_unmerge_cells(wb, sheet = current_sheet(), dims = NULL, ...) } \arguments{ \item{wb}{A workbook object} \item{sheet}{A name or index of a worksheet} -\item{cols, rows}{Column and row specifications to merge on. Note: \code{min()} and -\code{max()} of each vector are provided for specs. Skipping rows or columns is -not recognized.} +\item{dims}{worksheet cells} + +\item{...}{additional arguments} } \description{ Merge cells within a worksheet