Openxlsx 条件格式化分隔列

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

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:

df &lt;- data.frame(One = c(&#39;Dog&#39;, &#39;Dog&#39;),
                 Two = c(&#39;Cat&#39;, &#39;Cat&#39;),
                 Three = c(&#39;Bird&#39;, &#39;Bird&#39;), 
                 Four = c(&#39;Cow&#39;, &#39;Cow&#39;),
                 Five = c(&#39;Horse&#39;, &#39;Horse&#39;),
                 Six = c(&#39;Lion&#39;, &#39;Lion&#39;),
                 Seven = c(&#39;Tiger&#39;, &#39;Tiger&#39;))
    
    style1 &lt;- createStyle(fontName = &#39;Calibri&#39;, 
    fontSize = 11,
    bgFill = &quot;#FFC7CE&quot;)
    
    conditionalFormatting(wb, 
    sheet, 
    rows = 1:nrow(df),
    cols = c(2,4,6), #&lt;--- treated as all columns in range 2-6
    type = &#39;contains&#39;,
    rule = &quot;A&quot;, #&lt;---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 &lt;- createStyle(fontName = &quot;Calibri&quot;, 
fontSize = 11,
fgFill = &quot;#FFC7CE&quot;)

addStyle(wb, 
sheet, 
rows = 1:nrow(df),
cols = c(2,4,6), #&lt;---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 &lt;- 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?

答案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:

library(openxlsx2)

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')
)

wb <- wb_workbook()$add_worksheet()$add_data(x = df)

cols.to.format <- c(2,4,6)
for(col in cols.to.format){
  wb$add_conditional_formatting(
    rows = 1:nrow(df),
    cols = col,
    type = 'containsText',
    rule = "A")
}

if (interactive()) wb$open()

Openxlsx 条件格式化分隔列

Edit: What OP really wanted:

library(openxlsx2)

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')
)

wb <- wb_workbook()$
  add_worksheet()$
  add_data(x = df)

cols.to.format <- c(4,6)
for(col in cols.to.format){
  wb$add_conditional_formatting(
    rows = 1:nrow(df) + 1L,
    cols = col,
    rule = 'B2="Cat"')
}

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:

library(openxlsx2)

df &lt;- data.frame(
  One   = c(&#39;Dog&#39;, &#39;Dog&#39;),
  Two   = c(&#39;Cat&#39;, &#39;Cat&#39;),
  Three = c(&#39;Bird&#39;, &#39;Bird&#39;), 
  Four  = c(&#39;Cow&#39;, &#39;Cow&#39;),
  Five  = c(&#39;Horse&#39;, &#39;Horse&#39;),
  Six   = c(&#39;Lion&#39;, &#39;Lion&#39;),
  Seven = c(&#39;Tiger&#39;, &#39;Tiger&#39;)
)

wb &lt;- wb_workbook()$add_worksheet()$add_data(x = df)

cols.to.format &lt;- c(2,4,6)
for(col in cols.to.format){
  wb$add_conditional_formatting(
    rows = 1:nrow(df),
    cols = col,
    type = &#39;containsText&#39;,
    rule = &quot;A&quot;)
}

if (interactive()) wb$open()

Openxlsx 条件格式化分隔列

Edit: What OP really wanted:

library(openxlsx2)

df &lt;- data.frame(
  One   = c(&#39;Dog&#39;, &#39;Dog&#39;),
  Two   = c(&#39;Cat&#39;, &#39;Cat&#39;),
  Three = c(&#39;Bird&#39;, &#39;Bird&#39;), 
  Four  = c(&#39;Cow&#39;, &#39;Cow&#39;),
  Five  = c(&#39;Horse&#39;, &#39;Horse&#39;),
  Six   = c(&#39;Lion&#39;, &#39;Lion&#39;),
  Seven = c(&#39;Tiger&#39;, &#39;Tiger&#39;)
)

wb &lt;- wb_workbook()$
  add_worksheet()$
  add_data(x = df)

cols.to.format &lt;- c(4,6)
for(col in cols.to.format){
  wb$add_conditional_formatting(
    rows = 1:nrow(df) + 1L,
    cols = col,
    rule = &#39;B2=&quot;Cat&quot;&#39;)
}

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:

确定