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

[styles] Create named styles. closes #274 #628

Merged
merged 9 commits into from
Jun 6, 2023
Merged

[styles] Create named styles. closes #274 #628

merged 9 commits into from
Jun 6, 2023

Conversation

JanMarvin
Copy link
Owner

@JanMarvin JanMarvin commented May 27, 2023

Steps to create named styles

In MS365 Excel there are the following named styles.

library(openxlsx2)
wb <- wb_load("https://github.com/JanMarvin/openxlsx-data/raw/main/styles/Atlas.xlsx")
wb$styles_mgr$cellStyle$name
#>  [1] "20% - Accent1"    "20% - Accent2"    "20% - Accent3"    "20% - Accent4"   
#>  [5] "20% - Accent5"    "20% - Accent6"    "40% - Accent1"    "40% - Accent2"   
#>  [9] "40% - Accent3"    "40% - Accent4"    "40% - Accent5"    "40% - Accent6"   
#> [13] "60% - Accent1"    "60% - Accent2"    "60% - Accent3"    "60% - Accent4"   
#> [17] "60% - Accent5"    "60% - Accent6"    "Accent1"          "Accent2"         
#> [21] "Accent3"          "Accent4"          "Accent5"          "Accent6"         
#> [25] "Bad"              "Calculation"      "Check Cell"       "Comma"           
#> [29] "Comma [0]"        "Currency"         "Currency [0]"     "Explanatory Text"
#> [33] "Good"             "Heading 1"        "Heading 2"        "Heading 3"       
#> [37] "Heading 4"        "Input"            "Linked Cell"      "Neutral"         
#> [41] "Normal"           "Note"             "Output"           "Per cent"        
#> [45] "Title"            "Total"            "Warning Text"

## for named style title
wb$styles_mgr$styles$cellStyles[45]
#> [1] "<cellStyle name=\"Title\" xfId=\"6\" builtinId=\"15\"/>"
# -> points to
wb$styles_mgr$styles$cellStyleXfs[7]
#> [1] "<xf numFmtId=\"0\" fontId=\"2\" fillId=\"0\" borderId=\"0\" applyNumberFormat=\"0\" applyFill=\"0\" applyBorder=\"0\" applyAlignment=\"0\" applyProtection=\"0\"/>"
# -> points to
wb$styles_mgr$styles$fonts[3]
#> [1] "<font><sz val=\"18\"/><color theme=\"3\"/><name val=\"Calibri Light\"/><family val=\"2\"/><scheme val=\"major\"/></font>"
# applied via
wb$styles_mgr$styles$cellXfs[17]
#> [1] "<xf numFmtId=\"0\" fontId=\"2\" fillId=\"0\" borderId=\"0\" xfId=\"6\"/>"

Unfortunately these styles are apparently not builtins known to the openxml format, but most likely builtins for Excel. If such a style is applied to a cell, Excel adds style entries to all styles similar to how cell styling is applied per default. This means that styles do not exist upon creation, but are applied to styles.xml. Applying a named cell style creates styles in:

  • font,
  • numfmt,
  • border,
  • fill,
  • xf, and
  • cellstyle

In addition it creates so called Formatting Records in cellStyleXfs. These are used by named styles to identify their unique cell formatting. Basically it creates the cell styles in cellStyleXfs and references these in cellXfs via xfId. This way we can overwrite cellXfsstyle formats and the named style remains intact.

Thinking of it like this:

  • add a named style like Title
  • cell style references the formatting record for this name. In the formatting record all font, fill, border etc. are referenced, but application is disabled. If required, they are added in the xf records
  • style the cell further (add a border). This is applied only to xf similar to default cell styling
library(openxlsx2)

wb <- wb_workbook()$add_worksheet()

name <- "Normal"
dims <- "A1"
wb$add_data(dims = dims, x = name)

name <- "Bad"
dims <- "B1"
wb <- wb_add_named_style(wb, dims = dims, name = name)
wb$add_data(dims = dims, x = name)

name <- "Good"
dims <- "C1"
wb <- wb_add_named_style(wb, dims = dims, name = name)
wb$add_data(dims = dims, x = name)

name <- "Neutral"
dims <- "D1"
wb <- wb_add_named_style(wb, dims = dims, name = name)
wb$add_data(dims = dims, x = name)

name <- "Calculation"
dims <- "A2"
wb <- wb_add_named_style(wb, dims = dims, name = name)
wb$add_data(dims = dims, x = name)

name <- "Check Cell"
dims <- "B2"
wb <- wb_add_named_style(wb, dims = dims, name = name)
wb$add_data(dims = dims, x = name)

name <- "Explanatory Text"
dims <- "C2"
wb <- wb_add_named_style(wb, dims = dims, name = name)
wb$add_data(dims = dims, x = name)

name <- "Input"
dims <- "D2"
wb <- wb_add_named_style(wb, dims = dims, name = name)
wb$add_data(dims = dims, x = name)

name <- "Linked Cell"
dims <- "E2"
wb <- wb_add_named_style(wb, dims = dims, name = name)
wb$add_data(dims = dims, x = name)

name <- "Note"
dims <- "F2"
wb <- wb_add_named_style(wb, dims = dims, name = name)
wb$add_data(dims = dims, x = name)

name <- "Output"
dims <- "G2"
wb <- wb_add_named_style(wb, dims = dims, name = name)
wb$add_data(dims = dims, x = name)

name <- "Warning Text"
dims <- "H2"
wb <- wb_add_named_style(wb, dims = dims, name = name)
wb$add_data(dims = dims, x = name)

name <- "Heading 1"
dims <- "A3"
wb <- wb_add_named_style(wb, dims = dims, name = name)
wb$add_data(dims = dims, x = name)

name <- "Heading 2"
dims <- "B3"
wb <- wb_add_named_style(wb, dims = dims, name = name)
wb$add_data(dims = dims, x = name)

name <- "Heading 3"
dims <- "C3"
wb <- wb_add_named_style(wb, dims = dims, name = name)
wb$add_data(dims = dims, x = name)

name <- "Heading 4"
dims <- "D3"
wb <- wb_add_named_style(wb, dims = dims, name = name)
wb$add_data(dims = dims, x = name)

name <- "Title"
dims <- "E3"
wb <- wb_add_named_style(wb, dims = dims, name = name)
wb$add_data(dims = dims, x = name)

name <- "Total"
dims <- "F3"
wb <- wb_add_named_style(wb, dims = dims, name = name)
wb$add_data(dims = dims, x = name)

for (i in seq_len(6)) {
  
  name <- paste0("20% - Accent", i)
  dims <- paste0(int2col(i), "4")
  wb <- wb_add_named_style(wb, dims = dims, name = name)
  wb$add_data(dims = dims, x = name)
  
  name <- paste0("40% - Accent", i)
  dims <- paste0(int2col(i), "5")
  wb <- wb_add_named_style(wb, dims = dims, name = name)
  wb$add_data(dims = dims, x = name)
  
  name <- paste0("60% - Accent", i)
  dims <- paste0(int2col(i), "6")
  wb <- wb_add_named_style(wb, dims = dims, name = name)
  wb$add_data(dims = dims, x = name)
  
  name <- paste0("Accent", i)
  dims <- paste0(int2col(i), "7")
  wb <- wb_add_named_style(wb, dims = dims, name = name)
  wb$add_data(dims = dims, x = name)
  
}

name <- "Comma"
dims <- "A8"
wb <- wb_add_named_style(wb, dims = dims, name = name)
wb$add_data(dims = dims, x = name)

name <- "Comma [0]"
dims <- "B8"
wb <- wb_add_named_style(wb, dims = dims, name = name)
wb$add_data(dims = dims, x = name)

name <- "Currency"
dims <- "C8"
wb <- wb_add_named_style(wb, dims = dims, name = name)
wb$add_data(dims = dims, x = name)

name <- "Currency [0]"
dims <- "D8"
wb <- wb_add_named_style(wb, dims = dims, name = name)
wb$add_data(dims = dims, x = name)

name <- "Per cent"
dims <- "E8"
wb <- wb_add_named_style(wb, dims = dims, name = name)
wb$add_data(dims = dims, x = name)

# wb$open()
Screenshot 2023-05-28 at 12 23 16

@JanMarvin JanMarvin changed the title Create named styles [WIP] Create named styles May 27, 2023
@JanMarvin JanMarvin added enhancement 😀 New feature or request styles 🎨 labels May 28, 2023
@JanMarvin JanMarvin force-pushed the cellstyles branch 4 times, most recently from bddbe31 to 6ff8a25 Compare June 4, 2023 13:13
@JanMarvin JanMarvin changed the title [WIP] Create named styles [styles] Create named styles Jun 4, 2023
@JanMarvin JanMarvin changed the title [styles] Create named styles [styles] Create named styles. closes #274 Jun 6, 2023
@JanMarvin JanMarvin merged commit 348f620 into main Jun 6, 2023
@JanMarvin JanMarvin deleted the cellstyles branch June 6, 2023 06:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement 😀 New feature or request styles 🎨
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant