diff --git a/R/class-workbook-wrappers.R b/R/class-workbook-wrappers.R
index fe3272470..806dbecb1 100644
--- a/R/class-workbook-wrappers.R
+++ b/R/class-workbook-wrappers.R
@@ -3414,6 +3414,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)
@@ -3444,6 +3445,7 @@ wb_add_font <- function(
scheme = "",
shadow = "",
vert_align = "",
+ update = NULL,
...
) {
assert_workbook(wb)
@@ -3466,6 +3468,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 40f61409e..7e2f0a4ce 100644
--- a/R/class-workbook.R
+++ b/R/class-workbook.R
@@ -8642,6 +8642,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(),
@@ -8662,6 +8663,7 @@ wbWorkbook <- R6::R6Class(
scheme = "",
shadow = "",
vert_align = "",
+ update = NULL,
...
) {
sheet <- private$get_sheet_index(sheet)
@@ -8696,9 +8698,52 @@ 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 (length(update)) {
+ valid <- c(
+ "name", "color", "colour", "size", "bold", "italic", "outline", "strike",
+ "underline", "charset", "condense", "extend", "family", "scheme", "shadow",
+ "vert_align"
+ )
+ match.arg(update, valid, several.ok = TRUE)
+
+ font_properties <- c(
+ bold = "b",
+ charset = "charset",
+ color = "color",
+ condense = "condense",
+ extend = "extend",
+ family = "family",
+ italic = "i",
+ name = "name",
+ outline = "outline",
+ scheme = "scheme",
+ shadow = "shadow",
+ strike = "strike",
+ size = "sz",
+ underline = "u",
+ vert_align = "vertAlign"
+ )
+ sel <- font_properties[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
+ 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 42fe1d887..16c9a0fb2 100644
--- a/man/wbWorkbook.Rd
+++ b/man/wbWorkbook.Rd
@@ -3360,6 +3360,7 @@ provide simple font function
scheme = "",
shadow = "",
vert_align = "",
+ update = NULL,
...
)}\if{html}{\out{}}
}
@@ -3401,6 +3402,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..4b64c439a 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 = NULL,
...
)
}
@@ -63,6 +64,8 @@ wb_add_font(
\item{vert_align}{vertical alignment}
+\item{update}{update}
+
\item{...}{...}
}
\value{
diff --git a/tests/testthat/test-wb_styles.R b/tests/testthat/test-wb_styles.R
index 5f4437d28..e18333d81 100644
--- a/tests/testthat/test-wb_styles.R
+++ b/tests/testthat/test-wb_styles.R
@@ -916,3 +916,21 @@ test_that("dims work", {
expect_setequal(wb$worksheets[[1]]$sheet_data$cc$c_s, "1")
})
+
+test_that("update font works", {
+ wb <- wb_workbook() |>
+ wb_add_worksheet() |>
+ wb_add_data(x = letters) |>
+ wb_add_font(dims = wb_dims(x = letters), name = "Calibri", size = 20, update = c("name", "size", "scheme"))
+
+ exp <- ""
+ got <- wb$styles_mgr$styles$fonts[2]
+ expect_equal(exp, got)
+
+ # updates only the font color
+ wb$add_font(dims = wb_dims(x = letters), color = wb_color("orange"), update = c("color"))
+
+ exp <- ""
+ got <- wb$styles_mgr$styles$fonts[3]
+ expect_equal(exp, got)
+})