From 42829e4a1046e31c39892c40a5ee666ac3446f0d Mon Sep 17 00:00:00 2001 From: Jan Marvin Garbuszus Date: Tue, 22 Aug 2023 06:34:41 +0200 Subject: [PATCH] restore and improve tab_color. it is now possible to pass wb_color(theme). --- R/class-chart-sheet.R | 10 +++--- R/class-workbook.R | 14 ++++---- R/class-worksheet.R | 3 +- man/wbChartSheet.Rd | 4 +-- tests/testthat/test-class-worksheet.R | 47 +++++++++++++++++++++++++++ 5 files changed, 65 insertions(+), 13 deletions(-) diff --git a/R/class-chart-sheet.R b/R/class-chart-sheet.R index a5dad9350..f3340e2e0 100644 --- a/R/class-chart-sheet.R +++ b/R/class-chart-sheet.R @@ -51,11 +51,13 @@ wbChartSheet <- R6::R6Class( #' @description #' Create a new workbook chart sheet object - #' @param tabColor `character` a tab color to set + #' @param tab_color tabColor #' @return The `wbChartSheet` object - initialize = function(tabColor = tabColor) { - if (length(tabColor)) { - tabColor <- sprintf('', tabColor) + initialize = function(tab_color = NULL) { + + if (!is.null(tab_color)) { + tab_color <- xml_node_create("tabColor", xml_attributes = tab_color) + tabColor <- sprintf('%s', tab_color) } else { tabColor <- character() } diff --git a/R/class-workbook.R b/R/class-workbook.R index 0bde6aa40..aeafd0b97 100644 --- a/R/class-workbook.R +++ b/R/class-workbook.R @@ -429,9 +429,10 @@ wbWorkbook <- R6::R6Class( if (!is.null(tab_color)) { if (is_wbColour(tab_color)) { - tab_color <- as.character(tab_color) + tabColor <- tab_color } else { - tab_color <- validate_color(tab_color, msg = "Invalid tab_color in add_chartsheet.") + validate_color(tab_color, msg = "Invalid tab_color in add_chartsheet.") + tabColor <- wb_color(tab_color) } } @@ -450,7 +451,7 @@ wbWorkbook <- R6::R6Class( self$append("worksheets", wbChartSheet$new( - tabColor = tab_color + tab_color = tabColor ) ) @@ -582,9 +583,10 @@ wbWorkbook <- R6::R6Class( if (!is.null(tab_color)) { if (is_wbColour(tab_color)) { - tabColor <- as.character(tab_color) + tabColor <- tab_color } else { - tabColor <- validate_color(tab_color, msg = "Invalid tab_color in add_worksheet.") + validate_color(tab_color, msg = "Invalid tab_color in add_worksheet.") + tabColor <- wb_color(tab_color) } } @@ -684,7 +686,7 @@ wbWorkbook <- R6::R6Class( ## append to worksheets list self$append("worksheets", wbWorksheet$new( - tab_color = tab_color, + tab_color = tabColor, odd_header = odd_header, odd_footer = odd_footer, even_header = even_header, diff --git a/R/class-worksheet.R b/R/class-worksheet.R index ce9915546..7ab3a2d3a 100644 --- a/R/class-worksheet.R +++ b/R/class-worksheet.R @@ -173,7 +173,8 @@ wbWorksheet <- R6::R6Class( standardize_case_names(...) if (!is.null(tab_color)) { - tabColor <- sprintf('', tab_color) + tab_color <- xml_node_create("tabColor", xml_attributes = tab_color) + tabColor <- sprintf('%s', tab_color) } else { tabColor <- character() } diff --git a/man/wbChartSheet.Rd b/man/wbChartSheet.Rd index 5784c43c5..1a83a825a 100644 --- a/man/wbChartSheet.Rd +++ b/man/wbChartSheet.Rd @@ -55,13 +55,13 @@ hyperlinks = NULL,} \subsection{Method \code{new()}}{ Create a new workbook chart sheet object \subsection{Usage}{ -\if{html}{\out{
}}\preformatted{wbChartSheet$new(tabColor = tabColor)}\if{html}{\out{
}} +\if{html}{\out{
}}\preformatted{wbChartSheet$new(tab_color = NULL)}\if{html}{\out{
}} } \subsection{Arguments}{ \if{html}{\out{
}} \describe{ -\item{\code{tabColor}}{\code{character} a tab color to set} +\item{\code{tab_color}}{tabColor} } \if{html}{\out{
}} } diff --git a/tests/testthat/test-class-worksheet.R b/tests/testthat/test-class-worksheet.R index 4f661f0c4..d59343a95 100644 --- a/tests/testthat/test-class-worksheet.R +++ b/tests/testthat/test-class-worksheet.R @@ -130,3 +130,50 @@ test_that("ignore_error works", { expect_equal(exp, got) }) + +test_that("tab_color works", { + + # worksheet + wb <- wb_workbook()$ + add_worksheet(tab_color = "red")$ + add_worksheet(tab_color = wb_color("red")) + + expect_equal( + wb$worksheets[[1]]$sheetPr, + wb$worksheets[[2]]$sheetPr + ) + + # chartsheet + wb <- wb_workbook()$ + add_chartsheet(tab_color = "red")$ + add_chartsheet(tab_color = wb_color("red")) + + expect_equal( + wb$worksheets[[1]]$sheetPr, + wb$worksheets[[2]]$sheetPr + ) + + # use color theme + wb <- wb_workbook()$ + add_worksheet(tab_color = wb_color(theme = 4))$ + add_chartsheet(tab_color = wb_color(theme = 4)) + + expect_equal( + wb$worksheets[[1]]$sheetPr, + wb$worksheets[[2]]$sheetPr + ) + + # error with invalid tab_color. blau is German for blue. + expect_error( + wb <- wb_workbook()$ + add_worksheet(tab_color = "blau"), + "Invalid tab_color in add_worksheet" + ) + expect_error( + wb <- wb_workbook()$ + add_chartsheet(tab_color = "blau"), + "Invalid tab_color in add_worksheet" + ) + + +})