使用 R 包 openxlsx 将样式应用到整个 Excel 工作表

Jas*_*ter 5 excel r openxlsx

我正在使用名为openxlsx的 R 包。我已经创建了AlignStyle如下所示的内容。我想将此样式应用于标题为“test-sheet”的整个工作表。当我尝试将此样式应用于 Excel 工作表的所有行和所有列时,我得到一个Error in 1:Inf : result would be too long a vector.

# Define a style
AlignStyle <- createStyle(halign = "CENTER", valign = "TOP")

# Apply the style
addStyle(wb, "test-sheet", style = AlignStyle, rows = 1:Inf, cols = 1:Inf, gridExpand = TRUE)
Run Code Online (Sandbox Code Playgroud)

我知道我可以做类似的事情rows = 1:nrows(df),但我的数据框的名称并不总是df

您知道我可以使用openxlsx R 包将样式应用到整个工作表或整个工作簿的另一种方法吗?

小智 2

你可以这样做:

map(df, ~addStyle(wb, "test-sheet", style = AlignStyle, rows = 0:nrow(.x)+1, cols = 0:ncol(.x)+1, gridExpand = TRUE)
Run Code Online (Sandbox Code Playgroud)

如果第一行是标题,则在末尾添加 +1 将包括所有每个单元格