我正在尝试添加多个验证并将公式添加到 Excel 文件。这是我使用的代码:
library(openxlsx)
fileTemplate <- 'New01.xlsx'
wbTemplate <- loadWorkbook(fileTemplate)
addWorksheet(wbTemplate, "Sheet1")
writeData(wbTemplate, "Sheet1", dataset)
len <- NROW(dataset)
dataValidation(wbTemplate, 2, col = 2, rows = 2:len, type = "list", value = "'Data Validation'!$A$2:$A$19")
dataValidation(wbTemplate, 2, col = 3, rows = 2:len, type = "list", value = "'Data Validation'!$B$2:$B$501")
dataValidation(wbTemplate, 2, col = 5, rows = 2:len, type = "list", value = "'Data Validation'!$C$2:$C$6")
openXL(wbTemplate)
Run Code Online (Sandbox Code Playgroud)
如果我只使用一个 dataValidation,它会打开,如果不止一个,它会抱怨文件已损坏......
不幸的是,这看起来像是在数据验证类型“列表”中发现的错误,当工作表上有多个时失败 #266。
幸运的是,有一个拉取请求试图解决这个问题。使用devtools::dev_mode(),您可以安装该tkunstek/openxlsx版本,而无需删除并重新安装 的 CRAN 版本openxlsx。
# install the devtools package
install.packages( pkgs = "devtools" )
# load necessary packages
library( devtools )
# create a new library for storing installed packages.
dev_mode(on = TRUE )
# download the PR request that fixes
# the dataValidation error
install_github( repo = "tkunstek/openxlsx" )
# load the library
library( openxlsx )
# create workbook
wb <- createWorkbook()
# initialize worksheet
addWorksheet( wb = wb, sheetName = "Sheet1" )
# add iris to Sheet1
writeData( wb = wb
, sheet = "Sheet1"
, x = iris
)
# add Excel data validation to cells
dataValidation( wb = wb
, sheet = "Sheet1"
, cols = 1:4
, rows = 2:( 1 + nrow( iris ) )
, type = "decimal"
, operator = "between"
, value = c( 0, 10 )
)
dataValidation( wb = wb
, sheet = "Sheet1"
, cols = 5
, rows = 2:( 1 + nrow( iris ) )
, type = "textLength"
, operator = "lessThanOrEqual"
, value = 10
)
# view the data in Excel
# notice that the file is no longer corrupt
openXL( file = wb )
# turn off dev_mode
dev_mode( on = FALSE ) # return to CRAN version of openxlsx
# end of script
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1744 次 |
| 最近记录: |