From 62a1c509cde88f84de6422153c26029905753176 Mon Sep 17 00:00:00 2001 From: Jan Marvin Garbuszus Date: Sun, 6 Oct 2024 21:20:46 +0200 Subject: [PATCH] [styles] allow updates of font elements --- R/class-workbook-wrappers.R | 3 +++ R/class-workbook.R | 22 +++++++++++++++++++++- man/wbWorkbook.Rd | 3 +++ man/wb_add_font.Rd | 3 +++ 4 files changed, 30 insertions(+), 1 deletion(-) diff --git a/R/class-workbook-wrappers.R b/R/class-workbook-wrappers.R index 2f3ac98b9..28974ef50 100644 --- a/R/class-workbook-wrappers.R +++ b/R/class-workbook-wrappers.R @@ -3321,6 +3321,7 @@ wb_add_fill <- function( #' @param shadow shadow #' @param extend extend #' @param vert_align vertical alignment +#' @param update update #' @param ... ... #' @examples #' wb <- wb_workbook() %>% wb_add_worksheet("S1") %>% wb_add_data("S1", mtcars) @@ -3351,6 +3352,7 @@ wb_add_font <- function( scheme = "", shadow = "", vert_align = "", + update = FALSE, ... ) { assert_workbook(wb) @@ -3373,6 +3375,7 @@ wb_add_font <- function( scheme = scheme, shadow = shadow, vert_align = vert_align, + update = update, ... = ... ) } diff --git a/R/class-workbook.R b/R/class-workbook.R index 5f499a71d..cb29a3959 100644 --- a/R/class-workbook.R +++ b/R/class-workbook.R @@ -8442,6 +8442,7 @@ wbWorkbook <- R6::R6Class( #' @param shadow shadow #' @param extend extend #' @param vert_align vertical alignment + #' @param update update #' @return The `wbWorkbook`, invisibly add_font = function( sheet = current_sheet(), @@ -8462,6 +8463,7 @@ wbWorkbook <- R6::R6Class( scheme = "", shadow = "", vert_align = "", + update = FALSE, ... ) { sheet <- private$get_sheet_index(sheet) @@ -8496,9 +8498,27 @@ wbWorkbook <- R6::R6Class( u = underline, vertAlign = vert_align ) - self$styles_mgr$add(new_font, new_font) xf_prev <- get_cell_styles(self, sheet, dim[[1]]) + + if (update) { + font_id <- as.integer(sapply(xml_attr(xf_prev, "xf"), "[[", "fontId")) + 1L + font_xml <- self$styles_mgr$styles$fonts[[font_id]] + + # read as data frame with xml elements + old_font <- read_font(read_xml(font_xml)) + new_font <- read_font(read_xml(new_font)) + + # update elements + sel <- new_font != "" + old_font[sel] <- new_font[sel] + + # write as xml font + new_font <- write_font(old_font) + } + + self$styles_mgr$add(new_font, new_font) + xf_new_font <- set_font(xf_prev, self$styles_mgr$get_font_id(new_font)) self$styles_mgr$add(xf_new_font, xf_new_font) diff --git a/man/wbWorkbook.Rd b/man/wbWorkbook.Rd index 9a7215aa6..ad56acb9f 100644 --- a/man/wbWorkbook.Rd +++ b/man/wbWorkbook.Rd @@ -3308,6 +3308,7 @@ provide simple font function scheme = "", shadow = "", vert_align = "", + update = FALSE, ... )}\if{html}{\out{}} } @@ -3349,6 +3350,8 @@ provide simple font function \item{\code{vert_align}}{vertical alignment} +\item{\code{update}}{update} + \item{\code{...}}{additional arguments} } \if{html}{\out{}} diff --git a/man/wb_add_font.Rd b/man/wb_add_font.Rd index 15d92aac4..108cec562 100644 --- a/man/wb_add_font.Rd +++ b/man/wb_add_font.Rd @@ -23,6 +23,7 @@ wb_add_font( scheme = "", shadow = "", vert_align = "", + update = FALSE, ... ) } @@ -63,6 +64,8 @@ wb_add_font( \item{vert_align}{vertical alignment} +\item{update}{update} + \item{...}{...} } \value{