Skip to content

Conditional row formatting #433

Answered by JanMarvin
AnnePdM asked this question in Q&A
Nov 23, 2022 · 4 comments · 1 reply
Discussion options

You must be logged in to vote

Hi @AnnePdM , something like this?

library(openxlsx2)

# create style
negStyle <- create_dxfs_style(font_color = wb_colour(hex = "FF9C0006"),
                              bgFill = wb_colour(hex = "FFFFC7CE"))

# create custom row
dat <- mtcars
dat$hl <- dat$mpg < 20 & dat$vs == 1

# get excel column name
rulerow <- int2col(ncol(dat))

# create workbook add data
wb <- wb_workbook()$
  add_worksheet()$
  add_data(x = dat)

# add style to workbook
wb$styles_mgr$add(negStyle, "negStyle")

# add conditional formatting
wb$add_conditional_formatting(
  "Sheet 1",
  cols = seq_along(dat),
  rows = seq_len(nrow(dat)) + 1,
  rule = sprintf("$%s2=TRUE", rulerow),
  style = "negStyle"
)

Replies: 4 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Answer selected by JanMarvin
Comment options

You must be logged in to vote
1 reply
@JanMarvin
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants