Skip to content

Commit

Permalink
[wb_comment] modify the background color
Browse files Browse the repository at this point in the history
  • Loading branch information
JanMarvin committed Dec 11, 2023
1 parent f77f351 commit f9603ca
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 8 deletions.
4 changes: 2 additions & 2 deletions R/baseXML.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ genBaseContent_Type <- function() {
)
}

genBaseShapeVML <- function(clientData, id) {
genBaseShapeVML <- function(clientData, id, fillcolor) {
if (grepl("visible", clientData, ignore.case = TRUE)) {
visible <- "visible"
} else {
Expand All @@ -23,7 +23,7 @@ genBaseShapeVML <- function(clientData, id) {
paste0(
sprintf('<v:shape id="_x0000_s%s" type="#_x0000_t202" style=\'position:absolute;', id),
sprintf('margin-left:107.25pt;margin-top:172.5pt;width:147pt;height:96pt;z-index:1;
visibility:%s;mso-wrap-style:tight\' fillcolor="#ffffe1" o:insetmode="auto">', visible),
visibility:%s;mso-wrap-style:tight\' fillcolor="%s" o:insetmode="auto">', visible, fillcolor),
'<v:fill color2="#ffffe1"/>
<v:shadow color="black" obscured="t"/>
<v:path o:connecttype="none"/>
Expand Down
20 changes: 18 additions & 2 deletions R/class-comment.R
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ NULL
#' @inheritParams wb_add_comment
#' @param comment An object created by [create_comment()]
#' @param row,col Row and column of the cell
#' @param color optional background color
#' @keywords internal
#' @export
#' @inherit wb_add_comment examples
Expand All @@ -207,7 +208,8 @@ write_comment <- function(
col = NULL,
row = NULL,
comment,
dims = rowcol_to_dim(row, col)
dims = rowcol_to_dim(row, col),
color = NULL
) {

# TODO add as method: wbWorkbook$addComment(); add param for replace?
Expand Down Expand Up @@ -264,9 +266,23 @@ write_comment <- function(

id <- 1025 + sum(lengths(wb$comments))

fillcolor <- color %||% "#ffffe1"
# looks like vml accepts only "#RGB" and not "ARGB"
if (is_wbColour(fillcolor)) {
if (names(fillcolor) != "rgb") {
# actually there are more colors like: "lime [11]" and
# "infoBackground [80]" (the default). But no clue how
# these are created.
stop("fillcolor needs to be an RGB color")
}

fillcolor <- paste0("#", substr(fillcolor, 3, 8))
}


# create new commment vml
cd <- unapply(comment_list, "[[", "clientData")
vml_xml <- read_xml(genBaseShapeVML(cd, id), pointer = FALSE)
vml_xml <- read_xml(genBaseShapeVML(cd, id, fillcolor), pointer = FALSE)
vml_comment <- '<o:shapelayout v:ext="edit"><o:idmap v:ext="edit" data="1"/></o:shapelayout><v:shapetype id="_x0000_t202" coordsize="21600,21600" o:spt="202" path="m,l,21600r21600,l21600,xe"><v:stroke joinstyle="miter"/><v:path gradientshapeok="t" o:connecttype="rect"/></v:shapetype>'
vml_xml <- paste0(vml_xml, vml_comment)

Expand Down
2 changes: 1 addition & 1 deletion R/class-workbook-wrappers.R
Original file line number Diff line number Diff line change
Expand Up @@ -3162,7 +3162,7 @@ wb_add_dxfs_style <- function(
#' Add comment to worksheet
#'
#' @details
#' If applying a `comment` with a string, it will use [wb_comment()] default values.
#' If applying a `comment` with a string, it will use [wb_comment()] default values. If additional background colors are applied, RGB colors should be provided, either as hex code or with builtin R colors. The alpha chanell is ignored.
#'
#' @param wb A workbook object
#' @param sheet A worksheet of the workbook
Expand Down
7 changes: 6 additions & 1 deletion R/class-workbook.R
Original file line number Diff line number Diff line change
Expand Up @@ -4135,6 +4135,7 @@ wbWorkbook <- R6::R6Class(

col <- list(...)[["col"]]
row <- list(...)[["row"]]
color <- list(...)[["color"]]

if (!is.null(row) && !is.null(col)) {
.Deprecated(old = "col/row", new = "dims", package = "openxlsx2")
Expand All @@ -4145,11 +4146,15 @@ wbWorkbook <- R6::R6Class(
comment <- wb_comment(text = comment, author = getOption("openxlsx2.creator"))
}

if (!is.null(color) && !is_wbColour(color))
stop("color needs to be a wb_color()")

write_comment(
wb = self,
sheet = sheet,
comment = comment,
dims = dims
dims = dims,
color = color
) # has no use: xy

invisible(self)
Expand Down
5 changes: 4 additions & 1 deletion man/comment_internal.Rd

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

2 changes: 1 addition & 1 deletion man/wb_add_comment.Rd

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

3 changes: 3 additions & 0 deletions man/wb_add_pivot_table.Rd

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

0 comments on commit f9603ca

Please sign in to comment.