Skip to content

Commit

Permalink
[protect] allow logical operators in protect worksheet (#662)
Browse files Browse the repository at this point in the history
  • Loading branch information
JanMarvin authored Jun 23, 2023
1 parent ecd682d commit 8d6b3ed
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 9 deletions.
12 changes: 10 additions & 2 deletions R/class-workbook-wrappers.R
Original file line number Diff line number Diff line change
Expand Up @@ -1359,12 +1359,20 @@ wb_page_setup <- function(
#' wb <- wb_workbook()
#' wb$add_worksheet("S1")
#' wb$add_data_table(1, x = iris[1:30, ])
#' # Formatting cells / columns is allowed , but inserting / deleting columns is protected:
#' wb$protect_worksheet("S1",
#'
#' wb$protect_worksheet(
#' "S1",
#' protect = TRUE,
#' properties = c("formatCells", "formatColumns", "insertColumns", "deleteColumns")
#' )
#'
#' # Formatting cells / columns is allowed , but inserting / deleting columns is protected:
#' wb$protect_worksheet(
#' "S1",
#' protect = TRUE,
#' properties = c(formatCells = FALSE, formatColumns = FALSE, insertColumns = TRUE, deleteColumns = TRUE)
#' )
#'
#' # Remove the protection
#' wb$protect_worksheet("S1", protect = FALSE)
wb_protect_worksheet <- function(
Expand Down
18 changes: 13 additions & 5 deletions R/class-workbook.R
Original file line number Diff line number Diff line change
Expand Up @@ -4809,20 +4809,28 @@ wbWorkbook <- R6::R6Class(

if (!is.null(properties)) {
# ensure only valid properties are listed
properties <- match.arg(properties, all_props, several.ok = TRUE)
if (is.null(names(properties))) {
properties <- match.arg(properties, all_props, several.ok = TRUE)
properties <- as_xml_attr(all_props %in% properties)
names(properties) <- all_props
properties <- properties[properties != "0"]
} else {
keep <- match.arg(names(properties), all_props, several.ok = TRUE)
properties <- properties[keep]
nms <- names(properties)
properties <- as_xml_attr(properties)
names(properties) <- nms
}
}

properties <- as.character(as.numeric(all_props %in% properties))
names(properties) <- all_props

if (!is.null(password))
properties <- c(properties, password = hashPassword(password))

self$worksheets[[sheet]]$sheetProtection <- xml_node_create(
"sheetProtection",
xml_attributes = c(
sheet = "1",
properties[properties != "0"]
properties
)
)

Expand Down
12 changes: 10 additions & 2 deletions man/wb_protect_worksheet.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions tests/testthat/test-protect-worksheet.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,31 @@ test_that("Protection", {
wb <- wb_protect_worksheet(wb, sheet = "s2", protect = FALSE)
expect_equal(wb$worksheets[[2]]$sheetProtection, character())
})

test_that("logical vector works too", {

wb <- wb_workbook()$add_worksheet("S1")

# pre 0.7.1 style
wb$protect_worksheet(
"S1",
protect = TRUE,
properties = c("formatCells", "formatColumns", "insertColumns", "deleteColumns")
)

exp <- "<sheetProtection sheet=\"1\" formatCells=\"1\" formatColumns=\"1\" insertColumns=\"1\" deleteColumns=\"1\"/>"
got <- wb$worksheets[[1]]$sheetProtection
expect_equal(exp, got)

# post 0.7 style
wb$protect_worksheet(
"S1",
protect = TRUE,
properties = c(formatCells = FALSE, formatColumns = FALSE, insertColumns = TRUE, deleteColumns = TRUE)
)

exp <- "<sheetProtection sheet=\"1\" formatCells=\"0\" formatColumns=\"0\" insertColumns=\"1\" deleteColumns=\"1\"/>"
got <- wb$worksheets[[1]]$sheetProtection
expect_equal(exp, got)

})

0 comments on commit 8d6b3ed

Please sign in to comment.