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 Oct 6, 2024
1 parent d359f18 commit 62a1c50
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 @@ -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)
Expand Down Expand Up @@ -3351,6 +3352,7 @@ wb_add_font <- function(
scheme = "",
shadow = "",
vert_align = "",
update = FALSE,
...
) {
assert_workbook(wb)
Expand All @@ -3373,6 +3375,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 @@ -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(),
Expand All @@ -8462,6 +8463,7 @@ wbWorkbook <- R6::R6Class(
scheme = "",
shadow = "",
vert_align = "",
update = FALSE,
...
) {
sheet <- private$get_sheet_index(sheet)
Expand Down Expand Up @@ -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)
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 62a1c50

Please sign in to comment.