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..6dcd68c63 100644 --- a/R/class-workbook.R +++ b/R/class-workbook.R @@ -427,12 +427,11 @@ wbWorkbook <- R6::R6Class( standardize(...) - if (!is.null(tab_color)) { - if (is_wbColour(tab_color)) { - tab_color <- as.character(tab_color) - } else { - tab_color <- validate_color(tab_color, msg = "Invalid tab_color in add_chartsheet.") - } + if (!is.null(tab_color) && !is_wbColour(tab_color)) { + validate_color(tab_color, msg = "Invalid tab_color in add_chartsheet.") + tabColor <- wb_color(tab_color) + } else { + tabColor <- tab_color } if (!is.numeric(zoom)) { @@ -450,7 +449,7 @@ wbWorkbook <- R6::R6Class( self$append("worksheets", wbChartSheet$new( - tabColor = tab_color + tab_color = tabColor ) ) @@ -580,12 +579,11 @@ wbWorkbook <- R6::R6Class( msg <- c(msg, "grid_lines must be a logical of length 1.") } - if (!is.null(tab_color)) { - if (is_wbColour(tab_color)) { - tabColor <- as.character(tab_color) - } else { - tabColor <- validate_color(tab_color, msg = "Invalid tab_color in add_worksheet.") - } + if (!is.null(tab_color) && !is_wbColour(tab_color)) { + validate_color(tab_color, msg = "Invalid tab_color in add_worksheet.") + tabColor <- wb_color(tab_color) + } else { + tabColor <- tab_color } if (!is.numeric(zoom)) { @@ -684,7 +682,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..aab529689 100644 --- a/tests/testthat/test-class-worksheet.R +++ b/tests/testthat/test-class-worksheet.R @@ -130,3 +130,49 @@ 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_chartsheet" + ) + +})