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. 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) })