Skip to content

Commit

Permalink
[styles] allow updates of font elements
Browse files Browse the repository at this point in the history
  • Loading branch information
JanMarvin committed Nov 1, 2024
1 parent 1a7e421 commit ce1ba76
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
3 changes: 3 additions & 0 deletions R/class-workbook-wrappers.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -3369,6 +3370,7 @@ wb_add_font <- function(
scheme = "",
shadow = "",
vert_align = "",
update = FALSE,
...
) {
assert_workbook(wb)
Expand All @@ -3391,6 +3393,7 @@ wb_add_font <- function(
scheme = scheme,
shadow = shadow,
vert_align = vert_align,
update = update,
... = ...
)
}
Expand Down
22 changes: 21 additions & 1 deletion R/class-workbook.R
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -8500,6 +8501,7 @@ wbWorkbook <- R6::R6Class(
scheme = "",
shadow = "",
vert_align = "",
update = FALSE,
...
) {
sheet <- private$get_sheet_index(sheet)
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions man/wbWorkbook.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_font.Rd

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

0 comments on commit ce1ba76

Please sign in to comment.