Skip to content

Commit

Permalink
fix: ph_hyperlink() is fixed when several hyperlinks are inside one…
Browse files Browse the repository at this point in the history
… slide

- change get_shape_id
- refactored `ph_remove()`
- drop visual tests that fail because of testthat/doconv

fix #489
  • Loading branch information
davidgohel committed Oct 22, 2023
1 parent efc8a4e commit 713756f
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 34 deletions.
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.3.003
Version: 0.6.3.004
Authors@R: c(
person("David", "Gohel", role = c("aut", "cre"),
email = "[email protected]"),
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

- Internal function `is_doc_open()` replaces `is_office_doc_edited()` to check if a document is open on Windows. This solves the issue where RStudio crashed while trying to write to an open Office document.
- Add option `preserve` to preserve cell line breaks to `docx_summary()`.
- fix `ph_hyperlink()` when several hyperlinks are inside one slide and refactored `ph_remove()`.

# officer 0.6.2

Expand Down
17 changes: 6 additions & 11 deletions R/ppt_ph_manipulate.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ get_shape_id <- function(x, type = NULL, id = NULL, ph_label = NULL ){
sel <- sel[id]
if( sum(is.finite(sel)) != 1 ) stop("no shape with label ", shQuote(ph_label), "and with id ", id, " has been found")
}
slsmry$id[sel]
sel
}


Expand Down Expand Up @@ -63,7 +63,7 @@ ph_remove <- function( x, type = "body", id = 1, ph_label = NULL, id_chr = NULL

slide <- x$slide$get_slide(x$cursor)
office_id <- get_shape_id(x, type = type, id = id, ph_label = ph_label )
current_elt <- xml_find_first(slide$get(), sprintf("p:cSld/p:spTree/*[*/p:cNvPr[@id='%s']]", office_id) )
current_elt <- xml_find_first(slide$get(), sprintf("p:cSld/p:spTree/*[p:nvSpPr/p:cNvPr][%.0f]", office_id) )

xml_remove(current_elt)

Expand Down Expand Up @@ -96,7 +96,7 @@ ph_slidelink <- function( x, type = "body", id = 1, id_chr = NULL, ph_label = NU

slide <- x$slide$get_slide(x$cursor)
office_id <- get_shape_id(x, type = type, id = id, ph_label = ph_label )
current_elt <- xml_find_first(slide$get(), sprintf("p:cSld/p:spTree/*[p:nvSpPr/p:cNvPr[@id='%s']]", office_id) )
current_elt <- xml_find_first(slide$get(), sprintf("p:cSld/p:spTree/*[p:nvSpPr/p:cNvPr][%.0f]", office_id) )

# declare slide ref in relationships
slide_name <- x$slide$names()[slide_index]
Expand Down Expand Up @@ -136,17 +136,12 @@ ph_hyperlink <- function( x, type = "body", id = 1, id_chr = NULL, ph_label = NU

slide <- x$slide$get_slide(x$cursor)
office_id <- get_shape_id(x, type = type, id = id, ph_label = ph_label )
node <- xml_find_first(slide$get(), sprintf("p:cSld/p:spTree/*[p:nvSpPr/p:cNvPr[@id='%s']]", office_id) )

# declare link in relationships
slide$reference_hyperlink(href)
rel_df <- slide$rel_df()
id <- rel_df[rel_df$target == href, "id" ]
current_elt <- xml_find_first(slide$get(), sprintf("p:cSld/p:spTree/*[p:nvSpPr/p:cNvPr][%.0f]", office_id) )

# add hlinkClick
cnvpr <- xml_child(node, "p:nvSpPr/p:cNvPr")
cnvpr <- xml_child(current_elt, "p:nvSpPr/p:cNvPr")
str_ <- "<a:hlinkClick xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" r:id=\"%s\"/>"
str_ <- sprintf(str_, id)
str_ <- sprintf(str_, href)
xml_add_child(cnvpr, as_xml_document(str_) )
x
}
Expand Down
2 changes: 1 addition & 1 deletion man/officer.Rd

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

Binary file removed tests/testthat/_snaps/docx-add/body_add-elements.png
Binary file not shown.
Binary file modified tests/testthat/_snaps/docx-add/docx-elements.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/testthat/_snaps/rtf-add/rtf-elements.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 19 additions & 19 deletions tests/testthat/test-docx-add.R
Original file line number Diff line number Diff line change
Expand Up @@ -233,22 +233,22 @@ test_that("visual testing", {
expect_snapshot_doc(x = x, name = "docx-elements", engine = "testthat")
})

test_that("body_add visual testing", {
local_edition(3)
testthat::skip_if_not_installed("doconv")
testthat::skip_if_not(doconv::msoffice_available())
library(doconv)

x <- read_docx()
# add text and a table ----
x <- body_add(x, "Hello World")
x <- body_add(x, "Hello title", style = "heading 1")
x <- body_add(x, "Hello title", style = "heading 2")
x <- body_add(x, head(cars))
x <- body_add(x, "Hello base plot", style = "heading 2")
x <- body_add(x, anyplot)
x <- body_add(x, "Hello fpars", style = "heading 2")
x <- body_add(x = x, bl)

expect_snapshot_doc(x = x, name = "body_add-elements", engine = "testthat")
})
# test_that("body_add visual testing", {
# local_edition(3)
# testthat::skip_if_not_installed("doconv")
# testthat::skip_if_not(doconv::msoffice_available())
# library(doconv)
#
# x <- read_docx()
# # add text and a table ----
# x <- body_add(x, "Hello World")
# x <- body_add(x, "Hello title", style = "heading 1")
# x <- body_add(x, "Hello title", style = "heading 2")
# x <- body_add(x, head(cars))
# x <- body_add(x, "Hello base plot", style = "heading 2")
# x <- body_add(x, anyplot)
# x <- body_add(x, "Hello fpars", style = "heading 2")
# x <- body_add(x = x, bl)
#
# expect_snapshot_doc(x = x, name = "body_add-elements", engine = "testthat")
# })
3 changes: 3 additions & 0 deletions tests/testthat/test-pptx-add.R
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,10 @@ test_that("hyperlink shape", {
doc <- ph_with(x = doc, location = ph_location_type(type = "title"), value = "Un titre 2")
doc <- on_slide(doc, 1)
doc <- ph_hyperlink(x = doc, type = "title", href = "https://cran.r-project.org")
outputfile <- tempfile(fileext = ".pptx")
print(doc, target = outputfile)

doc <- read_pptx(outputfile)
rel_df <- doc$slide$get_slide(1)$rel_df()

expect_true( "https://cran.r-project.org" %in% rel_df$target )
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-pptx-selections.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ test_that("get shape id", {
doc <- ph_with(doc, "hello", location = ph_location_type(type = "body"))
file <- print(doc, target = tempfile(fileext = ".pptx"))
doc <- read_pptx(file)
expect_equal(officer:::get_shape_id(doc, type = "body", id = 1), "2")
expect_equal(officer:::get_shape_id(doc, ph_label = "Content Placeholder 2", id = 1), "2")
expect_equal(officer:::get_shape_id(doc, type = "body", id = 1), 1)
expect_equal(officer:::get_shape_id(doc, ph_label = "Content Placeholder 2", id = 1), 1)
expect_error(officer:::get_shape_id(doc, type = "body", id = 4) )
})

Expand Down

0 comments on commit 713756f

Please sign in to comment.