gpt4 book ai didi

r - 如何使用 R 中的 `openxlsx` 包在单元格范围周围应用粗边框

转载 作者:行者123 更新时间:2023-12-03 13:40:51 25 4
gpt4 key购买 nike

我在 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)

    最佳答案

    我知道这是一个较旧的问题,但如果有人遇到这个问题,这里有一个函数,它只将边框应用于您传递的行和列参数的外部:

    OutsideBorders <-
    function(wb_,
    sheet_,
    rows_,
    cols_,
    border_col = "black",
    border_thickness = "medium") {
    left_col = min(cols_)
    right_col = max(cols_)
    top_row = min(rows_)
    bottom_row = max(rows_)

    sub_rows <- list(c(bottom_row:top_row),
    c(bottom_row:top_row),
    top_row,
    bottom_row)

    sub_cols <- list(left_col,
    right_col,
    c(left_col:right_col),
    c(left_col:right_col))

    directions <- list("Left", "Right", "Top", "Bottom")

    mapply(function(r_, c_, d) {
    temp_style <- createStyle(border = d,
    borderColour = border_col,
    borderStyle = border_thickness)
    addStyle(
    wb_,
    sheet_,
    style = temp_style,
    rows = r_,
    cols = c_,
    gridExpand = TRUE,
    stack = TRUE
    )

    }, sub_rows, sub_cols, directions)
    }

    # Function call example
    OutsideBorders(
    wb_,
    sheet_ = 1,
    rows_ = 1:nrow(test_sheet),
    cols_ = 1:ncol(test_sheet)
    )

    关于r - 如何使用 R 中的 `openxlsx` 包在单元格范围周围应用粗边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54322814/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com