Openxlsx保护工作表,但允许输入值

时间:2019-05-09 22:15:11

标签: r excel openxlsx

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

我很高兴能找到openxlsx软件包的派生版本,其中包含一个函数protectWorksheetLink。但是,当我使用该函数时,我无法调整该函数,以便仍可以填充值。

除了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)

1 个答案:

答案 0 :(得分:1)

由于@Reinhold Kainhofer,您可以使用createStyle来控制它。

从github下载https://github.com/kainhofer/openxlsxopenxlsx软件包。

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)