我有一个要读入 R 的 Excel 电子表格,它受密码保护并且单元格已锁定。我可以使用 excel.link 导入受密码保护的文件,但我不知道如何解锁/取消保护单元格。 excel.link 给我这个错误:
> <checkErrorInfo> 80020009 Error in top_left_corner[["CurrentRegion"]]
> : You cannot use this command on a protected sheet. To use this
> command, you must first unprotect the sheet (Review tab, Changes
> group, Unprotect Sheet button). You may be prompted for a password.
> (Microsoft Excel)
欢迎任何建议。我可以手动取消对单元格的保护,但我必须每天对数百个文件执行此操作。
我的最终目标是将 100 多个电子表格中的数据导入到 R 中进行分析。我不需要导出回 Excel。我也不需要将 protected 单元格导入 R,所以如果有一种方法可以跳过它们,那就行得通了。
编辑:出现了与此操作相关的新问题。当我尝试对共享工作簿进行提取时,我在 R 中遇到错误:
80020009 Error: Exception occurred.
如果我手动进入 Excel 并取消共享工作簿(在审阅->共享工作簿->取消选中允许多个用户进行的更改)。有没有办法通过 excel.link 以编程方式执行此操作?
最佳答案
试试下面的代码:
library(excel.link)
filename = "shared.xlsx"
xl.workbook.open(filename, password = "test")
# here we resave workbook to the temporary folder with exclusive access
new_path = paste0(tempdir(), "\\", filename)
xl()[["Activeworkbook"]]$saveas(new_path, AccessMode=xl.constants$xlExclusive)
###
xl()[["Activesheet"]]$Unprotect(password = "test")
data = crc[a1]
xl.workbook.close()
unlink(new_path) # remove temporary Excel File
更新 2018.07.16 添加以独占访问权限保存工作簿的代码。
关于使用锁定的单元格将 Excel 文件读入 R,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51270592/