R:使用openxlsx对Excel工作表上的偶数行和奇数行进行条件格式设置

flseospp  于 2023-02-01  发布在  其他
关注(0)|答案(1)|浏览(235)

我有多个Excel文件,每个文件有几个工作表。文件是通过R中的循环从 Dataframe 自动创建的。每个Excel文件的每个工作表都应应用格式。根据Excel文档布局的公司风格,行的颜色应互换。偶数行和奇数行应分别为白色和灰色。标题应为绿色,字母应为白色。当然,标头有自己的格式。为此,我尝试了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。标题中的字母仍然是黑色,而不是白色。此外,似乎偶数和奇数行(不是单元格中的偶数和奇数)的条件格式规则应该以另一种方式定义,但我没有发现任何东西,除了现在代码中的规则。

pgccezyw

pgccezyw1#

除非您真的需要条件格式,否则我将单元格样式直接应用于工作表,类似于应用标题样式的方式(计算输出中的行数)。这可能需要在当前循环中再进行一次循环。writeDataTable()具有bandedRows,但它将应用表格样式。
另外,如果您对openxlsx2感兴趣,这里有一个问题的解决方案(使用openxlsx2的当前主分支创建,几天后将成为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)

相关问题