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 20, 2023
1 parent 8cd19a3 commit 8806ace
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 26 deletions.
15 changes: 6 additions & 9 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 Expand Up @@ -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.
Expand Down
50 changes: 46 additions & 4 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 @@ -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,
Expand Down
12 changes: 8 additions & 4 deletions man/wbWorkbook.Rd

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

4 changes: 0 additions & 4 deletions man/wb_add_conditional_formatting.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 8806ace

Please sign in to comment.