From 5d7668f415522ffa29a1f4a700c8ac09205d7fd2 Mon Sep 17 00:00:00 2001 From: olivroy <52606734+olivroy@users.noreply.github.com> Date: Wed, 23 Aug 2023 16:17:53 -0400 Subject: [PATCH] Export `wb_comment()` (#758) * working example. * Change default to `visible = FALSE` in `wb_comment()` to account for new spreadsheet software behaviour. * lint * Different behaviour on multiple width and height in `wb_comment()` vs `create_comment()` * tweaks to make the overall diff more pleasant to look at. * Tweaks and show fmt_txt --- NAMESPACE | 1 + NEWS.md | 8 ++ R/class-comment.R | 80 ++++++++++++------- R/class-workbook-wrappers.R | 34 ++++---- R/class-workbook.R | 4 + R/openxlsx2-package.R | 3 + _pkgdown.yml | 1 + man/create_comment.Rd | 31 ++----- man/openxlsx2-deprecated.Rd | 1 + man/openxlsx2_options.Rd | 2 + man/wb_add_comment.Rd | 27 ++++--- man/wb_comment.Rd | 54 +++++++++++++ man/wb_workbook.Rd | 2 +- tests/testthat/test-asserts.R | 1 + tests/testthat/test-class-comment.R | 48 +++++++++-- tests/testthat/test-class-workbook-wrappers.R | 9 +++ 16 files changed, 213 insertions(+), 93 deletions(-) create mode 100644 man/wb_comment.Rd diff --git a/NAMESPACE b/NAMESPACE index c334d380e..27dc29fcf 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -76,6 +76,7 @@ export(wb_clone_sheet_style) export(wb_clone_worksheet) export(wb_color) export(wb_colour) +export(wb_comment) export(wb_copy_cells) export(wb_data) export(wb_dims) diff --git a/NEWS.md b/NEWS.md index a11bb945a..652d69b7e 100644 --- a/NEWS.md +++ b/NEWS.md @@ -11,11 +11,19 @@ * provide solve argument for `wb_merge_cells()`. This allows to solve cell intersecting regions. [733](https://github.com/JanMarvin/openxlsx2/pull/733) +* `wb_add_comment(comment = "x")` no longer errors when a comment as a character vector no longer fails [758, @olivroy](https://github.com/JanMarvin/openxlsx2/pull/758) + ## Breaking changes * no longer exporting `wb_get_sheet_name()` * deprecating `delete_data()` and minor improvements to `wb_clean_sheet()` * removing `wb_get_worksheet()`, `wb_ws()`. These never worked as expected. +* `create_comment()` has been renamed `wb_comment()`. [758, @olivroy](https://github.com/JanMarvin/openxlsx2/pull/758) The default arguments however are changed: + + * `author` looks at `options("openxlsx2.creator")` in `wb_comment()` compared to only `sys.getenv("user")` in `create_comment()` + * `visible` defaults to `FALSE` in `wb_comment()` to account for modern spreadsheet software behaviour. + In `create_comment()`, it is `TRUE`. + * `width` and `height` must now be of length 1. (In `create_comment()`, the first element is taken, other are ignored.) ## Internal changes diff --git a/R/class-comment.R b/R/class-comment.R index a61333bcc..ec4c2bee9 100644 --- a/R/class-comment.R +++ b/R/class-comment.R @@ -35,8 +35,7 @@ wbComment <- R6::R6Class( #' @param width Width of the comment in ... units #' @param height Height of comment in ... units #' @return a `wbComment` object - initialize = function(text, author, style, visible = TRUE, width = 2, height = 4) { - # TODO this needs the validations that the comment wrappers have + initialize = function(text, author, style, visible, width, height) { self$text <- text self$author <- author self$style <- style @@ -76,63 +75,63 @@ wbComment <- R6::R6Class( } ) ) - - -# wrappers ---------------------------------------------------------------- +# Comment creation wrappers ---------------------------------------------------------------- # TODO create_comment() should leverage wbComment$new() more # TODO write_comment() should leverage wbWorkbook$addComment() more # TODO remove_comment() should leverage wbWorkbook$remove_comment() more -#' Create a comment +#' Create a comment object #' #' Creates a `wbComment` object. Use with [wb_add_comment()] to add to a worksheet location. #' -#' @param text Comment text. Character vector. -#' @param author Author of comment. A string. +#' @param text Comment text. Character vector. or a [fmt_txt()] string. +#' @param author Author of comment. A string. By default, will look at `options("openxlsx2.creator")`. +#' Otherwise, will check the system username. #' @param style A Style object or list of style objects the same length as comment vector. -#' @param visible `TRUE` or `FALSE`. Is comment visible? +#' @param visible Is comment visible? Default: `FALSE`. #' @param width Textbox integer width in number of cells #' @param height Textbox integer height in number of cells -#' @returns a `wbComment` object -#' @seealso [wb_add_comment()] +#' +#' @return A `wbComment` object #' @export #' @examples #' wb <- wb_workbook() #' wb$add_worksheet("Sheet 1") #' #' # write comment without author -#' c1 <- create_comment(text = "this is a comment", author = "") +#' c1 <- wb_comment(text = "this is a comment", author = "", visible = TRUE) #' wb$add_comment(dims = "B10", comment = c1) #' #' # Write another comment with author information -#' c2 <- create_comment(text = "this is another comment", author = "Marco Polo") +#' c2 <- wb_comment(text = "this is another comment", author = "Marco Polo") #' wb$add_comment(sheet = 1, dims = "C10", comment = c2) #' #' # write a styled comment with system author #' s1 <- create_font(b = "true", color = wb_color(hex = "FFFF0000"), sz = "12") #' s2 <- create_font(color = wb_color(hex = "FF000000"), sz = "9") -#' c3 <- create_comment(text = c("This Part Bold red\n\n", "This part black"), style = c(s1, s2)) +#' c3 <- wb_comment(text = c("This Part Bold red\n\n", "This part black"), style = c(s1, s2)) #' #' wb$add_comment(sheet = 1, dims = wb_dims(3, 6), comment = c3) -create_comment <- function(text, - author = Sys.info()[["user"]], - style = NULL, - visible = TRUE, - width = 2, - height = 4) { - - # TODO move this to wbComment$new(); this could then be replaced with - # wb_comment() - +wb_comment <- function(text = NULL, + style = NULL, + visible = FALSE, + author = getOption("openxlsx2.creator"), + width = 2, + height = 4) { + # Code copied from the wbWorkbook + author <- author %||% Sys.getenv("USERNAME") + text <- text %||% "" assert_class(author, "character") assert_class(text, "character") - assert_class(width, "numeric") - assert_class(height, "numeric") + assert_class(width, c("numeric", "integer")) + assert_class(height, c("numeric", "integer")) assert_class(visible, "logical") if (length(visible) > 1) stop("visible must be a single logical") if (length(author) > 1) stop("author) must be a single character") + if (length(width) > 1) stop("width must be a single integer") + if (length(height) > 1) stop("height must be a single integer") width <- round(width) height <- round(height) @@ -163,7 +162,30 @@ create_comment <- function(text, ) } - invisible(wbComment$new(text = text, author = author, style = style, visible = visible, width = width[1], height = height[1])) + invisible(wbComment$new(text = text, author = author, style = style, width = width, height = height, visible = visible)) +} + +#' Create a comment +#' +#' Use [wb_comment()] in new code. +#' +#' @inheritParams wb_comment +#' @param author A string, by default, will use "user" +#' @param visible Default: `TRUE`. Is the comment visible by default? +#' @keywords internal +#' @returns a `wbComment` object +#' @export +create_comment <- function(text, + author = Sys.info()[["user"]], + style = NULL, + visible = TRUE, + width = 2, + height = 4) { + # + if (getOption("openxlsx2.soon_deprecated", default = FALSE)) { + .Deprecated("wb_comment()") + } + wb_comment(text = text, author = author, style = style, visible = visible, width = width[1], height = height[1]) } #' Internal comment functions @@ -383,9 +405,7 @@ remove_comment <- function( } -wb_comment <- function(text = character(), author = character(), style = character()) { - wbComment$new(text = text, author = author, style = style) -} + as_fmt_txt <- function(x) { vapply(x, function(y) { diff --git a/R/class-workbook-wrappers.R b/R/class-workbook-wrappers.R index 29007e3a9..fe76ff526 100644 --- a/R/class-workbook-wrappers.R +++ b/R/class-workbook-wrappers.R @@ -10,7 +10,7 @@ #' "Old Office Theme", "Organic", "Parallax", "Parcel", "Retrospect", #' "Savon", "Slice", "Vapor Trail", "View", "Wisp", "Wood Type" #' -#' @param creator Creator of the workbook (your name). Defaults to login username +#' @param creator Creator of the workbook (your name). Defaults to login username or `options("openxlsx2.creator")` if set. #' @param title,subject,category Workbook property, a string. #' @param datetime_created The time of the workbook is created #' @param theme Optional theme identified by string or number. @@ -2965,37 +2965,34 @@ wb_add_dxfs_style <- function( } -#' Add / remove comment in a worksheet +#' Add comment to worksheet #' -#' @description -#' The comment functions (add and remove) allow the modification of comments. -#' In more recent spreadsheet software they are called notes, while they are called -#' comments in openxml. Modification of what newer spreadsheet software now calls -#' comment is possible via [wb_add_thread()]. -#' -#' `comment` can be created with [create_comment()] to add styling. +#' @details +#' If applying a `comment` with a string, it will use [wb_comment()] default values. #' -#' @param wb A Workbook object +#' @param wb A workbook object #' @param sheet A worksheet of the workbook -#' @param dims Row and column as spreadsheet dimension, e.g. "A1" -#' @param comment A comment to apply to the worksheet -# TODO To fit, maybe comment, can be `x` +#' @param dims Optional row and column as spreadsheet dimension, e.g. "A1" +#' @param comment A comment to apply to `dims` created by [wb_comment()], a string or a [fmt_txt()] object #' @param ... additional arguments #' @returns The Workbook object, invisibly. -#' @seealso [create_comment()], [wb_add_thread()] +#' @seealso [wb_comment()], [wb_add_thread()] #' @name wb_add_comment #' @keywords comments #' @examples #' wb <- wb_workbook() #' wb$add_worksheet("Sheet 1") #' # add a comment without author -#' c1 <- create_comment(text = "this is a comment", author = "") +#' c1 <- wb_comment(text = "this is a comment", author = "") #' wb$add_comment(dims = "B10", comment = c1) #' #' # Remove comment #' wb$remove_comment(sheet = "Sheet 1", dims = "B10") #' # Write another comment with author information -#' c2 <- create_comment(text = "this is another comment", author = "Marco Polo") +#' c2 <- wb_comment(text = "this is another comment", author = "Marco Polo", visible = TRUE) #' wb$add_comment(sheet = 1, dims = "C10", comment = c2) +#' # Works with formatted text also. +#' formatted_text <- fmt_txt("bar", underline = TRUE) +#' wb$add_comment(dims = "B5", comment = formatted_text) NULL #' @rdname wb_add_comment #' @export @@ -3008,6 +3005,11 @@ wb_add_comment <- function( ) { assert_workbook(wb) + + if (is.character(comment)) { + comment <- wb_comment(text = comment, author = getOption("openxlsx2.creator")) + } + assert_comment(comment) wb$clone()$add_comment( diff --git a/R/class-workbook.R b/R/class-workbook.R index c2a6c54ea..76b20b374 100644 --- a/R/class-workbook.R +++ b/R/class-workbook.R @@ -3747,6 +3747,10 @@ wbWorkbook <- R6::R6Class( dims <- rowcol_to_dim(row, col) } + if (is.character(comment)) { + comment <- wb_comment(text = comment, author = getOption("openxlsx2.creator")) + } + write_comment( wb = self, sheet = sheet, diff --git a/R/openxlsx2-package.R b/R/openxlsx2-package.R index ce518aa94..9213a5924 100644 --- a/R/openxlsx2-package.R +++ b/R/openxlsx2-package.R @@ -73,6 +73,8 @@ #' * `options("openxlsx2.orientation" = "portrait")` ## page orientation #' * `options("openxlsx2.sheet.default_name" = "Sheet")` #' * `options("openxlsx2.rightToLeft" = NULL)` +#' * `options("openxlsx2.soon_deprecated" = FALSE)` ## warn if using camelCase +#' * `options("openxlsx2.creator")` ## Default is Windows Username #' #' @name openxlsx2_options NULL @@ -103,6 +105,7 @@ openxlsx2_celltype <- c( #' * [convertToExcelDate()] -> [convert_to_excel_date()] #' * [wb_grid_lines()] -> [wb_set_grid_lines()] #' * [delete_data()] -> [wb_clean_sheet()] +#' * [create_comment()] -> [wb_comment()] #' @seealso [.Deprecated] #' @name openxlsx2-deprecated NULL diff --git a/_pkgdown.yml b/_pkgdown.yml index 1fb5579ed..fddfe7d59 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -92,6 +92,7 @@ reference: contents: - wb_dims - wb_color + - wb_comment - wb_hyperlink - starts_with("create_") diff --git a/man/create_comment.Rd b/man/create_comment.Rd index 53749ddb1..0134b8f7c 100644 --- a/man/create_comment.Rd +++ b/man/create_comment.Rd @@ -14,13 +14,13 @@ create_comment( ) } \arguments{ -\item{text}{Comment text. Character vector.} +\item{text}{Comment text. Character vector. or a \code{\link[=fmt_txt]{fmt_txt()}} string.} -\item{author}{Author of comment. A string.} +\item{author}{A string, by default, will use "user"} \item{style}{A Style object or list of style objects the same length as comment vector.} -\item{visible}{\code{TRUE} or \code{FALSE}. Is comment visible?} +\item{visible}{Default: \code{TRUE}. Is the comment visible by default?} \item{width}{Textbox integer width in number of cells} @@ -30,27 +30,6 @@ create_comment( a \code{wbComment} object } \description{ -Creates a \code{wbComment} object. Use with \code{\link[=wb_add_comment]{wb_add_comment()}} to add to a worksheet location. -} -\examples{ -wb <- wb_workbook() -wb$add_worksheet("Sheet 1") - -# write comment without author -c1 <- create_comment(text = "this is a comment", author = "") -wb$add_comment(dims = "B10", comment = c1) - -# Write another comment with author information -c2 <- create_comment(text = "this is another comment", author = "Marco Polo") -wb$add_comment(sheet = 1, dims = "C10", comment = c2) - -# write a styled comment with system author -s1 <- create_font(b = "true", color = wb_color(hex = "FFFF0000"), sz = "12") -s2 <- create_font(color = wb_color(hex = "FF000000"), sz = "9") -c3 <- create_comment(text = c("This Part Bold red\n\n", "This part black"), style = c(s1, s2)) - -wb$add_comment(sheet = 1, dims = wb_dims(3, 6), comment = c3) -} -\seealso{ -\code{\link[=wb_add_comment]{wb_add_comment()}} +Use \code{\link[=wb_comment]{wb_comment()}} in new code. } +\keyword{internal} diff --git a/man/openxlsx2-deprecated.Rd b/man/openxlsx2-deprecated.Rd index 9ee5e4956..a197acb3b 100644 --- a/man/openxlsx2-deprecated.Rd +++ b/man/openxlsx2-deprecated.Rd @@ -11,6 +11,7 @@ These functions are provided for compatibility with older versions of \code{open \item \code{\link[=convertToExcelDate]{convertToExcelDate()}} -> \code{\link[=convert_to_excel_date]{convert_to_excel_date()}} \item \code{\link[=wb_grid_lines]{wb_grid_lines()}} -> \code{\link[=wb_set_grid_lines]{wb_set_grid_lines()}} \item \code{\link[=delete_data]{delete_data()}} -> \code{\link[=wb_clean_sheet]{wb_clean_sheet()}} +\item \code{\link[=create_comment]{create_comment()}} -> \code{\link[=wb_comment]{wb_comment()}} } } \seealso{ diff --git a/man/openxlsx2_options.Rd b/man/openxlsx2_options.Rd index d0797d33f..f4acf366e 100644 --- a/man/openxlsx2_options.Rd +++ b/man/openxlsx2_options.Rd @@ -17,5 +17,7 @@ The openxlsx2 package uses global options to simplify formatting: \item \code{options("openxlsx2.orientation" = "portrait")} ## page orientation \item \code{options("openxlsx2.sheet.default_name" = "Sheet")} \item \code{options("openxlsx2.rightToLeft" = NULL)} +\item \code{options("openxlsx2.soon_deprecated" = FALSE)} ## warn if using camelCase +\item \code{options("openxlsx2.creator")} ## Default is Windows Username } } diff --git a/man/wb_add_comment.Rd b/man/wb_add_comment.Rd index d3bad2814..73333dd00 100644 --- a/man/wb_add_comment.Rd +++ b/man/wb_add_comment.Rd @@ -3,20 +3,20 @@ \name{wb_add_comment} \alias{wb_add_comment} \alias{wb_remove_comment} -\title{Add / remove comment in a worksheet} +\title{Add comment to worksheet} \usage{ wb_add_comment(wb, sheet = current_sheet(), dims = "A1", comment, ...) wb_remove_comment(wb, sheet = current_sheet(), dims = "A1", ...) } \arguments{ -\item{wb}{A Workbook object} +\item{wb}{A workbook object} \item{sheet}{A worksheet of the workbook} -\item{dims}{Row and column as spreadsheet dimension, e.g. "A1"} +\item{dims}{Optional row and column as spreadsheet dimension, e.g. "A1"} -\item{comment}{A comment to apply to the worksheet} +\item{comment}{A comment to apply to \code{dims} created by \code{\link[=wb_comment]{wb_comment()}}, a string or a \code{\link[=fmt_txt]{fmt_txt()}} object} \item{...}{additional arguments} } @@ -24,26 +24,27 @@ wb_remove_comment(wb, sheet = current_sheet(), dims = "A1", ...) The Workbook object, invisibly. } \description{ -The comment functions (add and remove) allow the modification of comments. -In more recent spreadsheet software they are called notes, while they are called -comments in openxml. Modification of what newer spreadsheet software now calls -comment is possible via \code{\link[=wb_add_thread]{wb_add_thread()}}. - -\code{comment} can be created with \code{\link[=create_comment]{create_comment()}} to add styling. +Add comment to worksheet +} +\details{ +If applying a \code{comment} with a string, it will use \code{\link[=wb_comment]{wb_comment()}} default values. } \examples{ wb <- wb_workbook() wb$add_worksheet("Sheet 1") # add a comment without author -c1 <- create_comment(text = "this is a comment", author = "") +c1 <- wb_comment(text = "this is a comment", author = "") wb$add_comment(dims = "B10", comment = c1) #' # Remove comment wb$remove_comment(sheet = "Sheet 1", dims = "B10") # Write another comment with author information -c2 <- create_comment(text = "this is another comment", author = "Marco Polo") +c2 <- wb_comment(text = "this is another comment", author = "Marco Polo", visible = TRUE) wb$add_comment(sheet = 1, dims = "C10", comment = c2) +# Works with formatted text also. +formatted_text <- fmt_txt("bar", underline = TRUE) +wb$add_comment(dims = "B5", comment = formatted_text) } \seealso{ -\code{\link[=create_comment]{create_comment()}}, \code{\link[=wb_add_thread]{wb_add_thread()}} +\code{\link[=wb_comment]{wb_comment()}}, \code{\link[=wb_add_thread]{wb_add_thread()}} } \keyword{comments} diff --git a/man/wb_comment.Rd b/man/wb_comment.Rd new file mode 100644 index 000000000..54f80a1ab --- /dev/null +++ b/man/wb_comment.Rd @@ -0,0 +1,54 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/class-comment.R +\name{wb_comment} +\alias{wb_comment} +\title{Create a comment object} +\usage{ +wb_comment( + text = NULL, + style = NULL, + visible = FALSE, + author = getOption("openxlsx2.creator"), + width = 2, + height = 4 +) +} +\arguments{ +\item{text}{Comment text. Character vector. or a \code{\link[=fmt_txt]{fmt_txt()}} string.} + +\item{style}{A Style object or list of style objects the same length as comment vector.} + +\item{visible}{Is comment visible? Default: \code{FALSE}.} + +\item{author}{Author of comment. A string. By default, will look at \code{options("openxlsx2.creator")}. +Otherwise, will check the system username.} + +\item{width}{Textbox integer width in number of cells} + +\item{height}{Textbox integer height in number of cells} +} +\value{ +A \code{wbComment} object +} +\description{ +Creates a \code{wbComment} object. Use with \code{\link[=wb_add_comment]{wb_add_comment()}} to add to a worksheet location. +} +\examples{ +wb <- wb_workbook() +wb$add_worksheet("Sheet 1") + +# write comment without author +c1 <- wb_comment(text = "this is a comment", author = "", visible = TRUE) +wb$add_comment(dims = "B10", comment = c1) + +# Write another comment with author information +c2 <- wb_comment(text = "this is another comment", author = "Marco Polo") +wb$add_comment(sheet = 1, dims = "C10", comment = c2) + +# write a styled comment with system author +s1 <- create_font(b = "true", color = wb_color(hex = "FFFF0000"), sz = "12") +s2 <- create_font(color = wb_color(hex = "FF000000"), sz = "9") +c3 <- wb_comment(text = c("This Part Bold red\n\n", "This part black"), style = c(s1, s2)) + +wb$add_comment(sheet = 1, dims = wb_dims(3, 6), comment = c3) +} diff --git a/man/wb_workbook.Rd b/man/wb_workbook.Rd index 17d93e92b..585c8a334 100644 --- a/man/wb_workbook.Rd +++ b/man/wb_workbook.Rd @@ -15,7 +15,7 @@ wb_workbook( ) } \arguments{ -\item{creator}{Creator of the workbook (your name). Defaults to login username} +\item{creator}{Creator of the workbook (your name). Defaults to login username or \code{options("openxlsx2.creator")} if set.} \item{title, subject, category}{Workbook property, a string.} diff --git a/tests/testthat/test-asserts.R b/tests/testthat/test-asserts.R index 0f678d8cb..3d729b10f 100644 --- a/tests/testthat/test-asserts.R +++ b/tests/testthat/test-asserts.R @@ -2,6 +2,7 @@ test_that("assert_class() works", { expect_null(assert_class(2L, "integer")) expect_null(assert_class(2, "numeric")) + expect_error(assert_class(2, "integer")) expect_null(assert_class(2, c("numeric", "integer"))) expect_null(assert_class("2", "character")) expect_null(assert_class(Sys.Date(), "Date")) diff --git a/tests/testthat/test-class-comment.R b/tests/testthat/test-class-comment.R index cf559c8a0..5944572be 100644 --- a/tests/testthat/test-class-comment.R +++ b/tests/testthat/test-class-comment.R @@ -3,23 +3,36 @@ test_that("class wbComment works", { expect_null(assert_comment(wb_comment())) }) +test_that("wb_comment and create_comment are the same except for the different defaults", { + c1 <- create_comment("x1", author = "") + c1_wb <- wb_comment("x1", visible = TRUE, author = "") + expect_equal(c1, c1_wb) + # create_comment drops multiple widths and heights silently. + # wb_comment errors in this case + expect_silent(create_comment(text = "x", author = "", width = c(1, 2))) + expect_error(wb_comment(text = "x", author = "", width = c(1, 2)), "width must be a single") + +}) + test_that("create_comment() works", { # error checking expect_silent(create_comment("hi", width = 1)) - expect_error(create_comment("hi", width = 1L)) - expect_error(create_comment("hi", width = 1:2)) + expect_silent(create_comment("hi", width = 1L)) + expect_silent(create_comment("hi", width = c(1, 2))) + expect_silent(create_comment("hi", width = 1:2)) + expect_error(wb_comment("hi", width = 1:2), regexp = "width must be a") expect_silent(create_comment("hi", height = 1)) - expect_error(create_comment("hi", height = 1L)) - expect_error(create_comment("hi", height = 1:2)) - + expect_silent(create_comment("hi", height = 1L)) + expect_silent(create_comment("hi", height = 1:2)) + expect_error(wb_comment("hi", height = 1:2)) expect_error(create_comment("hi", visible = NULL)) expect_error(create_comment("hi", visible = c(TRUE, FALSE))) expect_error(create_comment("hi", author = 1)) expect_error(create_comment("hi", author = c("a", "a"))) - expect_true(inherits(create_comment("Hello"), "wbComment")) + expect_s3_class(create_comment("Hello"), "wbComment") }) @@ -60,7 +73,7 @@ test_that("comments", { wb$add_worksheet() # write comment without author - c1 <- create_comment(text = "this is a comment", author = "") + c1 <- create_comment(text = "this is a comment", author = "", visible = FALSE) wb$add_comment(dims = "B10", comment = c1) expect_silent(wb$save(tmp)) @@ -114,9 +127,30 @@ test_that("wb_add_comment", { wb_workbook()$add_worksheet()$add_comment(dims = "A1"), 'argument "comment" is missing, with no default' ) +}) + +test_that("wb_add_comment() works without supplying a wbComment object.", { + # Do not alter the workspace + opt <- getOption("openxlsx2.creator") + options("openxlsx2.creator" = "user") + # Using the new default values of wb_comment() (options("openxlsx2.creators)) + wb <- wb_workbook()$add_worksheet()$add_comment(comment = "this is a comment", dims = "A1") + c2 <- wb_comment(text = "this is a comment") + wb2 <- wb_workbook() %>% + wb_add_worksheet() %>% + wb_add_comment(dims = "A1", comment = c2) + # wb_comment() defaults and comment = "text" defaults are the same. + expect_equal(wb$comments, wb2$comments) + + # The wrapper behaves the same + wb3 <- wb_workbook()$add_worksheet()$add_comment(comment = "this is a comment") + expect_equal(wb$comments, wb3$comments) + + options("openxlsx2.creator" = opt) }) + test_that("wb_remove_comment", { c1 <- create_comment(text = "this is a comment", author = "") diff --git a/tests/testthat/test-class-workbook-wrappers.R b/tests/testthat/test-class-workbook-wrappers.R index 2318b2949..94e8def9a 100644 --- a/tests/testthat/test-class-workbook-wrappers.R +++ b/tests/testthat/test-class-workbook-wrappers.R @@ -375,7 +375,16 @@ test_that("wb_add_comment() is a wrapper", { wb = wb, params = list(comment = c1, dims = "A1") ) + opt <- getOption("openxlsx2.creator") + options("openxlsx2.creator" = "user") + wb <- wb_workbook()$add_worksheet() + expect_wrapper( + "add_comment", + wb = wb, + params = list(comment = "a new comment", dims = "A1") + ) + options("openxlsx2.creator" = opt) }) # wb_remove_comment() -----------------------------------------------------