From ce1ba76a801d8639e6155098373547023bb5ab10 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 c04f0fbdd..fc617eac9 100644 --- a/R/class-workbook-wrappers.R +++ b/R/class-workbook-wrappers.R @@ -3339,6 +3339,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) @@ -3369,6 +3370,7 @@ wb_add_font <- function( scheme = "", shadow = "", vert_align = "", + update = FALSE, ... ) { assert_workbook(wb) @@ -3391,6 +3393,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 44909d97f..c3f8d0c16 100644 --- a/R/class-workbook.R +++ b/R/class-workbook.R @@ -8480,6 +8480,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(), @@ -8500,6 +8501,7 @@ wbWorkbook <- R6::R6Class( scheme = "", shadow = "", vert_align = "", + update = FALSE, ... ) { sheet <- private$get_sheet_index(sheet) @@ -8534,9 +8536,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 f0cdcd10a..0694f4b43 100644 --- a/man/wbWorkbook.Rd +++ b/man/wbWorkbook.Rd @@ -3311,6 +3311,7 @@ provide simple font function scheme = "", shadow = "", vert_align = "", + update = FALSE, ... )}\if{html}{\out{}} } @@ -3352,6 +3353,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{