我在 R 中加载了一个 Excel 工作簿,并希望对矩形单元格范围内的边框进行一些格式化。
- 我想在所有单元格之间设置一个细边框
- 在单元格范围的外部放置一个粗边框。
目前,我只能看到以下方法(从下面的代码开始):
- 在范围内添加细边框
- 用左粗边框和其他细边框覆盖左侧单元格
- 用右粗边框和其他细边框覆盖右单元格
- 用正确的边框分别覆盖每个角单元
有没有更简单的方法来实现这一点?
编辑1:
如果我stack = TRUE
在第二次通话中使用,我可以跳过角落:
library(openxlsx)
wb <- openxlsx::createWorkbook()
openxlsx::addWorksheet(
wb = wb,
sheetName = "Borders"
)
rangeRows = 2:5
rangeCols = 4:8
insideBorders <- openxlsx::createStyle(
border = c("top", "bottom", "left", "right"),
borderStyle = "thin"
)
openxlsx::addStyle(
wb = wb,
sheet = "Borders",
style = insideBorders,
rows = rangeRows,
cols = rangeCols,
gridExpand = TRUE
)
openxlsx::openXL(wb)
## left borders
openxlsx::addStyle(
wb = wb,
sheet = "Borders",
style = openxlsx::createStyle(
border = c("left"),
borderStyle = c("thick")
),
rows = rangeRows,
cols = rangeCols[1],
stack = TRUE,
gridExpand = TRUE
)
##right borders
openxlsx::addStyle(
wb = wb,
sheet = "Borders",
style = openxlsx::createStyle(
border = c("right"),
borderStyle = c("thick")
),
rows = rangeRows,
cols = tail(rangeCols, 1),
stack = TRUE,
gridExpand = TRUE
)
## top borders
openxlsx::addStyle(
wb = wb,
sheet = "Borders",
style = openxlsx::createStyle(
border = c("top"),
borderStyle = c("thick")
),
rows = rangeRows[1],
cols = rangeCols,
stack = TRUE,
gridExpand = TRUE
)
##bottom borders
openxlsx::addStyle(
wb = wb,
sheet = "Borders",
style = openxlsx::createStyle(
border = c("bottom"),
borderStyle = c("thick")
),
rows = tail(rangeRows, 1),
cols = rangeCols,
stack = TRUE,
gridExpand = TRUE
)
openxlsx::openXL(wb)
原始代码:
library(openxlsx)
wb <- openxlsx::createWorkbook()
openxlsx::addWorksheet(
wb = wb,
sheetName = "Borders"
)
rangeRows = 2:5
rangeCols = 4:8
insideBorders <- openxlsx::createStyle(
border = c("top", "bottom", "left", "right"),
borderStyle = "thin"
)
openxlsx::addStyle(
wb = wb,
sheet = "Borders",
style = insideBorders,
rows = rangeRows,
cols = rangeCols,
gridExpand = TRUE
)
openxlsx::openXL(wb)
leftBorders <- openxlsx::createStyle(
border = c("top", "bottom", "left", "right"),
borderStyle = c("thin", "thin", "thick", "thin")
)
openxlsx::addStyle(
wb = wb,
sheet = "Borders",
style = leftBorders,
rows = rangeRows,
cols = rangeCols[1],
gridExpand = TRUE
)
openxlsx::openXL(wb)