From e1951b7430d4fa91b264eba7a276814d7fd3f11a Mon Sep 17 00:00:00 2001 From: Jan Marvin Garbuszus Date: Mon, 21 Jun 2021 22:32:44 +0200 Subject: [PATCH 1/2] add DocSecurity type to protectWorkbook --- R/WorkbookClass.R | 10 ++++++++-- R/wrappers.R | 5 +++-- man/protectWorkbook.Rd | 5 ++++- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/R/WorkbookClass.R b/R/WorkbookClass.R index 5e480126..2d5d2ce1 100644 --- a/R/WorkbookClass.R +++ b/R/WorkbookClass.R @@ -828,11 +828,14 @@ Workbook$methods( fl = file.path(relsDir, ".rels") ) + app <- "Microsoft Excel" + # further protect argument (might be extended with: , , , , , , ) + if (!is.null(workbook$apps)) app <- paste0(app, workbook$apps) ## write app.xml write_file( head = '', - body = "Microsoft Excel", + body = app, tail = "", fl = file.path(docPropsDir, "app.xml") ) @@ -4223,7 +4226,8 @@ Workbook$methods( protectWorkbook = function(protect = TRUE, lockStructure = FALSE, lockWindows = FALSE, - password = NULL) { + password = NULL, + type = NULL) { attr <- c() if (!is.null(password)) { attr["workbookPassword"] <- hashPassword(password) @@ -4248,6 +4252,8 @@ Workbook$methods( sep = "" ) ) + if (!is.null(type) | !is.null(password)) + workbook$apps <<- sprintf("%i", type) } else { workbook$workbookProtection <<- "" } diff --git a/R/wrappers.R b/R/wrappers.R index 732b970c..cccf7beb 100644 --- a/R/wrappers.R +++ b/R/wrappers.R @@ -2437,6 +2437,7 @@ protectWorksheet <- function(wb, sheet, protect = TRUE, password = NULL, #' @param password (optional) password required to unprotect the workbook #' @param lockStructure Whether the workbook structure should be locked #' @param lockWindows Whether the window position of the spreadsheet should be locked +#' @param 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. #' @export #' @examples #' wb <- createWorkbook() @@ -2450,12 +2451,12 @@ protectWorksheet <- function(wb, sheet, protect = TRUE, password = NULL, #' \dontrun{ #' saveWorkbook(wb, "WorkBook_Protection_unprotected.xlsx", overwrite = TRUE) #' } -protectWorkbook <- function(wb, protect = TRUE, password = NULL, lockStructure = FALSE, lockWindows = FALSE) { +protectWorkbook <- function(wb, protect = TRUE, password = NULL, lockStructure = FALSE, lockWindows = FALSE, type = 1L) { if (!"Workbook" %in% class(wb)) { stop("First argument must be a Workbook.") } - invisible(wb$protectWorkbook(protect = protect, password = password, lockStructure = lockStructure, lockWindows = lockWindows)) + invisible(wb$protectWorkbook(protect = protect, password = password, lockStructure = lockStructure, lockWindows = lockWindows, type = type)) } diff --git a/man/protectWorkbook.Rd b/man/protectWorkbook.Rd index 58fcf87a..dba76117 100644 --- a/man/protectWorkbook.Rd +++ b/man/protectWorkbook.Rd @@ -9,7 +9,8 @@ protectWorkbook( protect = TRUE, password = NULL, lockStructure = FALSE, - lockWindows = FALSE + lockWindows = FALSE, + type = 1L ) } \arguments{ @@ -22,6 +23,8 @@ protectWorkbook( \item{lockStructure}{Whether the workbook structure should be locked} \item{lockWindows}{Whether the window position of the spreadsheet should be locked} + +\item{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.} } \description{ Protect or unprotect a workbook from modifications by the user in the graphical user interface. Replaces an existing protection. From 60b962c9afd667d2ee39cd27d42de9823c6c0cdc Mon Sep 17 00:00:00 2001 From: Jan Marvin Garbuszus Date: Tue, 22 Jun 2021 00:11:18 +0200 Subject: [PATCH 2/2] minor hack to make this test work (apps was added with protectWorkbook) --- tests/testthat/test-protect-workbook.R | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/testthat/test-protect-workbook.R b/tests/testthat/test-protect-workbook.R index b204cf6c..7699bbc1 100644 --- a/tests/testthat/test-protect-workbook.R +++ b/tests/testthat/test-protect-workbook.R @@ -24,8 +24,10 @@ test_that("Reading protected Workbook", { saveWorkbook(wb, tmp_file, overwrite = TRUE) wb2 <- loadWorkbook(file = tmp_file) - # Check that the order of teh sub-elements is preserved - expect_equal(names(wb2$workbook), names(wb$workbook)) + # Check that the order of the sub-elements is preserved + n1 <- names(wb2$workbook) + n2 <- names(wb$workbook)[names(wb$workbook) != "apps"] + expect_equal(n1, n2) unlink(tmp_file, recursive = TRUE, force = TRUE) })