You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在R的openxlsx2中通过条件格式正确设置字体与背景颜色?

解决openxlsx2条件格式中字体与背景颜色的设置问题

你的代码存在两处参数命名错误,导致样式无法正常生效:

  • 字体颜色参数需使用fontColor,而非font_color
  • 单元格背景色参数需使用fgFill,而非bgFill(在openxlsx2的dxfs样式体系中,前景填充色对应单元格的背景显示色)

修正后的完整代码如下:

library(openxlsx2)

wb_workbook(creator = "Jorge Hernandez", title = "accounts MX summarise", category = "sales") |>
  wb_add_worksheet("Evolucion", gridLines = F, tabColor = "#4e6fa3") |>
  wb_add_data(
    x = iris
  ) |>
  wb_add_conditional_formatting(type = "expression",
                                cols = 1:ncol(iris),
                                rows = 2:200,
                                style = create_dxfs_style(fontColor = wb_color(hex = "FF9C6500"),
                                                          fgFill = wb_color(hex = "FFFFEB9C")),
                                rule = ">0" ) |>
  wb_save(
    path = "/Users/jorgehca/Desktop/data.xlsx"
  )

运行这段代码后,即可正确为满足>0条件的单元格应用指定的字体颜色和背景填充色。

内容的提问来源于stack exchange,提问作者Jorge Hernández

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 15:52:37