Skip to content

Commit

Permalink
[merge_cells] allow splitting into dimensions
Browse files Browse the repository at this point in the history
  • Loading branch information
JanMarvin committed Dec 14, 2024
1 parent 49c89dc commit fd4df03
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 15 deletions.
5 changes: 3 additions & 2 deletions R/class-workbook-wrappers.R
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,7 @@ wb_copy_cells <- function(
#' @param sheet A name or index of a worksheet
#' @param dims worksheet cells
#' @param solve logical if intersecting merges should be solved
#' @param direction direction in which to split the cell merging. Allows "row" or "col"
#' @param ... additional arguments
#'
#' @examples
Expand Down Expand Up @@ -934,9 +935,9 @@ wb_copy_cells <- function(
#' @family workbook wrappers
#' @family worksheet content functions
#' @export
wb_merge_cells <- function(wb, sheet = current_sheet(), dims = NULL, solve = FALSE, ...) {
wb_merge_cells <- function(wb, sheet = current_sheet(), dims = NULL, solve = FALSE, direction = NULL, ...) {
assert_workbook(wb)
wb$clone(deep = TRUE)$merge_cells(sheet = sheet, dims = dims, solve = solve, ... = ...)
wb$clone(deep = TRUE)$merge_cells(sheet = sheet, dims = dims, solve = solve, direction = direction, ... = ...)
}

#' @export
Expand Down
14 changes: 3 additions & 11 deletions R/class-workbook.R
Original file line number Diff line number Diff line change
Expand Up @@ -5256,8 +5256,9 @@ wbWorkbook <- R6::R6Class(
#' @description
#' Set cell merging for a sheet
#' @param solve logical if intersecting cells should be solved
#' @param direction direction in which to split the cell merging. Allows "row" or "col".
#' @return The `wbWorkbook` object, invisibly
merge_cells = function(sheet = current_sheet(), dims = NULL, solve = FALSE, ...) {
merge_cells = function(sheet = current_sheet(), dims = NULL, solve = FALSE, direction = NULL, ...) {

cols <- list(...)[["cols"]]
rows <- list(...)[["rows"]]
Expand All @@ -5273,17 +5274,8 @@ wbWorkbook <- R6::R6Class(
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,
cols = cols,
solve = solve
)
self$worksheets[[sheet]]$merge_cells(dims = dims, solve = solve, direction = direction)
invisible(self)
},

Expand Down
14 changes: 13 additions & 1 deletion R/class-worksheet.R
Original file line number Diff line number Diff line change
Expand Up @@ -514,8 +514,14 @@ wbWorksheet <- R6::R6Class(
#' Set cell merging for a sheet
#' @param rows,cols Row and column specifications.
#' @param solve logical if intersects should be solved
#' @param direction direction in which to split
#' @return The `wbWorkbook` object, invisibly
merge_cells = function(rows = NULL, cols = NULL, solve = FALSE) {
merge_cells = function(dims = NULL, solve = FALSE, direction = NULL) {

ddims <- dims_to_rowcol(dims)

rows <- ddims[[2]]
cols <- ddims[[1]]

rows <- range(as.integer(rows))
cols <- range(col2int(cols))
Expand Down Expand Up @@ -556,6 +562,12 @@ wbWorksheet <- R6::R6Class(
}
}

if (!is.null(direction)) {
if (direction == "row") direction <- 1
if (direction == "col") direction <- 2
sqref <- split_dims(sqref, direction = direction)
}

self$append("mergeCells", sprintf('<mergeCell ref="%s"/>', sqref))

invisible(self)
Expand Down
3 changes: 3 additions & 0 deletions man/wbWorkbook.Rd

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

11 changes: 10 additions & 1 deletion man/wb_merge_cells.Rd

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

1 change: 1 addition & 0 deletions openxlsx2.Rproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Version: 1.0
ProjectId: d6ecb160-ba3b-42c4-8c47-059500a3b4de

RestoreWorkspace: Default
SaveWorkspace: Default
Expand Down

0 comments on commit fd4df03

Please sign in to comment.