Skip to content

Commit

Permalink
Set of tweaks (change to the color name when I know it in other places.
Browse files Browse the repository at this point in the history
Use `rows` instead of `row`
  • Loading branch information
olivroy committed Jul 19, 2023
1 parent e854a4d commit d250b96
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 41 deletions.
50 changes: 22 additions & 28 deletions vignettes/conditional-formatting.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ library(openxlsx2)

```{r}
wb <- wb_workbook()
wb$add_dxfs_style(name = "negStyle", font_color = wb_color(hex = "FF9C0006"), bgFill = wb_color(hex = "FFFFC7CE"))
wb$add_dxfs_style(name = "posStyle", font_color = wb_color(hex = "FF006100"), bgFill = wb_color(hex = "FFC6EFCE"))
wb$add_dxfs_style(name = "negStyle", font_color = wb_color(hex = "#FF9C0006"),bg_fill = wb_color(hex = "#FFFFC7CE"))
wb$add_dxfs_style(name = "posStyle", font_color = wb_color(hex = "#FF006100"), bg_fill = wb_color(hex = "#FFC6EFCE"))
```

## Rule applies to all each cell in range
Expand All @@ -43,8 +43,7 @@ wb$add_conditional_formatting(
)
wb$add_conditional_formatting(
"cellIs",
cols = 1,
rows = 1:11,
dims = "A1:A11",
rule = "==0",
style = "posStyle"
)
Expand Down Expand Up @@ -92,8 +91,7 @@ wb$add_conditional_formatting(
)
wb$add_conditional_formatting(
"Moving Col",
cols = 1:2,
rows = 1:11,
dims = "A1:B11",
rule = "A$1>0",
style = "posStyle"
)
Expand All @@ -117,8 +115,7 @@ wb$add_conditional_formatting(
)
wb$add_conditional_formatting(
"Dependent on",
cols = 1:2,
rows = 1:11,
dims = "A1:B11",
rule = "$A$1>0",
style = "posStyle"
)
Expand All @@ -130,15 +127,13 @@ wb$add_conditional_formatting(
wb$add_data("Dependent on", data.frame(x = 1:10, y = runif(10)), startRow = 15)
wb$add_conditional_formatting(
"Dependent on",
cols = 1,
rows = 16:25,
dims = "A16:A25",
rule = "B16<0.5",
style = "negStyle"
)
wb$add_conditional_formatting(
"Dependent on",
cols = 1,
rows = 16:25,
dims = "A16:A25",
rule = "B16>=0.5",
style = "posStyle"
)
Expand All @@ -155,8 +150,7 @@ wb$add_worksheet("Duplicates")
wb$add_data("Duplicates", sample(LETTERS[1:15], size = 10, replace = TRUE))
wb$add_conditional_formatting(
"Duplicates",
cols = 1,
rows = 1:10,
dims = "A1:A10",
type = "duplicatedValues"
)
```
Expand All @@ -173,8 +167,7 @@ wb$add_worksheet("containsText")
wb$add_data("containsText", sapply(1:10, fn))
wb$add_conditional_formatting(
"containsText",
cols = 1,
rows = 1:10,
dims = "A1:A10",
type = "containsText",
rule = "A"
)
Expand All @@ -189,11 +182,12 @@ knitr::include_graphics("img/cf_contains_no_text.jpg")

```{r}
fn <- function(x) paste(sample(LETTERS, 10), collapse = "-")
wb$add_data("notcontainsText", sapply(1:10, fn))
my_dat <- sapply(1:10, fn)
wb$add_data("notcontainsText", x =my_dat)
wb$add_conditional_formatting(
"notcontainsText",
cols = 1,
rows = 1:10,
dims = wb_dims(x = my_dat),
type = "notContainsText",
rule = "A"
)
Expand All @@ -211,8 +205,7 @@ wb$add_worksheet("beginsWith")
wb$add_data("beginsWith", sapply(1:100, fn))
wb$add_conditional_formatting(
"beginsWith",
cols = 1,
rows = 1:100,
dims = "A1:A100",
type = "beginsWith",
rule = "A"
)
Expand All @@ -227,11 +220,10 @@ knitr::include_graphics("img/cf_ends_with.jpg")
```{r}
fn <- function(x) paste(sample(LETTERS, 10), collapse = "-")
wb$add_worksheet("endsWith")
wb$add_data("endsWith", sapply(1:100, fn))
wb$add_data("endsWith",x = sapply(1:100, fn))
wb$add_conditional_formatting(
"endsWith",
cols = 1,
rows = 1:100,
dims = "A1:A100",
type = "endsWith",
rule = "A"
)
Expand All @@ -246,17 +238,18 @@ knitr::include_graphics("img/cf_color_scale.jpg")
```{r}
df <- read_xlsx("https://github.com/JanMarvin/openxlsx-data/raw/main/readTest.xlsx", sheet = 5)
wb$add_worksheet("colorScale", zoom = 30)
wb$add_data("colorScale", df, colNames = FALSE) ## write data.frame
wb$add_data(x = df, col_names = FALSE) ## write data.frame
```

Rule is a vector or colors of length 2 or 3 (any hex color or any of `colors()`).
If rule is `NULL`, min and max of cells is used. Rule must be the same length as style or L.

```{r}
wb$add_conditional_formatting(
"colorScale",
cols = c(1, ncol(df)),
sheet = "colorScale",
# dims = wb_dims(x = df, col_names = FALSE),
rows = seq_len(nrow(df)),
cols = c(1, ncol(df)),
style = c("black", "white"),
rule = c(0, 255),
type = "colorScale"
Expand All @@ -265,6 +258,7 @@ wb$set_col_widths("colorScale", cols = seq_along(df), widths = 1.07)
wb$set_row_heights("colorScale", rows = seq_len(nrow(df)), heights = 7.5)
```


## Databars

```{r echo=FALSE, warning=FALSE, out.width="100%"}
Expand Down Expand Up @@ -297,7 +291,7 @@ wb <- wb_add_conditional_formatting(
wb$add_data("databar", -5:5, start_col = 5)
wb <- wb_add_conditional_formatting(
wb,
"databar",
sheet = "databar",
dims = "E1:E11",
type = "dataBar",
style = c("#a6a6a6"),
Expand Down
2 changes: 1 addition & 1 deletion vignettes/openxlsx2_charts_manual.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ wb$add_worksheet("add_mschart")$add_mschart(dims = "A10:G25", graph = mylc)
# write data starting at B2
wb$add_worksheet("add_mschart - wb_data")$
add_data(x = mtcars, dims = "B2")$
add_data(x = data.frame(name = rownames(mtcars)), dims = wb_dims(start_row = 2))
add_data(x = data.frame(name = rownames(mtcars)), dims = "A2")
# create wb_data object this will tell this mschart
# from this PR to create a file corresponding to openxlsx2
Expand Down
27 changes: 15 additions & 12 deletions vignettes/openxlsx2_style_manual.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ border_col <- wb_color(theme = 1)
border_sty <- "thin"
# using wb_dims() to avoid counting manually which `dims` are spanned.
dims_mat_header <- wb_dims(col = seq_len(ncol(mat)), row = 1)
dims_mat_header <- wb_dims(col = seq_len(ncol(mat)), rows = 1)
# returns "A1:AB1"
# prepare workbook with data and formated first row
wb <- wb_workbook() %>%
Expand All @@ -53,8 +53,8 @@ wb <- wb_workbook() %>%
right_color = border_col, right_border = border_sty,
inner_hcolor = border_col, inner_hgrid = border_sty
) %>%
wb_add_fill(dims = dims_mat_header, color = wb_color(hex = "FF334E6F")) %>%
wb_add_font(dims = dims_mat_header, name = "Arial", bold = TRUE, color = wb_color(hex = "FFFFFFFF"), size = 20) %>%
wb_add_fill(dims = dims_mat_header, color = wb_color(hex = "#FF334E6F")) %>%
wb_add_font(dims = dims_mat_header, name = "Arial", bold = TRUE, color = wb_color("white"), size = 20) %>%
wb_add_cell_style(dims = dims_mat_header, horizontal = "center", textRotation = 45)
# create various number formats
Expand All @@ -73,6 +73,7 @@ for (i in seq_along(x)) {
```

### the long way: using bare metal functions

```{r}
# create workbook
wb <- wb_workbook() %>% wb_add_worksheet("test")
Expand All @@ -81,11 +82,11 @@ wb <- wb_workbook() %>% wb_add_worksheet("test")
set.seed(123)
mat <- matrix(rnorm(28 * 28, mean = 44444, sd = 555), ncol = 28)
colnames(mat) <- make.names(seq_len(ncol(mat)))
wb$add_data(x = mat, colNames = TRUE)
wb$add_data(x = mat, col_names = TRUE)
# create a border style and assign it to the workbook
black <- wb_color(hex = "FF000000")
# same as wb_color("black") or wb_color(hex = "#FF000000")
black <- wb_color("black")
# same as wb_color("black") or wb_color(hex = "#FF000000") or wb_color("FF000000")
new_border <- create_border(
bottom = "thin", bottom_color = black,
top = "thin", top_color = black,
Expand All @@ -96,11 +97,11 @@ wb$styles_mgr$add(new_border, "new_border")
# create a fill style and assign it to the workbook
new_fill <- create_fill(patternType = "solid", fgColor = wb_color(hex = "FF334E6F"))
new_fill <- create_fill(patternType = "solid", fgColor = wb_color(hex = "#FF334E6F"))
wb$styles_mgr$add(new_fill, "new_fill")
# create a font style and assign it to the workbook
new_font <- create_font(sz = 20, name = "Arial", b = TRUE, color = wb_color(hex = "FFFFFFFF"))
new_font <- create_font(sz = 20, name = "Arial", b = TRUE, color = wb_color("white"))
wb$styles_mgr$add(new_font, "new_font")
# create a new cell style, that uses the fill, the font and the border style
Expand All @@ -116,7 +117,9 @@ new_cellxfs <- create_cell_style(
wb$styles_mgr$add(new_cellxfs, "new_styles")
# assign the new cell style to the header row of our data set
cell <- wb_dims(col = seq_len(ncol(mat)), row = 1)
# Achieve the same with `wb_dims()` and sprintf
# cell <- sprintf("A1:%s1", int2col(nrow(mat)))
cell <- wb_dims(col = seq_len(ncol(mat)), rows = 1)
wb <- wb %>% wb_set_cell_style(
dims = cell,
style = wb$styles_mgr$get_xf_id("new_styles")
Expand Down Expand Up @@ -149,7 +152,7 @@ wb$worksheets[[1]]$sheetPr <- xml_node_create(
"sheetPr",
xml_children = xml_node_create(
"tabColor",
xml_attributes = wb_color(hex = "FF00FF00")
xml_attributes = wb_color(hex = "#FF00FF00")
)
)
Expand Down Expand Up @@ -270,7 +273,7 @@ wb$add_worksheet("S3", grid_lines = FALSE)$
wb$add_border(
dims = "A2:K33",
inner_vgrid = "thin",
inner_vcolor = wb_color(hex = "FF808080")
inner_vcolor = wb_color(hex = "#FF808080")
)
# no inner grid
wb$add_worksheet("S4", grid_lines = FALSE)$add_data(x = mtcars)
Expand Down Expand Up @@ -327,7 +330,7 @@ wb <- wb_workbook() %>%
)
# create a border style and assign it to the workbook
black <- wb_color(hex = "FF000000")
black <- wb_color("black")
top_border <- create_border(
top = "double", top_color = black,
bottom = "thin", bottom_color = black
Expand Down

0 comments on commit d250b96

Please sign in to comment.