Skip to content

Commit

Permalink
move default style initialization to workbook
Browse files Browse the repository at this point in the history
  • Loading branch information
JanMarvin committed Sep 29, 2023
1 parent 8a8ba34 commit cee1122
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
21 changes: 12 additions & 9 deletions R/class-workbook.R
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,18 @@ wbWorkbook <- R6::R6Class(
self$vml_rels <- NULL

private$current_sheet <- 0L

empty_cellXfs <- data.frame(
numFmtId = "0",
fontId = "0",
fillId = "0",
borderId = "0",
xfId = "0",
stringsAsFactors = FALSE
)
self$styles_mgr$styles$cellXfs <- write_xf(empty_cellXfs)
self$styles_mgr$initialize(self)

invisible(self)
},

Expand Down Expand Up @@ -650,15 +662,6 @@ wbWorkbook <- R6::R6Class(
visible
)

# Order matters: if a sheet is added to a blank workbook, we add a default style. If we already have
# sheets in the workbook, we do not add a new style. This could confuse Excel which will complain.
# This fixes output of the example in wb_load.
if (length(self$sheet_names) == 0) {
# TODO this should live wherever the other default values for an empty worksheet are initialized
empty_cellXfs <- data.frame(numFmtId = "0", fontId = "0", fillId = "0", borderId = "0", xfId = "0", stringsAsFactors = FALSE)
self$styles_mgr$styles$cellXfs <- write_xf(empty_cellXfs)
}

self$append_sheets(
sprintf(
'<sheet name="%s" sheetId="%s" state="%s" r:id="rId%s"/>',
Expand Down
25 changes: 25 additions & 0 deletions tests/testthat/test-styles_xml.R
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,28 @@ test_that("colors", {
)

})

test_that("initialized styles remain available", {
foo_fill <- create_fill(patternType = "solid",
fgColor = wb_color("blue"))
foo_font <- create_font(sz = 36, b = TRUE, color = wb_color("yellow"))

wb <- wb_workbook()
wb$styles_mgr$add(foo_fill, "foo")
wb$styles_mgr$add(foo_font, "foo")

foo_style <- create_cell_style(
fill_id = wb$styles_mgr$get_fill_id("foo"),
font_id = wb$styles_mgr$get_font_id("foo")
)

wb$styles_mgr$add(foo_style, "foo")

wb$add_worksheet("test")
wb$add_data(x = "Foo")
wb$set_cell_style(dims = "A1", style = wb$styles_mgr$get_xf_id("foo"))

exp <- c("xf-0", "foo")
got <- wb$styles_mgr$xf$name
expect_equal(exp, got)
})
2 changes: 1 addition & 1 deletion tests/testthat/test-wb_styles.R
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ test_that("wb_add_named_style() works", {
wb$add_data(dims = dims, x = name)

exp <- c(
"Bad", "Good", "Neutral", "Calculation", "Check Cell", "Explanatory Text",
"Normal", "Bad", "Good", "Neutral", "Calculation", "Check Cell", "Explanatory Text",
"Input", "Linked Cell", "Note", "Output", "Warning Text", "Heading 1",
"Heading 2", "Heading 3", "Heading 4", "Title", "Total", "20% - Accent1",
"40% - Accent1", "60% - Accent1", "Accent1", "20% - Accent2",
Expand Down

0 comments on commit cee1122

Please sign in to comment.