Learn R Programming

openxlsx (version 4.2.3)

protectWorksheet: Protect a worksheet from modifications

Description

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

Usage

protectWorksheet(
  wb,
  sheet,
  protect = TRUE,
  password = NULL,
  lockSelectingLockedCells = NULL,
  lockSelectingUnlockedCells = NULL,
  lockFormattingCells = NULL,
  lockFormattingColumns = NULL,
  lockFormattingRows = NULL,
  lockInsertingColumns = NULL,
  lockInsertingRows = NULL,
  lockInsertingHyperlinks = NULL,
  lockDeletingColumns = NULL,
  lockDeletingRows = NULL,
  lockSorting = NULL,
  lockAutoFilter = NULL,
  lockPivotTables = NULL,
  lockObjects = NULL,
  lockScenarios = NULL
)

Arguments

wb

A workbook object

sheet

A name or index of a worksheet

protect

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

password

(optional) password required to unprotect the worksheet

lockSelectingLockedCells

Whether selecting locked cells is locked

lockSelectingUnlockedCells

Whether selecting unlocked cells is locked

lockFormattingCells

Whether formatting cells is locked

lockFormattingColumns

Whether formatting columns is locked

lockFormattingRows

Whether formatting rows is locked

lockInsertingColumns

Whether inserting columns is locked

lockInsertingRows

Whether inserting rows is locked

lockInsertingHyperlinks

Whether inserting hyperlinks is locked

lockDeletingColumns

Whether deleting columns is locked

lockDeletingRows

Whether deleting rows is locked

lockSorting

Whether sorting is locked

lockAutoFilter

Whether auto-filter is locked

lockPivotTables

Whether pivot tables are locked

lockObjects

Whether objects are locked

lockScenarios

Whether scenarios are locked

Examples

Run this code
# NOT RUN {
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
)

# Remove the protection
protectWorksheet(wb, "S1", protect = FALSE)
# }
# NOT RUN {
saveWorkbook(wb, "pageSetupExample.xlsx", overwrite = TRUE)
# }

Run the code above in your browser using DataLab