diff --git a/NAMESPACE b/NAMESPACE index b50f8c49b..514490624 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -60,6 +60,7 @@ export(wb_add_creators) export(wb_add_data) export(wb_add_data_table) export(wb_add_data_validation) +export(wb_add_dxfs_style) export(wb_add_fill) export(wb_add_filter) export(wb_add_font) diff --git a/R/class-workbook-wrappers.R b/R/class-workbook-wrappers.R index e650c4bde..479bc9c46 100644 --- a/R/class-workbook-wrappers.R +++ b/R/class-workbook-wrappers.R @@ -2708,6 +2708,63 @@ wb_add_named_style <- function( ) } +#' add dxfs style +#' These styles are used with conditional formatting and custom table styles +#' @param wb wbWorkbook +#' @param name the style name +#' @param font_name the font name +#' @param font_size the font size +#' @param font_color the font color (a `wb_color()` object) +#' @param numFmt the number format +#' @param border logical if borders are applied +#' @param border_color the border color +#' @param border_style the border style +#' @param bgFill any background fill +#' @param gradientFill any gradient fill +#' @param text_bold logical if text is bold +#' @param text_italic logical if text is italic +#' @param text_underline logical if text is underlined +#' @param ... additional arguments passed to `create_dxfs_style()` +#' @return The `wbWorkbookObject`, invisibly +#' @export +wb_add_dxfs_style <- function( + wb, + name, + font_name = NULL, + font_size = NULL, + font_color = NULL, + numFmt = NULL, + border = NULL, + border_color = wb_color(getOption("openxlsx2.borderColor", "black")), + border_style = getOption("openxlsx2.borderStyle", "thin"), + bgFill = NULL, + gradientFill = NULL, + text_bold = NULL, + text_italic = NULL, + text_underline = NULL, + ... +) { + + assert_workbook(wb) + wb$clone()$add_dxfs_style( + name = name, + font_name = font_name, + font_size = font_size, + font_color = font_color, + numFmt = numFmt, + border = border, + border_color = border_color, + border_style = border_style, + bgFill = bgFill, + gradientFill = gradientFill, + text_bold = text_bold, + text_italic = text_italic, + text_underline = text_underline, + ... = ... + ) + +} + #' Add comment to worksheet #' @param wb A workbook object #' @param sheet A worksheet of the workbook diff --git a/R/class-workbook.R b/R/class-workbook.R index b87c8b80b..6cdc3e107 100644 --- a/R/class-workbook.R +++ b/R/class-workbook.R @@ -6353,6 +6353,67 @@ wbWorkbook <- R6::R6Class( invisible(self) }, + #' @description create dxfs style + #' These styles are used with conditional formatting and custom table styles + #' @param name the style name + #' @param font_name the font name + #' @param font_size the font size + #' @param font_color the font color (a `wb_color()` object) + #' @param numFmt the number format + #' @param border logical if borders are applied + #' @param border_color the border color + #' @param border_style the border style + #' @param bgFill any background fill + #' @param gradientFill any gradient fill + #' @param text_bold logical if text is bold + #' @param text_italic logical if text is italic + #' @param text_underline logical if text is underlined + #' @param ... additional arguments passed to `create_dxfs_style()` + #' @return The `wbWorksheetObject`, invisibly + #' @export + add_dxfs_style = function( + name, + font_name = NULL, + font_size = NULL, + font_color = NULL, + numFmt = NULL, + border = NULL, + border_color = wb_color(getOption("openxlsx2.borderColor", "black")), + border_style = getOption("openxlsx2.borderStyle", "thin"), + bgFill = NULL, + gradientFill = NULL, + text_bold = NULL, + text_italic = NULL, + text_underline = NULL, + ... + ) { + + xml_style <- create_dxfs_style( + font_name = font_name, + font_size = font_size, + font_color = font_color, + numFmt = numFmt, + border = border, + border_color = border_color, + border_style = border_style, + bgFill = bgFill, + gradientFill = gradientFill, + text_bold = text_bold, + text_italic = text_italic, + text_underline = text_underline, + ... = ... + ) + + got <- self$styles_mgr$get_dxf_id(name) + + if (!is.null(got) && !is.na(got)) + warning("dxfs style names should be unique") + + self$add_style(xml_style, name) + + invisible(self) + }, + #' @description clone style from one sheet to another #' @param from the worksheet you are cloning #' @param to the worksheet the style is applied to diff --git a/man/wbWorkbook.Rd b/man/wbWorkbook.Rd index 9f08f43fe..00acf5665 100644 --- a/man/wbWorkbook.Rd +++ b/man/wbWorkbook.Rd @@ -327,6 +327,7 @@ is created, not when the Excel files are saved.} \item \href{#method-wbWorkbook-get_cell_style}{\code{wbWorkbook$get_cell_style()}} \item \href{#method-wbWorkbook-set_cell_style}{\code{wbWorkbook$set_cell_style()}} \item \href{#method-wbWorkbook-add_named_style}{\code{wbWorkbook$add_named_style()}} +\item \href{#method-wbWorkbook-add_dxfs_style}{\code{wbWorkbook$add_dxfs_style()}} \item \href{#method-wbWorkbook-clone_sheet_style}{\code{wbWorkbook$clone_sheet_style()}} \item \href{#method-wbWorkbook-add_sparklines}{\code{wbWorkbook$add_sparklines()}} \item \href{#method-wbWorkbook-add_ignore_error}{\code{wbWorkbook$add_ignore_error()}} @@ -2945,6 +2946,68 @@ The \code{wbWorkbook}, invisibly } } \if{html}{\out{
}} +\if{html}{\out{}} +\if{latex}{\out{\hypertarget{method-wbWorkbook-add_dxfs_style}{}}} +\subsection{Method \code{add_dxfs_style()}}{ +create dxfs style +These styles are used with conditional formatting and custom table styles +\subsection{Usage}{ +\if{html}{\out{
}}\preformatted{wbWorkbook$add_dxfs_style( + name, + font_name = NULL, + font_size = NULL, + font_color = NULL, + numFmt = NULL, + border = NULL, + border_color = wb_color(getOption("openxlsx2.borderColor", "black")), + border_style = getOption("openxlsx2.borderStyle", "thin"), + bgFill = NULL, + gradientFill = NULL, + text_bold = NULL, + text_italic = NULL, + text_underline = NULL, + ... +)}\if{html}{\out{
}} +} + +\subsection{Arguments}{ +\if{html}{\out{
}} +\describe{ +\item{\code{name}}{the style name} + +\item{\code{font_name}}{the font name} + +\item{\code{font_size}}{the font size} + +\item{\code{font_color}}{the font color (a \code{wb_color()} object)} + +\item{\code{numFmt}}{the number format} + +\item{\code{border}}{logical if borders are applied} + +\item{\code{border_color}}{the border color} + +\item{\code{border_style}}{the border style} + +\item{\code{bgFill}}{any background fill} + +\item{\code{gradientFill}}{any gradient fill} + +\item{\code{text_bold}}{logical if text is bold} + +\item{\code{text_italic}}{logical if text is italic} + +\item{\code{text_underline}}{logical if text is underlined} + +\item{\code{...}}{additional arguments passed to \code{create_dxfs_style()}} +} +\if{html}{\out{
}} +} +\subsection{Returns}{ +The \code{wbWorksheetObject}, invisibly +} +} +\if{html}{\out{
}} \if{html}{\out{}} \if{latex}{\out{\hypertarget{method-wbWorkbook-clone_sheet_style}{}}} \subsection{Method \code{clone_sheet_style()}}{ diff --git a/man/wb_add_dxfs_style.Rd b/man/wb_add_dxfs_style.Rd new file mode 100644 index 000000000..01e211373 --- /dev/null +++ b/man/wb_add_dxfs_style.Rd @@ -0,0 +1,63 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/class-workbook-wrappers.R +\name{wb_add_dxfs_style} +\alias{wb_add_dxfs_style} +\title{add dxfs style +These styles are used with conditional formatting and custom table styles} +\usage{ +wb_add_dxfs_style( + wb, + name, + font_name = NULL, + font_size = NULL, + font_color = NULL, + numFmt = NULL, + border = NULL, + border_color = wb_color(getOption("openxlsx2.borderColor", "black")), + border_style = getOption("openxlsx2.borderStyle", "thin"), + bgFill = NULL, + gradientFill = NULL, + text_bold = NULL, + text_italic = NULL, + text_underline = NULL, + ... +) +} +\arguments{ +\item{wb}{wbWorkbook} + +\item{name}{the style name} + +\item{font_name}{the font name} + +\item{font_size}{the font size} + +\item{font_color}{the font color (a \code{wb_color()} object)} + +\item{numFmt}{the number format} + +\item{border}{logical if borders are applied} + +\item{border_color}{the border color} + +\item{border_style}{the border style} + +\item{bgFill}{any background fill} + +\item{gradientFill}{any gradient fill} + +\item{text_bold}{logical if text is bold} + +\item{text_italic}{logical if text is italic} + +\item{text_underline}{logical if text is underlined} + +\item{...}{additional arguments passed to \code{create_dxfs_style()}} +} +\value{ +The \code{wbWorkbookObject}, invisibly +} +\description{ +add dxfs style +These styles are used with conditional formatting and custom table styles +} diff --git a/tests/testthat/test-wb_styles.R b/tests/testthat/test-wb_styles.R index e9e2f9638..874ea74be 100644 --- a/tests/testthat/test-wb_styles.R +++ b/tests/testthat/test-wb_styles.R @@ -743,3 +743,60 @@ test_that("wb_add_named_style() works", { expect_equal(exp, got) }) + +test_that("wb_add_dxfs_style() works", { + wb <- wb_workbook() %>% + wb_add_worksheet() %>% + wb_add_dxfs_style( + name = "nay", + font_color = wb_color(hex = "FF9C0006"), + bgFill = wb_color(hex = "FFFFC7CE") + ) %>% + wb_add_dxfs_style( + name = "yay", + font_color = wb_color(hex = "FF006100"), + bgFill = wb_color(hex = "FFC6EFCE") + ) %>% + wb_add_data(x = -5:5) %>% + wb_add_data(x = LETTERS[1:11], startCol = 2) %>% + wb_add_conditional_formatting( + cols = 1, + rows = 1:11, + rule = "!=0", + style = "nay" + ) %>% + wb_add_conditional_formatting( + cols = 1, + rows = 1:11, + rule = "==0", + style = "yay" + ) + + exp <- c( + `A1:A11` = "A1<>0", + `A1:A11` = "A1=0" + ) + got <- wb$worksheets[[1]]$conditionalFormatting + expect_equal(exp, got) + + exp <- c("nay", "yay") + got <- wb$styles_mgr$dxf$name + expect_equal(exp, got) + + expect_warning( + wb_workbook() %>% + wb_add_worksheet() %>% + wb_add_dxfs_style( + name = "nay", + font_color = wb_color(hex = "FF9C0006"), + bgFill = wb_color(hex = "FFFFC7CE") + ) %>% + wb_add_dxfs_style( + name = "nay", + font_color = wb_color(hex = "FF006100"), + bgFill = wb_color(hex = "FFC6EFCE") + ), + "dxfs style names should be unique" + ) + +}) diff --git a/vignettes/conditional-formatting.Rmd b/vignettes/conditional-formatting.Rmd index 2e195c60d..449262002 100644 --- a/vignettes/conditional-formatting.Rmd +++ b/vignettes/conditional-formatting.Rmd @@ -21,10 +21,8 @@ library(openxlsx2) ```{r} wb <- wb_workbook() -negStyle <- create_dxfs_style(font_color = wb_color(hex = "FF9C0006"), bgFill = wb_color(hex = "FFFFC7CE")) -posStyle <- create_dxfs_style(font_color = wb_color(hex = "FF006100"), bgFill = wb_color(hex = "FFC6EFCE")) -wb$styles_mgr$add(negStyle, "negStyle") -wb$styles_mgr$add(posStyle, "posStyle") +wb$add_dxfs_style(name = "negStyle", font_color = wb_color(hex = "FF9C0006"), bgFill = wb_color(hex = "FFFFC7CE")) +wb$add_dxfs_style(name = "posStyle", font_color = wb_color(hex = "FF006100"), bgFill = wb_color(hex = "FFC6EFCE")) ``` ## Rule applies to all each cell in range