[英]Applying style to all sheets of a workbook using openxlsx package in R
While using openxlsx
package in R, I want to apply a particular style to all sheets of a workbook. 在R中使用
openxlsx
包时,我想将特定样式应用于工作簿的所有工作表。 I am using addStyle()
after defining a style. 定义样式后,我正在使用
addStyle()
。 Here, I need to specify the sheet name or number on which style needs to be added 在这里,我需要指定需要添加样式的工作表名称或编号
addStyle(wb, sheet=1, style, rows, cols, gridExpand = FALSE, stack = FALSE)
Is there a simple way to do this without using any loop or adding the style individually per sheet? 有没有一种简单的方法可以执行此操作而无需使用任何循环或每张纸单独添加样式? Reason being no.of sheets may change for every run
原因是页数可能每次运行都会更改
You don't have to use a fixed number for the loop. 您不必为循环使用固定的数字。 If you loop through
names(wb)
ie 如果您遍历
names(wb)
即
for(curr_sheet in names(wb))
and pass that value into the sheet
parameter it will work regardless of when you change the number of sheets. 并将该值传递到工作
sheet
参数中,无论何时更改工作sheet
数,该参数都将起作用。
I don't think there is a way to use addStyle
for all sheets without some kind of for loop/apply function. 我认为没有某种for循环/应用功能,没有一种方法可以对所有工作表使用
addStyle
。
Just to extend on Danny's conjecture 只是为了扩大丹尼的猜想
I don't think there is a way to use
addStyle
for all sheets without some kind of for loop/apply function.我认为没有某种for循环/应用功能,没有一种方法可以对所有工作表使用
addStyle
。
and for the sake of completeness - there is another possibility: the purrr
package from tidyverse
! 和完整性的缘故-还有另外一种可能:在
purrr
从包tidyverse
! It is a way of functional programming that allows you to avoid loops. 这是一种功能编程的方法,可让您避免循环。
A minimal reproducible example on how to add styles to multiple sheets via openxlsx
and data.frames with different lenghts: 关于如何通过具有不同长度的
openxlsx
和data.frames向多个工作表添加样式的最小可复制示例:
library(purrr)
library(openxlsx)
# random data.frames
a <- data.frame(name = c("A", "B", "C"),
value = c(0.01, 0.02, 0.03))
b <- data.frame(name = c("D", "E", "F", "G"),
value = c(0.01, 0.02, 0.03, 0.04))
# functional programing is mainly based on lists
l <- list(a, b)
# Excel Sheets
sheet_names <- c("SheetA", "SheetB")
# Create the workbook
wb <- createWorkbook()
# Walk replaces the for-loop to create empty sheets given our variable
# sheet_names = .x
walk(sheet_names, ~addWorksheet(wb, .x))
# Walk2 takes a second parameter, in our case the list of data.frames
# sheet_names = .x
# l = .y
walk2(sheet_names, l, ~writeData(wb, .x, .y))
# To apply a certain style, i.e. percentage format, we need to know the number of rows
# for each data.frame. Thus, walk2 helps us to circumvent a nested for-loop
# sheet_names = .x
# l = .y
walk2(sheet_names, l, ~addStyle(wb, .x,
style = createStyle(numFmt='PERCENTAGE'),
rows = 2:(nrow(.y)+1), cols = 2, gridExpand = TRUE))
# Save the workbook
saveWorkbook(wb, "myfile.xlsx", overwrite = T)
Of course, in such a small example a for-loop would be easy to implement, but when requirements get tougher and you want to increase the speed of your script, go for purrr
and functional programming. 当然,在这么小的示例中,for循环将很容易实现,但是当要求变得更加严格并且您想提高脚本速度时,请进行
purrr
和函数式编程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.