Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provide wb_copy_cells. closes #282 #515

Merged
merged 4 commits into from
Jan 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export(wb_clone_worksheet)
export(wb_color)
export(wb_colour)
export(wb_conditional_formatting)
export(wb_copy_cells)
export(wb_data)
export(wb_freeze_pane)
export(wb_get_active_sheet)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

* `tabColor` in `wb_add_worksheet()` now allows passing `wb_color()`. [500](https://github.com/JanMarvin/openxlsx2/pull/500)

* Add `wb_copy_cells()` a wrapper that allows copying cell ranges in a workbook as direct copy, as reference or as value. [515](https://github.com/JanMarvin/openxlsx2/pull/515)

## Fixes

* Reading of files with frozen panes and more than one section node was restored. [495](https://github.com/JanMarvin/openxlsx2/pull/495)
Expand Down
45 changes: 45 additions & 0 deletions R/class-workbook-wrappers.R
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,51 @@ wb_add_formula <- function(
)
}

#' copy cells around
#' @param wb workbook
#' @param sheet a worksheet
#' @param dims cell used as start
#' @param data a wb_data object
#' @param as_value should a copy of the value be written
#' @param as_ref should references to the cell be written
#' @param transpose should the data be written transposed
#' @examples
#' wb <- wb_workbook()$
#' add_worksheet()$
#' add_data(x = mtcars)$
#' add_fill(dims = "A1:F1", color = wb_color("yellow"))
#'
#' dat <- wb_data(wb, dims = "A1:D4", colNames = FALSE)
#'
#' wb$
#' # 1:1 copy to M2
#' clone_worksheet(old = 1, new = "Clone1")$
#' copy_cells(data = dat, dims = "M2")
#' @family workbook wrappers
#' @export
#' @returns the wbWorkbook invisibly
wb_copy_cells <- function(
wb,
sheet = current_sheet(),
dims = "A1",
data,
as_value = FALSE,
as_ref = FALSE,
transpose = FALSE
) {
assert_workbook(wb)
wb$
clone(deep = TRUE)$
copy_cells(
sheet = sheet,
dims = dims,
data = data,
as_value = as_value,
as_ref = as_ref,
transpose = transpose
)
}

# merge cells -------------------------------------------------------------

#' Worksheet cell merging
Expand Down
87 changes: 87 additions & 0 deletions R/class-workbook.R
Original file line number Diff line number Diff line change
Expand Up @@ -1987,6 +1987,93 @@ wbWorkbook <- R6::R6Class(
invisible(self)
},

### copy cells ----

#' @description
#' copy cells around in a workbook
#' @param sheet a worksheet
#' @param dims cell used as start
#' @param data a wb_data object
#' @param as_value should a copy of the value be written
#' @param as_ref should references to the cell be written
#' @param transpose should the data be written transposed
#' @return The `wbWorksheet` object, invisibly
copy_cells = function(
sheet = current_sheet(),
dims = "A1",
data,
as_value = FALSE,
as_ref = FALSE,
transpose = FALSE
) {

assert_class(data, "wb_data")
sheet <- private$get_sheet_index(sheet)

to_ncol <- ncol(data) - 1
to_nrow <- nrow(data) - 1

start_col <- col2int(dims)
start_row <- as.integer(gsub("\\D+", "", dims))

to_cols <- seq.int(start_col, start_col + to_ncol)
to_rows <- seq.int(start_row, start_row + to_nrow)

to_dims <- rowcol_to_dims(to_rows, to_cols)
to_dims_i <- dims_to_dataframe(to_dims, fill = FALSE)
to_dims_f <- dims_to_dataframe(to_dims, fill = TRUE)

if (transpose) {
to_dims_i <- as.data.frame(t(to_dims_i))
to_dims_f <- as.data.frame(t(to_dims_f))
}

to_dims_f <- unname(unlist(to_dims_f))

from_sheet <- attr(data, "sheet")
from_dims <- attr(data, "dims")

from_sheet <- wb_validate_sheet(self, from_sheet)
from_dims <- as.character(unlist(from_dims))
cc <- self$worksheets[[from_sheet]]$sheet_data$cc

# TODO improve this. It should use v or inlineStr from cc
if (as_value) {
to_data <- data

if (transpose) {
data <- t(data)
}

self$add_data(sheet = sheet, x = data, dims = to_dims_f[[1]], colNames = FALSE)

return(invisible(self))
}

# initialize dims we write to as empty cells
private$do_cell_init(sheet, to_dims)

to_cc <- cc[match(from_dims, cc$r), ]
from_cells <- to_cc$r
to_cc[c("r", "row_r", "c_r")] <- cbind(
to_dims_f,
gsub("\\D+", "", to_dims_f),
int2col(col2int(to_dims_f))
)

if (as_ref) {
from_sheet_name <- self$get_sheet_names()[[from_sheet]]
to_cc[c("c_t", "c_cm", "c_ph", "c_vm", "v", "f", "f_t", "f_ref", "f_ca", "f_si", "is")] <- ""
to_cc[c("f")] <- paste0(shQuote(from_sheet_name, type = "sh"), "!", from_cells)
}

cc <- self$worksheets[[sheet]]$sheet_data$cc
cc[match(to_dims_f, cc$r), ] <- to_cc

self$worksheets[[sheet]]$sheet_data$cc <- cc

invisible(self)
},

### base font ----

Expand Down
38 changes: 38 additions & 0 deletions man/wbWorkbook.Rd

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

1 change: 1 addition & 0 deletions man/wb_add_chartsheet.Rd

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

1 change: 1 addition & 0 deletions man/wb_add_data_table.Rd

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

1 change: 1 addition & 0 deletions man/wb_add_formula.Rd

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

1 change: 1 addition & 0 deletions man/wb_add_worksheet.Rd

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

1 change: 1 addition & 0 deletions man/wb_clone_worksheet.Rd

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

70 changes: 70 additions & 0 deletions man/wb_copy_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 man/wb_creators.Rd

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

1 change: 1 addition & 0 deletions man/wb_freeze_pane.Rd

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

1 change: 1 addition & 0 deletions man/wb_get_base_font.Rd

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

1 change: 1 addition & 0 deletions man/wb_save.Rd

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

1 change: 1 addition & 0 deletions man/wb_set_col_widths.Rd

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

1 change: 1 addition & 0 deletions man/wb_set_last_modified_by.Rd

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

1 change: 1 addition & 0 deletions man/wb_set_row_heights.Rd

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

1 change: 1 addition & 0 deletions man/wb_workbook.Rd

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

1 change: 1 addition & 0 deletions man/workbook_grouping.Rd

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

Loading