根據此處的檔案和眾多答案,您需要將樣式應用于單個行和列索引,作為串列傳遞給 addStyle。
這似乎適用于許多其他 excel 庫。根據openpyxl,需要將樣式應用于單個單元格作為檔案格式的限制。
以這種方式格式化會導致任何具有大量空樣式單元格的 Excel 電子表格的檔案大小膨脹。同時,使用 ctrl A 將樣式應用于 excel 應用程式中的整個作業表可以有效地執行此樣式。
有誰知道發生了什么?為什么不可能實作這種看似受支持的行為?
我在嘗試使用 openxlsx 對作業表中的所有單元格應用前景色時遇到了這個問題。我閱讀了其他答案,這表明必須將樣式應用于單個單元格作為 xlsx 格式的限制。在注意到生成的作業簿和通過桌面應用程式生成的檔案之間的檔案大小差異后,我發現自己質疑我得到的答案。
uj5u.com熱心網友回復:
可以指定樣式,盡管您使用的庫必須支持它。使用openxlsx20.3.1 版,您可以這樣做:
library(openxlsx2)
# Create the colors. It looks like every colored row must start with a cell in the requested
# color otherwise the A column is empty for this row.
# Colored rows do not share this restriction. All we need is a single row in a style
wb <- wb_workbook() %>%
wb_add_worksheet() %>%
wb_add_fill(dims = "D1", color = wb_colour("orange")) %>%
wb_add_fill(dims = "A4:A6", color = wb_colour("blue"))
# get the styles 1 and 2 in our case
wb %>% wb_get_cell_style(dims = "D1")
#> [1] "1"
wb %>% wb_get_cell_style(dims = "A4")
#> [1] "2"
# apply style 1 (simply assign the xml string)
wb$worksheets[[1]]$cols_attr <- "<col min=\"4\" max=\"6\" width=\"8.83203125\" style=\"1\"/>"
# apply style 2 (extract, modify and insert)
rows <- wb$worksheets[[1]]$sheet_data$row_attr
rows$customFormat[rows$r %in% c(4:6)] <- "1"
rows$s[rows$r %in% c(4:6)] <- "2"
wb$worksheets[[1]]$sheet_data$row_attr <- rows
# check the final result
wb$open()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/532753.html
