Openxlsx 条件格式化分隔列

huangapple go评论130阅读模式
英文:

Openxlsx Conditional Formatting separated columns

问题

I've noticed something unusual with the conditionalFormatting() function in the Openxlsx package. When you specify a vector of columns, such as c(2,4,6), the function doesn't highlight those three columns specifically, but rather all columns from #2-#6. Consider the code below:

df <- data.frame(One = c('Dog', 'Dog'),
Two = c('Cat', 'Cat'),
Three = c('Bird', 'Bird'),
Four = c('Cow', 'Cow'),
Five = c('Horse', 'Horse'),
Six = c('Lion', 'Lion'),
Seven = c('Tiger', 'Tiger'))

style1 <- createStyle(fontName = 'Calibri',
fontSize = 11,
bgFill = "#FFC7CE")

conditionalFormatting(wb,
sheet,
rows = 1:nrow(df),
cols = c(2,4,6), #<--- treated as all columns in range 2-6
type = 'contains',
rule = "A", #<---highlight all rows that contain text
style = style1)

However, if you were to use the addStyle() function, then it seems as if it would color those three columns specifically, rather than the entire range:

style2 <- createStyle(fontName = "Calibri",
fontSize = 11,
fgFill = "#FFC7CE")

addStyle(wb,
sheet,
rows = 1:nrow(df),
cols = c(2,4,6), #<---treated as columns 2,4,6
style = style2,
gridExpand = TRUE)

I've tried using a for loop to see if it tricks it into formatting them correctly, but it doesn't.

cols.to.format <- c(2,4,6)
for(col in cols.to.format){
conditionalFormatting(wb,
sheet,
rows = 1:nrow(df),
cols = col,
style = style2,
gridExpand = TRUE)
}

Is there a way to use conditional formatting to format a specific segment of columns rather than a range?

英文:

I've noticed something unusual with the conditionalFormatting() function in the Openxlsx package. When you specify a vector of columns, such as c(2,4,6), the function doesn't highlight those three columns specifically, but rather all columns from #2-#6. Consider the code below:

  1. df &lt;- data.frame(One = c(&#39;Dog&#39;, &#39;Dog&#39;),
  2. Two = c(&#39;Cat&#39;, &#39;Cat&#39;),
  3. Three = c(&#39;Bird&#39;, &#39;Bird&#39;),
  4. Four = c(&#39;Cow&#39;, &#39;Cow&#39;),
  5. Five = c(&#39;Horse&#39;, &#39;Horse&#39;),
  6. Six = c(&#39;Lion&#39;, &#39;Lion&#39;),
  7. Seven = c(&#39;Tiger&#39;, &#39;Tiger&#39;))
  8. style1 &lt;- createStyle(fontName = &#39;Calibri&#39;,
  9. fontSize = 11,
  10. bgFill = &quot;#FFC7CE&quot;)
  11. conditionalFormatting(wb,
  12. sheet,
  13. rows = 1:nrow(df),
  14. cols = c(2,4,6), #&lt;--- treated as all columns in range 2-6
  15. type = &#39;contains&#39;,
  16. rule = &quot;A&quot;, #&lt;---highlight all rows that contain text
  17. style = style1)

However, if you were to use the addStyle() function, then it seems as if it would color those three columns specifically, rather than the entire range:

  1. style2 &lt;- createStyle(fontName = &quot;Calibri&quot;,
  2. fontSize = 11,
  3. fgFill = &quot;#FFC7CE&quot;)
  4. addStyle(wb,
  5. sheet,
  6. rows = 1:nrow(df),
  7. cols = c(2,4,6), #&lt;---treated as columns 2,4,6
  8. style = style2,
  9. gridExpand = TRUE)

I've tried using a for loop to see if it tricks it into formatting them correctly, but it doesn't.

  1. cols.to.format &lt;- c(2,4,6)
  2. for(col in cols.to.format){
  3. conditionalFormatting(wb,
  4. sheet,
  5. rows = 1:nrow(df),
  6. cols = col,
  7. style = style2,
  8. gridExpand = TRUE)
  9. }

Is there a way to use conditional formatting to format a specific segment of columns rather than a range?

答案1

得分: 1

Looping should work. conditionalFormatting in the open xml standard allows either a single cell or a cell range, but nothing like "A1:A2;C1:C2" and this is what openxlsx tries to honor. The only issue here is, that a warning could be shown to the user.

For reference this is the loop in openxlsx2:

  1. library(openxlsx2)
  2. df <- data.frame(
  3. One = c('Dog', 'Dog'),
  4. Two = c('Cat', 'Cat'),
  5. Three = c('Bird', 'Bird'),
  6. Four = c('Cow', 'Cow'),
  7. Five = c('Horse', 'Horse'),
  8. Six = c('Lion', 'Lion'),
  9. Seven = c('Tiger', 'Tiger')
  10. )
  11. wb <- wb_workbook()$add_worksheet()$add_data(x = df)
  12. cols.to.format <- c(2,4,6)
  13. for(col in cols.to.format){
  14. wb$add_conditional_formatting(
  15. rows = 1:nrow(df),
  16. cols = col,
  17. type = 'containsText',
  18. rule = "A")
  19. }
  20. if (interactive()) wb$open()

Openxlsx 条件格式化分隔列

Edit: What OP really wanted:

  1. library(openxlsx2)
  2. df <- data.frame(
  3. One = c('Dog', 'Dog'),
  4. Two = c('Cat', 'Cat'),
  5. Three = c('Bird', 'Bird'),
  6. Four = c('Cow', 'Cow'),
  7. Five = c('Horse', 'Horse'),
  8. Six = c('Lion', 'Lion'),
  9. Seven = c('Tiger', 'Tiger')
  10. )
  11. wb <- wb_workbook()$
  12. add_worksheet()$
  13. add_data(x = df)
  14. cols.to.format <- c(4,6)
  15. for(col in cols.to.format){
  16. wb$add_conditional_formatting(
  17. rows = 1:nrow(df) + 1L,
  18. cols = col,
  19. rule = 'B2="Cat"')
  20. }
  21. if (interactive()) wb$open()

Openxlsx 条件格式化分隔列

英文:

Looping should work. conditionalFormatting in the open xml standard allows either a single cell or a cell range, but nothing like "A1:A2;C1:C2" and this is what openxlsx tries to honor. The only issue here is, that a warning could be shown to the user.

For reference this is the loop in openxlsx2:

  1. library(openxlsx2)
  2. df &lt;- data.frame(
  3. One = c(&#39;Dog&#39;, &#39;Dog&#39;),
  4. Two = c(&#39;Cat&#39;, &#39;Cat&#39;),
  5. Three = c(&#39;Bird&#39;, &#39;Bird&#39;),
  6. Four = c(&#39;Cow&#39;, &#39;Cow&#39;),
  7. Five = c(&#39;Horse&#39;, &#39;Horse&#39;),
  8. Six = c(&#39;Lion&#39;, &#39;Lion&#39;),
  9. Seven = c(&#39;Tiger&#39;, &#39;Tiger&#39;)
  10. )
  11. wb &lt;- wb_workbook()$add_worksheet()$add_data(x = df)
  12. cols.to.format &lt;- c(2,4,6)
  13. for(col in cols.to.format){
  14. wb$add_conditional_formatting(
  15. rows = 1:nrow(df),
  16. cols = col,
  17. type = &#39;containsText&#39;,
  18. rule = &quot;A&quot;)
  19. }
  20. if (interactive()) wb$open()

Openxlsx 条件格式化分隔列

Edit: What OP really wanted:

  1. library(openxlsx2)
  2. df &lt;- data.frame(
  3. One = c(&#39;Dog&#39;, &#39;Dog&#39;),
  4. Two = c(&#39;Cat&#39;, &#39;Cat&#39;),
  5. Three = c(&#39;Bird&#39;, &#39;Bird&#39;),
  6. Four = c(&#39;Cow&#39;, &#39;Cow&#39;),
  7. Five = c(&#39;Horse&#39;, &#39;Horse&#39;),
  8. Six = c(&#39;Lion&#39;, &#39;Lion&#39;),
  9. Seven = c(&#39;Tiger&#39;, &#39;Tiger&#39;)
  10. )
  11. wb &lt;- wb_workbook()$
  12. add_worksheet()$
  13. add_data(x = df)
  14. cols.to.format &lt;- c(4,6)
  15. for(col in cols.to.format){
  16. wb$add_conditional_formatting(
  17. rows = 1:nrow(df) + 1L,
  18. cols = col,
  19. rule = &#39;B2=&quot;Cat&quot;&#39;)
  20. }
  21. if (interactive()) wb$open()

Openxlsx 条件格式化分隔列

huangapple
  • 本文由 发表于 2023年6月8日 17:05:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76430258.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定