gpt4 book ai didi

r - Openxlsx 多次验证损坏输出文件

转载 作者:行者123 更新时间:2023-12-02 13:34:36 25 4
gpt4 key购买 nike

我正在尝试添加多个验证并将公式添加到 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)

如果我只使用一个 dataValidation,它会正常打开,如果超过一个,它会提示文件已损坏......

最佳答案

不幸的是,这看起来像是 data validation type 'list' fails when there are more than one on a sheet #266 中发现的错误。 。

幸运的是,there's a pull request that attempts to fix this issue 。使用devtools::dev_mode() ,您可以安装tkunstek/openxlsx版本,无需删除并重新安装 openxlsx 的 CRAN 版本.

# 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

答案来自How to install development version of R packages github repository

关于r - Openxlsx 多次验证损坏输出文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48844046/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com