Openxlsx 保护 sheet 但允许输入值

Openxlsx protect sheet but allow entering of values

我正在使用 excel 模板从不同的人那里收集数据。为了尽量减少人们更改模板结构的机会,我想保护工作表,但仍然允许他们填写我想要的数据值。

我很高兴我找到了 openxlsx 包的分叉版本,其中包含一个函数 protectWorksheet 来执行此操作,Link。但是当我使用该功能时,我无法调整功能以便仍然可以填充值。

除了 help file 示例中的函数外,我真的没有 MWE。如何调整示例以仍然允许填写值?可能吗?

wb <- createWorkbook()
addWorksheet(wb, "S1")
writeDataTable(wb, 1, x = iris[1:30,])
# Formatting cells / columns is allowed , but inserting / deleting columns is protected:
protectWorksheet(wb, "S1", protect = TRUE, lockFormattingCells = FALSE, lockFormattingColumns = FALSE, lockInsertingColumns = TRUE, lockDeletingColumns = TRUE)

saveWorkbook(wb, "pageSetupExample.xlsx", overwrite = TRUE)

感谢@Reinhold Kainhofer,您可以使用 createStyle 来控制它。

从 github 下载 openxlsx 包:https://github.com/kainhofer/openxlsx

wb <- createWorkbook()
addWorksheet(wb, "S1")
writeDataTable(wb, 1, x = iris[1:30,])
# Formatting cells / columns is allowed , but inserting / deleting columns is protected:
protectWorksheet(wb, "S1", protect = TRUE, lockFormattingCells = FALSE, lockFormattingColumns = FALSE, lockInsertingColumns = TRUE, lockDeletingColumns = TRUE)

#This line allows specified cells to be unlocked so that users can enter values.
addStyle(wb, "S1", style = createStyle(locked = FALSE), rows = 1:10, cols = 1)

saveWorkbook(wb, "pageSetupExample.xlsx", overwrite = TRUE)