Skip to content

Commit

Permalink
Make set_property() not change create date automatically, add modify …
Browse files Browse the repository at this point in the history
…date (#1176)

by: Moritz Lell <[email protected]>
  • Loading branch information
mlell authored Nov 15, 2024
1 parent 51cad78 commit f8943ca
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 62 deletions.
8 changes: 8 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# openxlsx2 (development version)

## New features

* `wb_set_properties()` now has a `datetime_modify` option. [1176](https://github.com/JanMarvin/openxlsx2/pull/1176)

## Fixes

* Create date is not reset to the present time in each call to `wb_set_properties()` [1176](https://github.com/JanMarvin/openxlsx2/pull/1176)


***************************************************************************

Expand Down
48 changes: 26 additions & 22 deletions R/class-workbook-wrappers.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#' @param creator Creator of the workbook (your name). Defaults to login username or `options("openxlsx2.creator")` if set.
#' @param title,subject,category,keywords,comments,manager,company Workbook property, a string.
#' @param datetime_created The time of the workbook is created
#' @param datetime_modified The time of the workbook was last modified
#' @param theme Optional theme identified by string or number.
#' See **Details** for options.
#' @param ... additional arguments
Expand All @@ -34,30 +35,32 @@
#' category = "sales"
#' )
wb_workbook <- function(
creator = NULL,
title = NULL,
subject = NULL,
category = NULL,
datetime_created = Sys.time(),
theme = NULL,
keywords = NULL,
comments = NULL,
manager = NULL,
company = NULL,
creator = NULL,
title = NULL,
subject = NULL,
category = NULL,
datetime_created = Sys.time(),
datetime_modified = NULL,
theme = NULL,
keywords = NULL,
comments = NULL,
manager = NULL,
company = NULL,
...
) {
wbWorkbook$new(
creator = creator,
title = title,
subject = subject,
category = category,
datetime_created = datetime_created,
theme = theme,
keywords = keywords,
comments = comments,
manager = manager,
company = company,
... = ...
creator = creator,
title = title,
subject = subject,
category = category,
datetime_created = datetime_created,
datetime_modified = datetime_modified,
theme = theme,
keywords = keywords,
comments = comments,
manager = manager,
company = company,
... = ...
)
}

Expand Down Expand Up @@ -2767,14 +2770,15 @@ wb_get_properties <- function(wb) {

#' @rdname properties-wb
#' @export
wb_set_properties <- function(wb, creator = NULL, title = NULL, subject = NULL, category = NULL, datetime_created = Sys.time(), modifier = NULL, keywords = NULL, comments = NULL, manager = NULL, company = NULL, custom = NULL) {
wb_set_properties <- function(wb, creator = NULL, title = NULL, subject = NULL, category = NULL, datetime_created = NULL, datetime_modified = NULL, modifier = NULL, keywords = NULL, comments = NULL, manager = NULL, company = NULL, custom = NULL) {
assert_workbook(wb)
wb$clone()$set_properties(
creator = creator,
title = title,
subject = subject,
category = category,
datetime_created = datetime_created,
datetime_modified = datetime_modified,
modifier = modifier,
keywords = keywords,
comments = comments,
Expand Down
98 changes: 61 additions & 37 deletions R/class-workbook.R
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ worksheet_lock_properties <- function() {
#' @param datetime_created The datetime (as `POSIXt`) the workbook is
#' created. Defaults to the current `Sys.time()` when the workbook object
#' is created, not when the Excel files are saved.
#' @param datetime_modified The datetime (as `POSIXt`) that should be recorded
#' as last modification date. Defaults to the creation date.
#' @param ... additional arguments
#' @export
wbWorkbook <- R6::R6Class(
Expand Down Expand Up @@ -302,16 +304,17 @@ wbWorkbook <- R6::R6Class(
#' @param ... additional arguments
#' @return a `wbWorkbook` object
initialize = function(
creator = NULL,
title = NULL,
subject = NULL,
category = NULL,
datetime_created = Sys.time(),
theme = NULL,
keywords = NULL,
comments = NULL,
manager = NULL,
company = NULL,
creator = NULL,
title = NULL,
subject = NULL,
category = NULL,
datetime_created = Sys.time(),
datetime_modified = NULL,
theme = NULL,
keywords = NULL,
comments = NULL,
manager = NULL,
company = NULL,
...
) {

Expand All @@ -331,6 +334,7 @@ wbWorkbook <- R6::R6Class(
default = Sys.getenv("USERNAME", unset = Sys.getenv("USER")))
# USERNAME is present for (Windows, Linux) "USER" is present for Mac

# Internal option to alleviate timing problems in CI and CRAN
datetime_created <- getOption("openxlsx2.datetimeCreated", datetime_created)


Expand All @@ -344,6 +348,11 @@ wbWorkbook <- R6::R6Class(
assert_class(company, "character", or_null = TRUE)

assert_class(datetime_created, "POSIXt")
assert_class(datetime_modified, "POSIXt", or_null = TRUE)

# Avoid modtime being slightly different from createtime by two distinct
# Sys.time() calls
if (is.null(datetime_modified)) datetime_modified <- datetime_created

stopifnot(
length(title) <= 1L,
Expand All @@ -352,15 +361,16 @@ wbWorkbook <- R6::R6Class(
)

self$set_properties(
creator = creator,
title = title,
subject = subject,
category = category,
datetime_created = datetime_created,
keywords = keywords,
comments = comments,
manager = manager,
company = company
creator = creator,
title = title,
subject = subject,
category = category,
datetime_created = datetime_created,
datetime_modified = datetime_modified,
keywords = keywords,
comments = comments,
manager = manager,
company = company
)
self$comments <- list()
self$threadComments <- list()
Expand Down Expand Up @@ -5514,6 +5524,7 @@ wbWorkbook <- R6::R6Class(
done <- as_xml_attr(resolve)
if (reply) done <- NULL

# Internal option to alleviate timing problems in CI and CRAN
ts <- getOption("openxlsx2.datetimeCreated", default = Sys.time())

tc <- xml_node_create(
Expand Down Expand Up @@ -6890,21 +6901,23 @@ wbWorkbook <- R6::R6Class(
},

#' @description Set a property of a workbook
#' @param title,subject,category,datetime_created,modifier,keywords,comments,manager,company,custom A workbook property to set
#' @param title,subject,category,datetime_created,datetime_modified,modifier,keywords,comments,manager,company,custom A workbook property to set
set_properties = function(
creator = NULL,
title = NULL,
subject = NULL,
category = NULL,
datetime_created = Sys.time(),
modifier = NULL,
keywords = NULL,
comments = NULL,
manager = NULL,
company = NULL,
custom = NULL
creator = NULL,
title = NULL,
subject = NULL,
category = NULL,
datetime_created = NULL,
datetime_modified = NULL,
modifier = NULL,
keywords = NULL,
comments = NULL,
manager = NULL,
company = NULL,
custom = NULL
) {

# Internal option to alleviate timing problems in CI and CRAN
datetime_created <-
getOption("openxlsx2.datetimeCreated", datetime_created)

Expand Down Expand Up @@ -6970,12 +6983,23 @@ wbWorkbook <- R6::R6Class(
self$app$Company <- xml_node_create("Company", xml_children = company)
}

xml_properties[core_created] <- xml_node_create(core_created,
xml_attributes = c(
`xsi:type` = "dcterms:W3CDTF"
),
xml_children = format(as_POSIXct_utc(datetime_created), "%Y-%m-%dT%H:%M:%SZ")
)
if (!is.null(datetime_created)) {
xml_properties[core_created] <- xml_node_create(core_created,
xml_attributes = c(
`xsi:type` = "dcterms:W3CDTF"
),
xml_children = format(as_POSIXct_utc(datetime_created), "%Y-%m-%dT%H:%M:%SZ")
)
}

if (!is.null(datetime_modified)) {
xml_properties[core_modifid] <- xml_node_create(core_modifid,
xml_attributes = c(
`xsi:type` = "dcterms:W3CDTF"
),
xml_children = format(as_POSIXct_utc(datetime_modified), "%Y-%m-%dT%H:%M:%SZ")
)
}

if (!is.null(modifier)) {
xml_properties[core_lastmod] <- xml_node_create(core_lastmod, xml_children = modifier)
Expand Down
1 change: 1 addition & 0 deletions inst/AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Luca Braglia
Luke Symes
Martins Liberts
Mikael Elenius
Moritz Lell
Noam Ross
olivroy
Philipp Schauberger
Expand Down
5 changes: 4 additions & 1 deletion man/properties-wb.Rd

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

9 changes: 7 additions & 2 deletions man/wbWorkbook.Rd

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

3 changes: 3 additions & 0 deletions man/wb_workbook.Rd

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

10 changes: 10 additions & 0 deletions tests/testthat/test-loading_workbook.R
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,16 @@ test_that("Loading a workbook with property preserves it.", {

})

test_that("Creation time is not changed by set_property unless specified", {
op <- options("openxlsx2.datetimeCreated" = NULL)
on.exit(options(op), add = TRUE)
t1 <- "2000-01-01T00:00:00Z"
wb <- wb_workbook(datetime_created = as.POSIXct(t1, tz = "UTC"))
wb$set_properties()
t2 <- wb$get_properties()[["datetime_created"]]
expect_equal(t1, t2)
})

test_that("failing to unzip works as expected", {

# try to read from single file
Expand Down

0 comments on commit f8943ca

Please sign in to comment.