[英]How do I password-protect an Excel file created in R with write.xlsx?
[英]Password protecting an Excel file created using write.xlsx in R with openxlsx
我想用密码保护我使用特定工作流程创建的大量 .xslx 文件。 工作流程很简单,并且依赖于我使用 R 中openxlsx
中的write.xlsx
命令编写的较小数据帧的命名列表。是否有使用类似工作流程的protectWorkbook
密码保护这些文件的解决方案? 谢谢你。
library(tidyverse)
library(openxlsx)
## Create reprex using diamonds
df_ls <- diamonds %>%
select_if(is.ordered) %>%
gather(key, value) %>%
split(.$key)
#> Warning: attributes are not identical across measure variables;
#> they will be dropped
## I like to use lists to write to .xlsx
## because write.xlsx creates each object
## in the list as its own sheet and names
## it using the list names.
.path <- tempfile(fileext = ".xlsx")
write.xlsx(df_ls, file = .path)
## I want to password protect this file(s)
map(.path, ~{protectWorkbook(.x, protect = TRUE, password = "random-password")})
# Error in protectWorkbook(.x, protect = TRUE, password = "random-password") :
# First argument must be a Workbook.
您需要先创建一个工作簿对象,因为我认为这就是错误所指示的内容。 write.xlsx 未被识别为工作簿对象,这是protectWorkbook 参数所必需的。
df_ls <- diamonds %>%
select_if(is.ordered) %>%
gather(key, value) %>%
split(.$key)
wb = createWorkbook()
lapply(seq_along(df_ls), function(i){
addWorksheet(wb=wb, sheetName = names(df_ls[i])) #Add each sheet
writeData(wb, sheet = i, df_ls[[i]]) #Add data to each sheet
protectWorksheet(wb, sheet = i, protect = TRUE, password = "Password") #Protect each sheet
})
saveWorkbook(wb, "example.xlsx", overwrite = TRUE)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.