diff --git a/R/class-workbook-wrappers.R b/R/class-workbook-wrappers.R index f5e69b862..0b341a63b 100644 --- a/R/class-workbook-wrappers.R +++ b/R/class-workbook-wrappers.R @@ -3378,6 +3378,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) @@ -3408,6 +3409,7 @@ wb_add_font <- function( scheme = "", shadow = "", vert_align = "", + update = FALSE, ... ) { assert_workbook(wb) @@ -3430,6 +3432,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 287837318..56e5b22f9 100644 --- a/R/class-workbook.R +++ b/R/class-workbook.R @@ -8557,6 +8557,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(), @@ -8577,6 +8578,7 @@ wbWorkbook <- R6::R6Class( scheme = "", shadow = "", vert_align = "", + update = FALSE, ... ) { sheet <- private$get_sheet_index(sheet) @@ -8611,9 +8613,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 921e29705..e4e4d18a6 100644 --- a/man/wbWorkbook.Rd +++ b/man/wbWorkbook.Rd @@ -3353,6 +3353,7 @@ provide simple font function scheme = "", shadow = "", vert_align = "", + update = FALSE, ... )}\if{html}{\out{}} } @@ -3394,6 +3395,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{