Skip to content

Commit 9da37cc

Browse files
committed
add gradientFill option to `create_dxfs_style()
1 parent e9518cb commit 9da37cc

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

R/wb_styles.R

+10-2
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,7 @@ get_cell_styles <- function(wb, sheet, cell) {
667667
#' @param border_style "thin"
668668
#' @param bgFill Cell background fill color.
669669
#' @param fgColor Cell foreground fill color.
670+
#' @param gradientFill An xml string beginning with <gradientFill> ...
670671
#' @param text_bold bold
671672
#' @param text_strike strikeout
672673
#' @param text_italic italic
@@ -706,6 +707,7 @@ create_dxfs_style <- function(
706707
border_style = getOption("openxlsx2.borderStyle", "thin"),
707708
bgFill = NULL,
708709
fgColor = NULL,
710+
gradientFill = NULL,
709711
text_bold = NULL,
710712
text_strike = NULL,
711713
text_italic = NULL,
@@ -742,8 +744,14 @@ create_dxfs_style <- function(
742744
patternType <- "solid"
743745
}
744746

745-
if (!is.null(bgFill) && !all(bgFill == "")) {
746-
fill <- create_fill(patternType = patternType, bgColor = bgFill, fgColor = fgColor)
747+
if (!is.null(bgFill) && !all(bgFill == "") || !is.null(gradientFill)) {
748+
if (is.null(gradientFill)) {
749+
# gradientFill is an xml string
750+
gradientFill <- ""
751+
} else {
752+
patternType <- ""
753+
}
754+
fill <- create_fill(patternType = patternType, bgColor = bgFill, fgColor = fgColor, gradientFill = gradientFill)
747755
} else {
748756
fill <- NULL
749757
}

man/create_dxfs_style.Rd

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-conditional_formatting.R

+23
Original file line numberDiff line numberDiff line change
@@ -767,3 +767,26 @@ test_that("un_list works", {
767767
expect_equal(pre_save, post_save)
768768

769769
})
770+
771+
test_that("conditional formatting with gradientFill works", {
772+
773+
gf <- read_xml(
774+
"<gradientFill degree=\"45\">
775+
<stop position=\"0\"><color rgb=\"FFFFC000\"/></stop>
776+
<stop position=\"1\"><color rgb=\"FF00B0F0\"/></stop>
777+
</gradientFill>",
778+
pointer = FALSE)
779+
780+
gf_style <- create_dxfs_style(gradientFill = gf)
781+
782+
wb <- wb_workbook()$
783+
add_worksheet()$
784+
add_data(x = 1)$
785+
add_conditional_formatting(cols = 1, rows = 1, rule = "==1", style = "gf_style")$
786+
add_style(gf_style)
787+
788+
exp <- "<dxf><fill><gradientFill degree=\"45\"><stop position=\"0\"><color rgb=\"FFFFC000\"/></stop><stop position=\"1\"><color rgb=\"FF00B0F0\"/></stop></gradientFill></fill></dxf>"
789+
got <- wb$styles_mgr$styles$dxfs
790+
expect_equal(exp, got)
791+
792+
})

0 commit comments

Comments
 (0)