Skip to content

Commit

Permalink
create_tablestyle (#609)
Browse files Browse the repository at this point in the history
* create_tablestyle

* add test
  • Loading branch information
JanMarvin committed May 17, 2023
1 parent 1a6e57c commit df95de3
Show file tree
Hide file tree
Showing 4 changed files with 221 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export(create_font)
export(create_hyperlink)
export(create_numfmt)
export(create_sparklines)
export(create_tablestyle)
export(current_sheet)
export(dataframe_to_dims)
export(delete_data)
Expand Down
144 changes: 144 additions & 0 deletions R/wb_styles.R
Original file line number Diff line number Diff line change
Expand Up @@ -768,3 +768,147 @@ create_dxfs_style <- function(
)

}

#' create tableStyle
#' @param name name
#' @param wholeTable wholeTable
#' @param headerRow headerRow
#' @param totalRow totalRow
#' @param firstColumn firstColumn
#' @param lastColumn lastColumn
#' @param firstRowStripe firstRowStripe
#' @param secondRowStripe secondRowStripe
#' @param firstColumnStripe firstColumnStripe
#' @param secondColumnStripe secondColumnStripe
#' @param firstHeaderCell firstHeaderCell
#' @param lastHeaderCell lastHeaderCell
#' @param firstTotalCell firstTotalCell
#' @param lastTotalCell lastTotalCell
#' @export
create_tablestyle <- function(
name,
wholeTable = NULL,
headerRow = NULL,
totalRow = NULL,
firstColumn = NULL,
lastColumn = NULL,
firstRowStripe = NULL,
secondRowStripe = NULL,
firstColumnStripe = NULL,
secondColumnStripe = NULL,
firstHeaderCell = NULL,
lastHeaderCell = NULL,
firstTotalCell = NULL,
lastTotalCell = NULL
) {

tab_wholeTable <- NULL
if (length(wholeTable)) {
tab_wholeTable <- xml_node_create(
"tableStyleElement",
xml_attributes = c(type = "wholeTable", dxfId = wholeTable))
}
tab_headerRow <- NULL
if (length(headerRow)) {
tab_headerRow <- xml_node_create(
"tableStyleElement",
xml_attributes = c(type = "headerRow", dxfId = headerRow))
}
tab_totalRow <- NULL
if (length(totalRow)) {
tab_totalRow <- xml_node_create(
"tableStyleElement",
xml_attributes = c(type = "totalRow", dxfId = totalRow))
}
tab_firstColumn <- NULL
if (length(firstColumn)) {
tab_firstColumn <- xml_node_create(
"tableStyleElement",
xml_attributes = c(type = "firstColumn", dxfId = firstColumn))
}
tab_lastColumn <- NULL
if (length(lastColumn)) {
tab_lastColumn <- xml_node_create(
"tableStyleElement",
xml_attributes = c(type = "lastColumn", dxfId = lastColumn))
}
tab_firstRowStripe <- NULL
if (length(firstRowStripe)) {
tab_firstRowStripe <- xml_node_create(
"tableStyleElement",
xml_attributes = c(type = "firstRowStripe", dxfId = firstRowStripe))
}
tab_secondRowStripe <- NULL
if (length(secondRowStripe)) {
tab_secondRowStripe <- xml_node_create(
"tableStyleElement",
xml_attributes = c(type = "secondRowStripe", dxfId = secondRowStripe))
}
tab_firstColumnStripe <- NULL
if (length(firstColumnStripe)) {
tab_firstColumnStripe <- xml_node_create(
"tableStyleElement",
xml_attributes = c(type = "firstColumnStripe", dxfId = firstColumnStripe))
}
tab_secondColumnStripe <- NULL
if (length(secondColumnStripe)) {
tab_secondColumnStripe <- xml_node_create(
"tableStyleElement",
xml_attributes = c(type = "secondColumnStripe", dxfId = secondColumnStripe))
}
tab_firstHeaderCell <- NULL
if (length(firstHeaderCell)) {
tab_firstHeaderCell <- xml_node_create(
"tableStyleElement",
xml_attributes = c(type = "firstHeaderCell", dxfId = firstHeaderCell))
}
tab_lastHeaderCell <- NULL
if (length(lastHeaderCell)) {
tab_lastHeaderCell <- xml_node_create(
"tableStyleElement",
xml_attributes = c(type = "lastHeaderCell", dxfId = lastHeaderCell))
}
tab_firstTotalCell <- NULL
if (length(firstTotalCell)) {
tab_firstTotalCell <- xml_node_create(
"tableStyleElement",
xml_attributes = c(type = "firstTotalCell", dxfId = firstTotalCell))
}
tab_lastTotalCell <- NULL
if (length(lastTotalCell)) {
tab_lastTotalCell <- xml_node_create(
"tableStyleElement",
xml_attributes = c(type = "lastTotalCell", dxfId = lastTotalCell))
}

xml_elements <- c(
tab_wholeTable,
tab_headerRow,
tab_totalRow,
tab_firstColumn,
tab_lastColumn,
tab_firstRowStripe,
tab_secondRowStripe,
tab_firstColumnStripe,
tab_secondColumnStripe,
tab_firstHeaderCell,
tab_lastHeaderCell,
tab_firstTotalCell,
tab_lastTotalCell
)

rand_str <- random_string(length = 12, pattern = "[A-Z0-9]")

xml_node_create(
"tableStyle",
xml_attributes = c(
name = name,
pivot = "0", # pivot uses different styles
count = as_xml_attr(length(xml_elements)),
`xr9:uid` = sprintf("{CE23B8CA-E823-724F-9713-%s}", rand_str
)
),
xml_children = xml_elements
)

}
55 changes: 55 additions & 0 deletions man/create_tablestyle.Rd

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

21 changes: 21 additions & 0 deletions tests/testthat/test-wb_styles.R
Original file line number Diff line number Diff line change
Expand Up @@ -530,3 +530,24 @@ test_that("logical and numeric work too", {
)

})

test_that("create_tablestyle() works", {

exp <- "<tableStyle name=\"red_table\" pivot=\"0\" count=\"9\" xr9:uid=\"{CE23B8CA-E823-724F-9713-ASEVX1JWJGYG}\"><tableStyleElement type=\"wholeTable\" dxfId=\"8\"/><tableStyleElement type=\"headerRow\" dxfId=\"7\"/><tableStyleElement type=\"totalRow\" dxfId=\"6\"/><tableStyleElement type=\"firstColumn\" dxfId=\"5\"/><tableStyleElement type=\"lastColumn\" dxfId=\"4\"/><tableStyleElement type=\"firstRowStripe\" dxfId=\"3\"/><tableStyleElement type=\"secondRowStripe\" dxfId=\"2\"/><tableStyleElement type=\"firstColumnStripe\" dxfId=\"1\"/><tableStyleElement type=\"secondColumnStripe\" dxfId=\"0\"/></tableStyle>"
set.seed(123)
options("openxlsx2_seed" = NULL)
got <- create_tablestyle(
name = "red_table",
wholeTable = 8,
headerRow = 7,
totalRow = 6,
firstColumn = 5,
lastColumn = 4,
firstRowStripe = 3,
secondRowStripe = 2,
firstColumnStripe = 1,
secondColumnStripe = 0
)
expect_equal(exp, got)

})

0 comments on commit df95de3

Please sign in to comment.