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

restore and improve tab_color #750

Merged
merged 1 commit into from
Aug 22, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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"
)

})