Skip to content

Commit a6d4064

Browse files
committed
fix: with body_add_docx(), basename can not contain ' '
fix #572
1 parent 626f18f commit a6d4064

File tree

5 files changed

+26
-7
lines changed

5 files changed

+26
-7
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Type: Package
22
Package: officer
33
Title: Manipulation of Microsoft Word and PowerPoint Documents
4-
Version: 0.6.7.004
4+
Version: 0.6.7.005
55
Authors@R: c(
66
person("David", "Gohel", , "[email protected]", role = c("aut", "cre")),
77
person("Stefan", "Moog", , "[email protected]", role = "aut"),

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
- store paddings as numeric values and not integer values.
66
- remove_fields in `docx_summary()` now also removes "w:fldData" nodes.
7+
- complete the manual of `body_add_docx()` with a note about the file basename
8+
that can not contain ' ' and trigger an error if it contains a ' '.
79

810
## Features
911

R/docx_add.R

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,20 @@ body_add_img <- function(x, src, style = NULL, width, height, pos = "after") {
6464
#' @export
6565
#' @title Add an external docx in a 'Word' document
6666
#' @description Add content of a docx into an rdocx object.
67-
#' @note
67+
#'
6868
#' The function is using a 'Microsoft Word' feature: when the
6969
#' document will be edited, the content of the file will be
7070
#' inserted in the main document.
7171
#'
7272
#' This feature is unlikely to work as expected if the
7373
#' resulting document is edited by another software.
74+
#'
75+
#' The file is added when the method `print()` that
76+
#' produces the final Word file is called, so don't remove
77+
#' file defined with `src` before.
7478
#' @inheritParams body_add_break
75-
#' @param src docx filename
79+
#' @param src docx filename, the path of the file must not contain
80+
#' any '&' and the basename must not contain any space.
7681
#' @examples
7782
#' file1 <- tempfile(fileext = ".docx")
7883
#' file2 <- tempfile(fileext = ".docx")
@@ -92,6 +97,11 @@ body_add_img <- function(x, src, style = NULL, width, height, pos = "after") {
9297
#' @export
9398
#' @family functions for adding content
9499
body_add_docx <- function(x, src, pos = "after") {
100+
101+
if(grepl(" ", basename(src))){
102+
stop("The basename of the file to be inserted cannot contain spaces.")
103+
}
104+
95105
xml_elt <- to_wml(block_pour_docx(file = src), add_ns = TRUE)
96106
body_add_xml(x = x, str = xml_elt, pos = pos)
97107
}

R/ooxml_block_objects.R

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,10 @@ block_pour_docx <- function(file){
224224
stop("file {", file, "} is not a docx file.", call. = FALSE)
225225
}
226226
if(grepl("&", file, ignore.case = TRUE)){
227-
stop("file path {", file, "} contains '&', please rename your file to use it with `block_pour_docx()`.", call. = FALSE)
227+
stop("file path {", file, "} contains '&', please rename your file.", call. = FALSE)
228+
}
229+
if(grepl(" ", basename(file), ignore.case = TRUE)){
230+
stop("file path {", basename(file), "} contains ' ', please rename your file.", call. = FALSE)
228231
}
229232

230233
z <- list(file = file)

man/body_add_docx.Rd

Lines changed: 7 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)