From 9bf0a39c2cca4c66fe518db040af8af677329d6c Mon Sep 17 00:00:00 2001 From: olivroy Date: Wed, 23 Aug 2023 12:52:22 -0400 Subject: [PATCH 1/6] working example. --- NAMESPACE | 1 + NEWS.md | 4 + R/class-comment.R | 121 ++++++++++-------- R/class-workbook-wrappers.R | 14 +- R/class-workbook.R | 4 + R/openxlsx2-package.R | 3 + _pkgdown.yml | 1 + man/comment.Rd | 14 +- man/openxlsx2-deprecated.Rd | 1 + man/openxlsx2_options.Rd | 2 + man/wb_comment.Rd | 35 +++++ man/wb_workbook.Rd | 2 +- tests/testthat/test-class-comment.R | 21 +++ tests/testthat/test-class-workbook-wrappers.R | 9 ++ 14 files changed, 174 insertions(+), 58 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..0b8ffd0f2 100644 --- a/NEWS.md +++ b/NEWS.md @@ -11,11 +11,15 @@ * 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()`. The default for `author` in `wb_comment()` is a bit different from `create_comment()` at it looks in `options("openxlsx2.creator")` if not specified. + Use `wb_comment()` in new code. [758, @olivroy](https://github.com/JanMarvin/openxlsx2/pull/758) ## Internal changes diff --git a/R/class-comment.R b/R/class-comment.R index b2f0c96fb..9b1360753 100644 --- a/R/class-comment.R +++ b/R/class-comment.R @@ -36,7 +36,6 @@ wbComment <- R6::R6Class( #' @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 self$text <- text self$author <- author self$style <- style @@ -76,61 +75,29 @@ wbComment <- R6::R6Class( } ) ) +# 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, write and remove comments -#' -#' The comment functions (create, write and remove) allow the -#' modification of comments. In newer 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()]. +#' Create a comment object #' #' @param text Comment text. Character vector. -#' @param author Author of comment. A 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 width Textbox integer width in number of cells #' @param height Textbox integer height in number of cells -#' @export -#' @rdname comment -#' @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) -#' -#' # remove the first comment c1 -#' wb$remove_comment(1, dims = "B10") -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() - +#' @return A `wbComment` object +#' @export +wb_comment <- function(text = NULL, + style = NULL, + visible = TRUE, + 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") @@ -169,7 +136,59 @@ 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)) +} + +# 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, write and remove comments +#' +#' The comment functions (create, write and remove) allow the +#' modification of comments. In newer 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()]. +#' +#' Use [wb_comment()] in new code +#' +#' @inheritParams wb_comment +#' @author A string, by default, will use "user" +#' @keywords internal +#' @export +#' @rdname comment +#' @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) +#' +#' # remove the first comment c1 +#' wb$remove_comment(1, dims = "B10") +create_comment <- function(text, + author = Sys.info()[["user"]], + style = NULL, + visible = TRUE, + width = 2, + height = 4) { + # .Deprecated("wb_comment()") + wb_comment(text = text, author = author, style = style, visible = visible, width = width, height = height) } #' Internal comment functions @@ -389,9 +408,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 0b393d59d..8bb6ea502 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. @@ -2920,11 +2920,14 @@ wb_add_dxfs_style <- function( } #' Add comment to worksheet +#' +#' @details +#' If applying a `comment` with a string, it will use [wb_comment()] default values. +#' #' @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 comment A comment to apply to the worksheet -# To fit, maybe comment, can be `x` +#' @param comment A comment to apply to `dims` created by [wb_comment()] or a string. #' @param ... additional arguments #' @returns The `wbWorkbook` object #' @seealso [wb_add_thread()] @@ -2939,6 +2942,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 6dcd68c63..efcf782c0 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 fe664006c..dd0cf5fd1 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -50,6 +50,7 @@ reference: - title: create functions contents: - starts_with("create_") + - wb_comment - title: XML functions desc: > diff --git a/man/comment.Rd b/man/comment.Rd index 4b264a9c8..f62560326 100644 --- a/man/comment.Rd +++ b/man/comment.Rd @@ -22,7 +22,8 @@ wb_remove_comment(wb, sheet = current_sheet(), dims = "A1", ...) \arguments{ \item{text}{Comment text. Character vector.} -\item{author}{Author of comment. A string.} +\item{author}{Author of comment. A string. By default, will look at \code{options("openxlsx2.creator")}. +Otherwise, will check the system username.} \item{style}{A Style object or list of style objects the same length as comment vector.} @@ -38,7 +39,7 @@ wb_remove_comment(wb, sheet = current_sheet(), dims = "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()}} or a string.} \item{...}{additional arguments} } @@ -52,6 +53,11 @@ 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()}}. } +\details{ +Use \code{\link[=wb_comment]{wb_comment()}} in new code + +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") @@ -77,3 +83,7 @@ wb$remove_comment(1, dims = "B10") \seealso{ \code{\link[=wb_add_thread]{wb_add_thread()}} } +\author{ +A string, by default, will use "user" +} +\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_comment.Rd b/man/wb_comment.Rd new file mode 100644 index 000000000..d1b0f76cd --- /dev/null +++ b/man/wb_comment.Rd @@ -0,0 +1,35 @@ +% 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 = TRUE, + author = getOption("openxlsx2.creator"), + width = 2, + height = 4 +) +} +\arguments{ +\item{text}{Comment text. Character vector.} + +\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{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{ +Create a comment object +} diff --git a/man/wb_workbook.Rd b/man/wb_workbook.Rd index 899d0b6c6..35355e756 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-class-comment.R b/tests/testthat/test-class-comment.R index cf559c8a0..a5a1d3376 100644 --- a/tests/testthat/test-class-comment.R +++ b/tests/testthat/test-class-comment.R @@ -114,9 +114,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() ----------------------------------------------------- From 6cf1d08d9b428faf9ea0d45dd24e7635a4a6f85a Mon Sep 17 00:00:00 2001 From: olivroy Date: Wed, 23 Aug 2023 13:18:23 -0400 Subject: [PATCH 2/6] Change default to `visible = FALSE` in `wb_comment()` to account for new spreadsheet software behaviour. --- NEWS.md | 7 +++++-- R/class-comment.R | 11 ++++++----- man/comment.Rd | 8 ++------ man/wb_comment.Rd | 4 ++-- tests/testthat/test-class-comment.R | 10 ++++++++-- 5 files changed, 23 insertions(+), 17 deletions(-) diff --git a/NEWS.md b/NEWS.md index 0b8ffd0f2..ae3e0f14c 100644 --- a/NEWS.md +++ b/NEWS.md @@ -18,8 +18,11 @@ * 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()`. The default for `author` in `wb_comment()` is a bit different from `create_comment()` at it looks in `options("openxlsx2.creator")` if not specified. - Use `wb_comment()` in new code. [758, @olivroy](https://github.com/JanMarvin/openxlsx2/pull/758) +* `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`. ## Internal changes diff --git a/R/class-comment.R b/R/class-comment.R index 9b1360753..d56ebebff 100644 --- a/R/class-comment.R +++ b/R/class-comment.R @@ -35,7 +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) { + initialize = function(text, author, style, visible, width, height) { self$text <- text self$author <- author self$style <- style @@ -83,7 +83,7 @@ wbComment <- R6::R6Class( #' @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 #' @@ -91,7 +91,7 @@ wbComment <- R6::R6Class( #' @export wb_comment <- function(text = NULL, style = NULL, - visible = TRUE, + visible = FALSE, author = getOption("openxlsx2.creator"), width = 2, height = 4) { @@ -136,7 +136,7 @@ wb_comment <- function(text = NULL, ) } - invisible(wbComment$new(text = text, author = author, style = style, width = width, height = height)) + invisible(wbComment$new(text = text, author = author, style = style, width = width, height = height, visible = visible)) } # wrappers ---------------------------------------------------------------- @@ -156,7 +156,8 @@ wb_comment <- function(text = NULL, #' Use [wb_comment()] in new code #' #' @inheritParams wb_comment -#' @author A string, by default, will use "user" +#' @param author A string, by default, will use "user" +#' @param visible Default: `TRUE`. Is the comment visible by default? #' @keywords internal #' @export #' @rdname comment diff --git a/man/comment.Rd b/man/comment.Rd index f62560326..371cdd18f 100644 --- a/man/comment.Rd +++ b/man/comment.Rd @@ -22,12 +22,11 @@ wb_remove_comment(wb, sheet = current_sheet(), dims = "A1", ...) \arguments{ \item{text}{Comment text. Character vector.} -\item{author}{Author of comment. A string. By default, will look at \code{options("openxlsx2.creator")}. -Otherwise, will check the system username.} +\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} @@ -83,7 +82,4 @@ wb$remove_comment(1, dims = "B10") \seealso{ \code{\link[=wb_add_thread]{wb_add_thread()}} } -\author{ -A string, by default, will use "user" -} \keyword{internal} diff --git a/man/wb_comment.Rd b/man/wb_comment.Rd index d1b0f76cd..b76a8cbc5 100644 --- a/man/wb_comment.Rd +++ b/man/wb_comment.Rd @@ -7,7 +7,7 @@ wb_comment( text = NULL, style = NULL, - visible = TRUE, + visible = FALSE, author = getOption("openxlsx2.creator"), width = 2, height = 4 @@ -18,7 +18,7 @@ wb_comment( \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}{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.} diff --git a/tests/testthat/test-class-comment.R b/tests/testthat/test-class-comment.R index a5a1d3376..8cb042fcb 100644 --- a/tests/testthat/test-class-comment.R +++ b/tests/testthat/test-class-comment.R @@ -3,6 +3,12 @@ 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) +}) + test_that("create_comment() works", { # error checking expect_silent(create_comment("hi", width = 1)) @@ -19,7 +25,7 @@ test_that("create_comment() works", { 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 +66,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 = F) wb$add_comment(dims = "B10", comment = c1) expect_silent(wb$save(tmp)) From 0eaacc3d502cd7434dbe6627955b286eac1bb933 Mon Sep 17 00:00:00 2001 From: olivroy Date: Wed, 23 Aug 2023 13:35:17 -0400 Subject: [PATCH 3/6] lint --- tests/testthat/test-class-comment.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat/test-class-comment.R b/tests/testthat/test-class-comment.R index 8cb042fcb..2cf3d3a10 100644 --- a/tests/testthat/test-class-comment.R +++ b/tests/testthat/test-class-comment.R @@ -66,7 +66,7 @@ test_that("comments", { wb$add_worksheet() # write comment without author - c1 <- create_comment(text = "this is a comment", author = "", visible = F) + c1 <- create_comment(text = "this is a comment", author = "", visible = FALSE) wb$add_comment(dims = "B10", comment = c1) expect_silent(wb$save(tmp)) From 43de8dc5e88f9b77fee07e36eb47b0d7d39ef582 Mon Sep 17 00:00:00 2001 From: olivroy Date: Wed, 23 Aug 2023 14:07:53 -0400 Subject: [PATCH 4/6] Different behaviour on multiple width and height in `wb_comment()` vs `create_comment()` --- NEWS.md | 1 + R/class-comment.R | 8 +++++--- tests/testthat/test-asserts.R | 1 + tests/testthat/test-class-comment.R | 17 ++++++++++++----- 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/NEWS.md b/NEWS.md index ae3e0f14c..652d69b7e 100644 --- a/NEWS.md +++ b/NEWS.md @@ -23,6 +23,7 @@ * `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 d56ebebff..998c22842 100644 --- a/R/class-comment.R +++ b/R/class-comment.R @@ -100,12 +100,14 @@ wb_comment <- function(text = NULL, 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) @@ -189,7 +191,7 @@ create_comment <- function(text, width = 2, height = 4) { # .Deprecated("wb_comment()") - wb_comment(text = text, author = author, style = style, visible = visible, width = width, height = height) + wb_comment(text = text, author = author, style = style, visible = visible, width = width[1], height = height[1]) } #' Internal comment functions 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 2cf3d3a10..5944572be 100644 --- a/tests/testthat/test-class-comment.R +++ b/tests/testthat/test-class-comment.R @@ -7,18 +7,25 @@ test_that("wb_comment and create_comment are the same except for the different d 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))) From f8bf5e6c0d314d0c32abede73bbed5d1b90cf6d7 Mon Sep 17 00:00:00 2001 From: olivroy Date: Wed, 23 Aug 2023 14:38:16 -0400 Subject: [PATCH 5/6] tweaks to make the overall diff more pleasant to look at. --- R/class-comment.R | 37 ++++++++++++++++++------------------- man/create_comment.Rd | 22 ---------------------- man/wb_comment.Rd | 19 +++++++++++++++++++ 3 files changed, 37 insertions(+), 41 deletions(-) diff --git a/R/class-comment.R b/R/class-comment.R index cbccceae0..d18871d78 100644 --- a/R/class-comment.R +++ b/R/class-comment.R @@ -91,6 +91,24 @@ wbComment <- R6::R6Class( #' #' @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 = "") +#' 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) wb_comment <- function(text = NULL, style = NULL, visible = FALSE, @@ -158,26 +176,7 @@ wb_comment <- function(text = NULL, #' @param visible Default: `TRUE`. Is the comment visible by default? #' @keywords internal #' @returns a `wbComment` object -#' @seealso [wb_add_comment()] #' @export -#' @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) create_comment <- function(text, author = Sys.info()[["user"]], style = NULL, diff --git a/man/create_comment.Rd b/man/create_comment.Rd index 7f1e21cae..3782acbdd 100644 --- a/man/create_comment.Rd +++ b/man/create_comment.Rd @@ -32,26 +32,4 @@ a \code{wbComment} object \description{ Use \code{\link[=wb_comment]{wb_comment()}} in new code. } -\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()}} -} \keyword{internal} diff --git a/man/wb_comment.Rd b/man/wb_comment.Rd index 1c2f4e344..4415cab48 100644 --- a/man/wb_comment.Rd +++ b/man/wb_comment.Rd @@ -33,3 +33,22 @@ 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) +} From bceecb0c788153005e0909130bc7530e274c06eb Mon Sep 17 00:00:00 2001 From: olivroy Date: Wed, 23 Aug 2023 15:00:52 -0400 Subject: [PATCH 6/6] Tweaks and show fmt_txt --- R/class-comment.R | 25 +++++++++++++------------ R/class-workbook-wrappers.R | 11 +++++++---- man/create_comment.Rd | 2 +- man/wb_add_comment.Rd | 11 +++++++---- man/wb_comment.Rd | 8 ++++---- 5 files changed, 32 insertions(+), 25 deletions(-) diff --git a/R/class-comment.R b/R/class-comment.R index d18871d78..ec4c2bee9 100644 --- a/R/class-comment.R +++ b/R/class-comment.R @@ -75,13 +75,17 @@ wbComment <- R6::R6Class( } ) ) -# Comment creation ------------------------------------------------------ +# 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 object #' #' Creates a `wbComment` object. Use with [wb_add_comment()] to add to a worksheet location. #' -#' @param text Comment text. Character vector. +#' @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. @@ -96,17 +100,17 @@ wbComment <- R6::R6Class( #' 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) wb_comment <- function(text = NULL, @@ -161,12 +165,6 @@ wb_comment <- function(text = NULL, invisible(wbComment$new(text = text, author = author, style = style, width = width, height = height, visible = visible)) } -# 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 #' #' Use [wb_comment()] in new code. @@ -183,7 +181,10 @@ create_comment <- function(text, visible = TRUE, width = 2, height = 4) { - # .Deprecated("wb_comment()") + # + 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]) } diff --git a/R/class-workbook-wrappers.R b/R/class-workbook-wrappers.R index ddafdbb02..fe76ff526 100644 --- a/R/class-workbook-wrappers.R +++ b/R/class-workbook-wrappers.R @@ -2973,23 +2973,26 @@ wb_add_dxfs_style <- function( #' @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 comment A comment to apply to `dims` created by [wb_comment()] or a string. +#' @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 diff --git a/man/create_comment.Rd b/man/create_comment.Rd index 3782acbdd..0134b8f7c 100644 --- a/man/create_comment.Rd +++ b/man/create_comment.Rd @@ -14,7 +14,7 @@ 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}{A string, by default, will use "user"} diff --git a/man/wb_add_comment.Rd b/man/wb_add_comment.Rd index d4d1fdac5..73333dd00 100644 --- a/man/wb_add_comment.Rd +++ b/man/wb_add_comment.Rd @@ -16,7 +16,7 @@ wb_remove_comment(wb, sheet = current_sheet(), dims = "A1", ...) \item{dims}{Optional row and column as spreadsheet dimension, e.g. "A1"} -\item{comment}{A comment to apply to \code{dims} created by \code{\link[=wb_comment]{wb_comment()}} or a string.} +\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} } @@ -33,15 +33,18 @@ If applying a \code{comment} with a string, it will use \code{\link[=wb_comment] 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 index 4415cab48..54f80a1ab 100644 --- a/man/wb_comment.Rd +++ b/man/wb_comment.Rd @@ -14,7 +14,7 @@ wb_comment( ) } \arguments{ -\item{text}{Comment text. Character vector.} +\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.} @@ -38,17 +38,17 @@ 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) }