Skip to content

Commit

Permalink
restore and improve tab_color. it is now possible to pass wb_color(th…
Browse files Browse the repository at this point in the history
…eme). (#750)
  • Loading branch information
JanMarvin committed Aug 22, 2023
1 parent bed27b7 commit 64eb8c3
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 21 deletions.
10 changes: 6 additions & 4 deletions R/class-chart-sheet.R
Original file line number Diff line number Diff line change
Expand Up @@ -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('<sheetPr><tabColor rgb="%s"/></sheetPr>', tabColor)
initialize = function(tab_color = NULL) {

if (!is.null(tab_color)) {
tab_color <- xml_node_create("tabColor", xml_attributes = tab_color)
tabColor <- sprintf('<sheetPr>%s</sheetPr>', tab_color)
} else {
tabColor <- character()
}
Expand Down
26 changes: 12 additions & 14 deletions R/class-workbook.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -450,7 +449,7 @@ wbWorkbook <- R6::R6Class(

self$append("worksheets",
wbChartSheet$new(
tabColor = tab_color
tab_color = tabColor
)
)

Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion R/class-worksheet.R
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ wbWorksheet <- R6::R6Class(
standardize_case_names(...)

if (!is.null(tab_color)) {
tabColor <- sprintf('<sheetPr><tabColor rgb="%s"/></sheetPr>', tab_color)
tab_color <- xml_node_create("tabColor", xml_attributes = tab_color)
tabColor <- sprintf('<sheetPr>%s</sheetPr>', tab_color)
} else {
tabColor <- character()
}
Expand Down
4 changes: 2 additions & 2 deletions man/wbChartSheet.Rd

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

46 changes: 46 additions & 0 deletions tests/testthat/test-class-worksheet.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

})

0 comments on commit 64eb8c3

Please sign in to comment.