From a6d4064e561ebd64c920ddff66cd249947bdb6ee Mon Sep 17 00:00:00 2001 From: David Gohel Date: Tue, 27 Aug 2024 17:05:03 +0200 Subject: [PATCH] fix: with `body_add_docx()`, basename can not contain ' ' fix #572 --- DESCRIPTION | 2 +- NEWS.md | 2 ++ R/docx_add.R | 14 ++++++++++++-- R/ooxml_block_objects.R | 5 ++++- man/body_add_docx.Rd | 10 +++++++--- 5 files changed, 26 insertions(+), 7 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 80aad386..d9aca146 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: officer Title: Manipulation of Microsoft Word and PowerPoint Documents -Version: 0.6.7.004 +Version: 0.6.7.005 Authors@R: c( person("David", "Gohel", , "david.gohel@ardata.fr", role = c("aut", "cre")), person("Stefan", "Moog", , "moogs@gmx.de", role = "aut"), diff --git a/NEWS.md b/NEWS.md index b282dd4d..63e68e0d 100644 --- a/NEWS.md +++ b/NEWS.md @@ -4,6 +4,8 @@ - store paddings as numeric values and not integer values. - remove_fields in `docx_summary()` now also removes "w:fldData" nodes. +- complete the manual of `body_add_docx()` with a note about the file basename +that can not contain ' ' and trigger an error if it contains a ' '. ## Features diff --git a/R/docx_add.R b/R/docx_add.R index 0b041dd4..6e0a919e 100644 --- a/R/docx_add.R +++ b/R/docx_add.R @@ -64,15 +64,20 @@ body_add_img <- function(x, src, style = NULL, width, height, pos = "after") { #' @export #' @title Add an external docx in a 'Word' document #' @description Add content of a docx into an rdocx object. -#' @note +#' #' The function is using a 'Microsoft Word' feature: when the #' document will be edited, the content of the file will be #' inserted in the main document. #' #' This feature is unlikely to work as expected if the #' resulting document is edited by another software. +#' +#' The file is added when the method `print()` that +#' produces the final Word file is called, so don't remove +#' file defined with `src` before. #' @inheritParams body_add_break -#' @param src docx filename +#' @param src docx filename, the path of the file must not contain +#' any '&' and the basename must not contain any space. #' @examples #' file1 <- tempfile(fileext = ".docx") #' file2 <- tempfile(fileext = ".docx") @@ -92,6 +97,11 @@ body_add_img <- function(x, src, style = NULL, width, height, pos = "after") { #' @export #' @family functions for adding content body_add_docx <- function(x, src, pos = "after") { + + if(grepl(" ", basename(src))){ + stop("The basename of the file to be inserted cannot contain spaces.") + } + xml_elt <- to_wml(block_pour_docx(file = src), add_ns = TRUE) body_add_xml(x = x, str = xml_elt, pos = pos) } diff --git a/R/ooxml_block_objects.R b/R/ooxml_block_objects.R index 3ee9c4f4..8b09bcf0 100644 --- a/R/ooxml_block_objects.R +++ b/R/ooxml_block_objects.R @@ -224,7 +224,10 @@ block_pour_docx <- function(file){ stop("file {", file, "} is not a docx file.", call. = FALSE) } if(grepl("&", file, ignore.case = TRUE)){ - stop("file path {", file, "} contains '&', please rename your file to use it with `block_pour_docx()`.", call. = FALSE) + stop("file path {", file, "} contains '&', please rename your file.", call. = FALSE) + } + if(grepl(" ", basename(file), ignore.case = TRUE)){ + stop("file path {", basename(file), "} contains ' ', please rename your file.", call. = FALSE) } z <- list(file = file) diff --git a/man/body_add_docx.Rd b/man/body_add_docx.Rd index 40acfcea..edaa3ee1 100644 --- a/man/body_add_docx.Rd +++ b/man/body_add_docx.Rd @@ -9,21 +9,25 @@ body_add_docx(x, src, pos = "after") \arguments{ \item{x}{an rdocx object} -\item{src}{docx filename} +\item{src}{docx filename, the path of the file must not contain +any '&' and the basename must not contain any space.} \item{pos}{where to add the new element relative to the cursor, one of "after", "before", "on".} } \description{ Add content of a docx into an rdocx object. -} -\note{ + The function is using a 'Microsoft Word' feature: when the document will be edited, the content of the file will be inserted in the main document. This feature is unlikely to work as expected if the resulting document is edited by another software. + +The file is added when the method \code{print()} that +produces the final Word file is called, so don't remove +file defined with \code{src} before. } \examples{ file1 <- tempfile(fileext = ".docx")