Learn R Programming

openxlsx2 (version 0.7)

wb_protect: 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

wb_protect(
  wb,
  protect = TRUE,
  password = NULL,
  lockStructure = FALSE,
  lockWindows = FALSE,
  type = 1,
  fileSharing = FALSE,
  username = unname(Sys.info()["user"]),
  readOnlyRecommended = FALSE
)

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 (see details)

fileSharing

Whether to enable a popup requesting the unlock password is prompted

username

The username for the fileSharing popup

readOnlyRecommended

Whether or not a post unlock message appears stating that the workbook is recommended to be opened in readonly mode.

Details

Lock types:

1

xlsx with password (default)

2

xlsx recommends read-only

4

xlsx enforces read-only

8

xlsx is locked for annotation

Examples

Run this code
wb <- wb_workbook()
wb$add_worksheet("S1")
wb_protect(wb, protect = TRUE, password = "Password", lockStructure = TRUE)

# Remove the protection
wb_protect(wb, protect = FALSE)

wb <- wb_protect(
  wb,
  protect = TRUE,
  password = "Password",
  lockStructure = TRUE,
  type = 2L,
  fileSharing = TRUE,
  username = "Test",
  readOnlyRecommended = TRUE
)

Run the code above in your browser using DataLab