Learn R Programming

openxlsx (version 4.2.7.1)

protectWorkbook: Protect a workbook from modifications

Description

Protect or unprotect a workbook from modifications by the user in the graphical user interface. Replaces an existing protection.

Usage

protectWorkbook(
  wb,
  protect = TRUE,
  password = NULL,
  lockStructure = FALSE,
  lockWindows = FALSE,
  type = 1L
)

Arguments

wb

A workbook object

protect

Whether to protect or unprotect the sheet (default=TRUE)

password

(optional) password required to unprotect the workbook

lockStructure

Whether the workbook structure should be locked

lockWindows

Whether the window position of the spreadsheet should be locked

type

Lock type, default 1. From the xml documentation: 1 - Document is password protected. 2 - Document is recommended to be opened as read-only. 4 - Document is enforced to be opened as read-only. 8 - Document is locked for annotation.

Author

Reinhold Kainhofer

Examples

Run this code
wb <- createWorkbook()
addWorksheet(wb, "S1")
protectWorkbook(wb, protect = TRUE, password = "Password", lockStructure = TRUE)
if (FALSE) {
saveWorkbook(wb, "WorkBook_Protection.xlsx", overwrite = TRUE)
}
# Remove the protection
protectWorkbook(wb, protect = FALSE)
if (FALSE) {
saveWorkbook(wb, "WorkBook_Protection_unprotected.xlsx", overwrite = TRUE)
}

Run the code above in your browser using DataLab