【问题标题】:How do I format an entire row within xlsx based on a cell character in R如何根据 R 中的单元格字符格式化 xlsx 中的整行
【发布时间】:2019-11-14 11:47:24
【问题描述】:

我正在使用 R 编辑 xlsx 工作表。我想根据特定列中的字符值用彩色行格式化工作表,然后以 xlsx 形式保存工作簿。我在 R 中使用 xlsx 成功加载了工作簿。我可以遍历工作簿中的字符并根据条件更改特定的单元格背景颜色,但是在更改整行的颜色时遇到了麻烦。

我的问题是,我怎样才能使整行成为纯色,而不仅仅是单元格?到目前为止,我已经按照此处的说明和代码进行操作:

Color cells with specific character values in r to export to xlsx

您必须在上述链接中的代码中添加什么内容才能使整行的颜色与目标特定单元格的颜色相同?

greenStyle <- createStyle(fontColour = "#000000", bgFill = "green")
yellowStyle <- createStyle(fontColour = "#000000", bgFill = "yellow")
conditionalFormatting(wb, "entire report", cols=1:12, rows=1:2000, rule="Finished", style = greenStyle, type = "contains")
conditionalFormatting(wb, "entire report", cols=1:12, rows=1:2000, rule="In Process", style = yellowStyle, type = "contains")
saveWorkbook(wb, file, overwrite=TRUE)

【问题讨论】:

    标签: r conditional-formatting openxlsx r-xlsx


    【解决方案1】:

    这个问题是很久以前的,我想这样做,然后找到答案并想分享(我是问题中提到的帖子的作者:))

    所以,为了给整行着色,我做了一个可重现的例子:

    可重现的例子:

    dfX <- data.frame('a' = c(1:4),
           'b' = c(1:2,2:1),
           'c' = LETTERS[1:4],
           'e' = LETTERS[1:2][2:1],
           'f' = c('Finished', 'In Process', 'In Process', 'In Process'))
    
    library(openxlsx)
    wb <- createWorkbook()
    addWorksheet(wb, "Sheet", gridLines = TRUE)
    writeData(wb, "Sheet", dfX)
    
    greenRows = data.frame(which(dfX == "Finished", arr.ind=TRUE))
    yellowRows = data.frame(which(dfX == "In Process", arr.ind=TRUE))
    ## Here I create data frames where it states which rows and columns
    ## have 'Finished' and which have 'In Process'. From here I want to keep only the
    ## rows from these data frames.
    
    # Create a heading style
    Heading <- createStyle(textDecoration = "bold", border = "Bottom")
    
    # Row styles
    greenStyle <- createStyle(fontColour = "#000000", fgFill = "green")
    yellowStyle <- createStyle(fontColour = "#000000", fgFill = "yellow")
    

    重要提示:我使用"fgFill" 而不是"bgFill",因为为了做到这一点,我们将使用addStyle(而不是conditionalFormatting),并且在文档中,它指出bgFill 只是对于conditionalFormatting

    # Apply header style:
    addStyle(wb, "Sheet", cols = 1:ncol(dfX), rows = 1, style = Heading)
    
    # Apply greenStyle:
    addStyle(wb, "Sheet", cols = 1:ncol(dfX), rows = greenRows[,1]+1,
             style = greenStyle, gridExpand = TRUE)
    
    # Apply yellowStyle:
    addStyle(wb, "Sheet", cols = 1:ncol(dfX), rows = yellowRows[,1]+1,
             style = yellowStyle, gridExpand = TRUE)
    
    saveWorkbook(wb, file, overwrite=TRUE)
    

    注意"rows = "我输入greenRows[,1]+1,表示只有greenRows data.frame的第一列加1(第一行是标题,所以跳过这个)

    还要注意,在最后一行,file 部分中,您应该指定以.xlsx 结尾的文件的保存目录,例如:

    saveWorkbook(wb, file = "C:/Documents/newfile.xlsx", overwrite=TRUE)
    

    This post,虽然不是同一个问题,但帮助了我。

    【讨论】:

    • 感谢分享示例!我现在正在尝试实施它。我在这里遇到了一个障碍:“样式”%in% 类(样式)中的错误:找不到对象“duppedStyle”有什么想法吗?
    • 愚蠢的我,我复制了错误的一步,我在答案中更正了它。问题出在addStyle 部分,上面写着style 我应该输入之前定义的样式名称(分别为greenStyleyellowStyle
    猜你喜欢
    • 2014-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-20
    • 1970-01-01
    • 2014-11-19
    • 1970-01-01
    • 2018-03-13
    相关资源
    最近更新 更多