【问题标题】:R: Conditional formatting of even and odd rows on the Excel sheet with openxlsxR:使用 openxlsx 对 Excel 工作表中的偶数行和奇数行进行条件格式化
【发布时间】:2023-01-29 20:57:37
【问题描述】:

我有多个 Excel 文件,每个文件都有几张纸。文件是通过 R 中的循环从数据帧自动创建的。每个 Excel 文件的每个工作表都应应用格式。根据 Excel 文档布局的公司风格,行的颜色应该互换。偶数行和奇数行应分别为白色和灰色。标题应该是绿色的,它的字母应该是白色的。当然,header 有自己的格式。为此,我尝试使用包 openxlsx 中的函数“conditionalFormatting”。不幸的是我只达到了部分结果。

我尝试应用此代码:

header_style <- createStyle(fontSize = 10, fontName = "SEGOE UI Semibold", fontColour = "#FFFFFF",
                halign = "left", valign = "top", fgFill = "#85BC22", border = "LeftRight",
                borderColour = "#FFFFFF", borderStyle = "thin", wrapText = TRUE)
style_even <- createStyle(fontSize = 10, fontName = "SEGOE UI", fontColour = "#000000",
              halign = "left", valign = "bottom", fgFill = "#FFFFFF", border = "LeftRight",
              borderColour = "#FFFFFF", borderStyle = "thin", wrapText = TRUE)
style_odd <- createStyle(fontSize = 10, fontName = "SEGOE UI", fontColour = "#000000",
              halign = "left", valign = "bottom", fgFill = "#D9D9D9", border = "LeftRight",
              borderColour = "#FFFFFF", borderStyle = "thin", wrapText = TRUE)
  
for (j in 1:number_of_sheets)
       {
    sheet_wb <- read.xlsx(wb, sheet = j, colNames = FALSE)
    conditionalFormatting(wb, sheet = j, rows = 1:nrow(sheet_wb), cols = 1:ncol(sheet_wb),
                          rule = "EVEN(ROW($A1))=ROW($A1)", style = style_even)
    conditionalFormatting(wb, sheet = j, rows = 1:nrow(sheet_wb), cols = 1:ncol(sheet_wb),
                          rule = "ODD(ROW($A1))=ROW($A1)", style = style_odd)
    addStyle(wb, sheet = j, header_style, rows = 1, cols = 1:ncol(sheet_wb), gridExpand = TRUE)
    rm(sheet_wb)
    }

然而,偶数行和奇数行仍然是白色的。他们的字体仍然是 Calibri,大小为 11。标题中的字母仍然是黑色,而不是白色。而且,似乎偶数行和奇数行(单元格中不是偶数和奇数)的条件格式规则应该以另一种方式定义,但除了现在代码中的规则外,我没有找到任何东西。

【问题讨论】:

  • openxlsx 是否允许使用表格样式和单元格样式?表格样式可以包含交替行着色,而无需条件格式规则。
  • 请提供足够的代码,以便其他人可以更好地理解或重现问题。
  • 抛开循环不谈,你能让你的代码只在一张纸上工作吗?
  • 尝试将 type = "expression" 添加到 conditionalFormatting 函数。

标签: r openxlsx


【解决方案1】:

除非你真的想要条件格式,否则我将单元格样式直接应用于工作表,类似于你应用标题样式的方式(计算输出中的行数)。这可能需要在当前循环内再循环一次。 writeDataTable()bandedRows 为此,但它将应用表格样式。

否则,如果您对 openxlsx2 感兴趣,这里是您问题的解决方案(使用 openxlsx2s 当前主分支创建,将在几天内发布 0.5):

library(openxlsx2)

in_xlsx <- temp_xlsx()
out_xlsx <- temp_xlsx()

# create example data
wb <- wb_workbook() %>%
  wb_add_worksheet() %>%
  wb_add_data(x = mtcars) %>% # A1:K33
  wb_add_worksheet() %>%
  wb_add_data(x = iris) %>% 
  wb_save(in_xlsx)

# colloring the tables in an unknown workbook
wb <- wb_load(in_xlsx)
for (i in seq_along(wb$worksheets)) {
  
  # get columns for our data
  dims <- wb %>% wb_data(sheet = i) %>% attr("dims")
  TO <- int2col(ncol(dims))
  ROW <- nrow(dims)
  
  # assume that they all begin at A1 and have a single header row
  header_dim <- sprintf("A1:%s1", TO)
  body_dim   <- sprintf("A2:%s%s", TO, ROW)
  
  wb <- wb %>% 
    # style the header
    wb_add_fill(sheet = i, dims = header_dim, color = wb_color("#85BC22")) %>% 
    wb_add_font(sheet = i, dims = header_dim, color = wb_color("white"),
                size = "10", name = "Segoe Ui") %>% 
    wb_add_border(sheet = i, dims = header_dim,
                  top_color = wb_color("white"), bottom_color = wb_color("white"),
                  left_color = wb_color("white"), right_color = wb_color("white"),
                  inner_vcolor = wb_color("white"),
                  top_border = "thin", bottom_border = "thin",
                  left_border = "thin", right_border = "thin",
                  inner_vgrid = "thin") %>% 
    # fill the background of every second row gray
    wb_add_fill(sheet = i, dims = body_dim, color = wb_color("#D9D9D9"),
                every_nth_row = 2) %>% 
    # set fonts and border for body
    wb_add_font(sheet = i, dims = body_dim,
                size = "10", name = "Segoe Ui") %>% 
    wb_add_border(sheet = i, dims = body_dim,
                  top_color = wb_color("white"), bottom_color = wb_color("white"),
                  left_color = wb_color("white"), right_color = wb_color("white"),
                  inner_vcolor = wb_color("white"),
                  top_border = "thin", bottom_border = "thin",
                  left_border = "thin", right_border = "thin",
                  inner_vgrid = "thin")
}

wb %>% wb_save(out_xlsx)

# xl_open(out_xlsx)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-02
    • 1970-01-01
    • 2012-06-06
    • 1970-01-01
    • 1970-01-01
    • 2017-03-18
    相关资源
    最近更新 更多