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

add gradientFill option to `create_dxfs_style(). closes #648 #651

Merged
merged 2 commits into from
Jun 13, 2023
Merged
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
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

* Order of arguments in `wb_add_conditional_formatting()` changed, because previously overlooked `dims` argument was added. [642](https://github.com/JanMarvin/openxlsx2/pull/642)

* New argument `gradientFill` was added to `create_dxfs_style()`. [651](https://github.com/JanMarvin/openxlsx2/pull/6501


***************************************************************************

Expand Down
12 changes: 10 additions & 2 deletions R/wb_styles.R
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,7 @@ get_cell_styles <- function(wb, sheet, cell) {
#' @param border_style "thin"
#' @param bgFill Cell background fill color.
#' @param fgColor Cell foreground fill color.
#' @param gradientFill An xml string beginning with <gradientFill> ...
#' @param text_bold bold
#' @param text_strike strikeout
#' @param text_italic italic
Expand Down Expand Up @@ -706,6 +707,7 @@ create_dxfs_style <- function(
border_style = getOption("openxlsx2.borderStyle", "thin"),
bgFill = NULL,
fgColor = NULL,
gradientFill = NULL,
text_bold = NULL,
text_strike = NULL,
text_italic = NULL,
Expand Down Expand Up @@ -742,8 +744,14 @@ create_dxfs_style <- function(
patternType <- "solid"
}

if (!is.null(bgFill) && !all(bgFill == "")) {
fill <- create_fill(patternType = patternType, bgColor = bgFill, fgColor = fgColor)
if (!is.null(bgFill) && !all(bgFill == "") || !is.null(gradientFill)) {
if (is.null(gradientFill)) {
# gradientFill is an xml string
gradientFill <- ""
} else {
patternType <- ""
}
fill <- create_fill(patternType = patternType, bgColor = bgFill, fgColor = fgColor, gradientFill = gradientFill)
} else {
fill <- NULL
}
Expand Down
3 changes: 3 additions & 0 deletions man/create_dxfs_style.Rd

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

23 changes: 23 additions & 0 deletions tests/testthat/test-conditional_formatting.R
Original file line number Diff line number Diff line change
Expand Up @@ -767,3 +767,26 @@ test_that("un_list works", {
expect_equal(pre_save, post_save)

})

test_that("conditional formatting with gradientFill works", {

gf <- read_xml(
"<gradientFill degree=\"45\">
<stop position=\"0\"><color rgb=\"FFFFC000\"/></stop>
<stop position=\"1\"><color rgb=\"FF00B0F0\"/></stop>
</gradientFill>",
pointer = FALSE)

gf_style <- create_dxfs_style(gradientFill = gf)

wb <- wb_workbook()$
add_worksheet()$
add_data(x = 1)$
add_conditional_formatting(cols = 1, rows = 1, rule = "==1", style = "gf_style")$
add_style(gf_style)

exp <- "<dxf><fill><gradientFill degree=\"45\"><stop position=\"0\"><color rgb=\"FFFFC000\"/></stop><stop position=\"1\"><color rgb=\"FF00B0F0\"/></stop></gradientFill></fill></dxf>"
got <- wb$styles_mgr$styles$dxfs
expect_equal(exp, got)

})