Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[styles] allow updates of font elements #1153

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

Loading