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

Misc #701

Merged
merged 5 commits into from
Jul 20, 2023
Merged

Misc #701

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
13 changes: 8 additions & 5 deletions R/class-comment.R
Original file line number Diff line number Diff line change
Expand Up @@ -171,15 +171,20 @@ create_comment <- function(text,
invisible(wbComment$new(text = text, author = author, style = style, visible = visible, width = width[1], height = height[1]))
}

#' Internal comment functions
#'
#' Users are advised to use [wb_add_comment()] and [wb_remove_comment()]
#' @name comment_internal
NULL

#' @name write_comment
#' @rdname comment_internal
#' @param wb A workbook object
#' @param sheet A vector of names or indices of worksheets
#' @param col Column a column number of letter. For `remove_comment` this can be a range.
#' @param row A row number. For `remove_comment` this can be a range.
#' @param comment A Comment object. See [create_comment()].
#' @param dims worksheet cell "A1"
#' @rdname comment
#' @keywords internal
#' @export
write_comment <- function(
wb,
Expand Down Expand Up @@ -335,11 +340,9 @@ write_comment <- function(
invisible(wb)
}


#' @name remove_comment
#' @rdname comment_internal
#' @param gridExpand If `TRUE`, all data in rectangle min(rows):max(rows) X min(cols):max(cols)
#' will be removed.
#' @rdname comment
#' @keywords internal
#' @export
remove_comment <- function(
Expand Down
42 changes: 17 additions & 25 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 @@ -2929,6 +2928,7 @@ wb_add_dxfs_style <- function(
#' @param ... additional arguments
#' @returns The `wbWorkbook` object
#' @seealso [wb_add_thread()]
#' @rdname comment
#' @export
wb_add_comment <- function(
wb,
Expand All @@ -2950,11 +2950,6 @@ wb_add_comment <- function(
}

#' Remove comment from worksheet
#' @param wb A workbook object
#' @param sheet A worksheet of the workbook
#' @param dims Optional row and column as spreadsheet dimension, e.g. "A1"
#' @param ... additional arguments
#' @returns The `wbWorkbook` object
#' @rdname comment
#' @export
wb_remove_comment <- function(
Expand Down Expand Up @@ -3099,12 +3094,11 @@ 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.
#' @param params Additional parameters passed. See **Details** for more
#' @param ... additional arguments
#' @details See Examples.
#'
#' @details
Expand Down Expand Up @@ -3169,8 +3163,6 @@ wb_add_conditional_formatting <- function(
wb,
sheet = current_sheet(),
dims = NULL,
cols = NULL,
rows = NULL,
rule = NULL,
style = NULL,
type = c(
Expand All @@ -3189,18 +3181,18 @@ wb_add_conditional_formatting <- function(
border = TRUE,
percent = FALSE,
rank = 5L
)
),
...
) {
assert_workbook(wb)
wb$clone()$add_conditional_formatting(
sheet = sheet,
dims = dims,
cols = cols,
rows = rows,
rule = rule,
style = style,
type = type,
params = params
sheet = sheet,
dims = dims,
rule = rule,
style = style,
type = type,
params = params,
... = ...
)
}

Expand Down
79 changes: 67 additions & 12 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 Expand Up @@ -3874,18 +3916,15 @@ wbWorkbook <- R6::R6Class(
#' @description Add conditional formatting
#' @param sheet sheet
#' @param dims dims
#' @param cols cols
#' @param rows rows
#' @param rule rule
#' @param style style
#' @param type type
#' @param params Additional parameters
#' @param ... additional arguments
#' @returns The `wbWorkbook` object
add_conditional_formatting = function(
sheet = current_sheet(),
dims = NULL,
cols = NULL,
rows = NULL,
rule = NULL,
style = NULL,
# TODO add vector of possible values
Expand All @@ -3905,18 +3944,34 @@ wbWorkbook <- R6::R6Class(
border = TRUE,
percent = FALSE,
rank = 5L
)
),
...
) {

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, as_integer = TRUE)
rows <- ddims[[2]]
cols <- ddims[[1]]

if (!is.null(style)) assert_class(style, "character")
assert_class(type, "character")
assert_class(params, "list")

type <- match.arg(type)

if (length(cols) > 2)
warning("cols > 2, will create range from min to max.")

## rows and cols
if (!is.null(cols) && !is.null(rows)) {
if (!is.numeric(cols)) {
Expand Down
34 changes: 6 additions & 28 deletions man/comment.Rd

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

46 changes: 46 additions & 0 deletions man/comment_internal.Rd

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

Loading