Skip to content

Commit

Permalink
fix: docx_summary behavior with unsaved Word documents
Browse files Browse the repository at this point in the history
fix #536
  • Loading branch information
davidgohel committed Feb 3, 2024
1 parent a9e33ea commit 30a1c3f
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: officer
Type: Package
Title: Manipulation of Microsoft Word and PowerPoint Documents
Version: 0.6.4.004
Version: 0.6.4.005
Authors@R: c(
person("David", "Gohel", role = c("aut", "cre"), email = "david.gohel@ardata.fr"),
person("Stefan", "Moog", role = "aut", email = 'moogs@gmx.de'),
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ importFrom(xml2,xml_child)
importFrom(xml2,xml_children)
importFrom(xml2,xml_find_all)
importFrom(xml2,xml_find_first)
importFrom(xml2,xml_has_attr)
importFrom(xml2,xml_length)
importFrom(xml2,xml_missing)
importFrom(xml2,xml_name)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ The docs gain a description of the columns of the returned dataframe.
- Fix. Refactor `docx_comments()` to (properly) account for comments spanning
multiple paragraphs, multiple comments in the same paragraph and replies. Closes #541.
- Check and stop if `fp_text_lite()` is using a *shading.color* but no *color*.
- Check used style in `body_add_caption()`.
- Use fake attribute `stlname` if available in `docx_summary()`.

# officer 0.6.3

Expand Down
8 changes: 8 additions & 0 deletions R/docx_add.R
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,9 @@ body_add_plot <- function(x, value, width = 6, height = 5, res = 300, style = "N
#' print(doc, target = tempfile(fileext = ".docx"))
body_add_caption <- function(x, value, pos = "after") {
stopifnot(inherits(value, "block_caption"))
if (!value$style %in% x$styles$style_name) {
stop("caption is using style ", shQuote(value$style), " that does not exist in the Word document.")
}
out <- to_wml(value, add_ns = TRUE, base_document = x)
body_add_xml(x = x, str = out, pos = pos)
}
Expand Down Expand Up @@ -697,6 +700,11 @@ body_add.data.frame <- function(x, value, style = NULL, header = TRUE,
#' @describeIn body_add add a [block_caption] object. These objects enable
#' the creation of set of formatted paragraphs made of formatted chunks of text.
body_add.block_caption <- function(x, value, ...) {

if (!value$style %in% x$styles$style_name) {
stop("caption is using style ", shQuote(value$style), " that does not exist in the Word document.")
}

xml_elt <- to_wml(value, add_ns = TRUE, base_document = x)
body_add_xml2(x = x, str = xml_elt)
}
Expand Down
3 changes: 3 additions & 0 deletions R/fortify_docx.R
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,14 @@ docxtable_as_tibble <- function( node, styles, preserve = FALSE ){
row_details
}

#' @importFrom xml2 xml_has_attr
par_as_tibble <- function(node, styles){

style_node <- xml_child(node, "w:pPr/w:pStyle")
if( inherits(style_node, "xml_missing") ){
style_name <- NA
} else if(xml_has_attr(style_node, "stlname")) {
style_name <- xml_attr( style_node, "stlname")
} else {
style_id <- xml_attr( style_node, "val")
style_name <- styles$style_name[styles$style_id %in% style_id]
Expand Down

0 comments on commit 30a1c3f

Please sign in to comment.