Skip to content

Commit

Permalink
dims for
Browse files Browse the repository at this point in the history
* wb_merge_cells
* wb_unmerge_cells
  • Loading branch information
JanMarvin committed Jul 19, 2023
1 parent 454dac2 commit 522c0f6
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 18 deletions.
13 changes: 6 additions & 7 deletions R/class-workbook-wrappers.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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, ... = ...)
}


Expand Down
48 changes: 45 additions & 3 deletions R/class-workbook.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -3551,8 +3572,29 @@ wbWorkbook <- R6::R6Class(
#' Removes cell merging for a sheet
#' @param sheet sheet
#' @param rows,cols Row and column specifications.
#' @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,
Expand Down
10 changes: 7 additions & 3 deletions man/wbWorkbook.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions man/wb_merge_cells.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 522c0f6

Please sign in to comment.