Skip to content

Commit

Permalink
fix: read_custom_properties and empty lpwstr tags
Browse files Browse the repository at this point in the history
  • Loading branch information
davidgohel committed Dec 20, 2022
1 parent a6e88f1 commit b32af49
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 26 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.4.5.012
Version: 0.4.5.013
Authors@R: c(
person("David", "Gohel", role = c("aut", "cre"),
email = "david.gohel@ardata.fr"),
Expand Down
59 changes: 34 additions & 25 deletions R/custom_properties.R
Original file line number Diff line number Diff line change
@@ -1,66 +1,75 @@
#' @importFrom xml2 xml_children
read_custom_properties <- function( package_dir ){
read_custom_properties <- function(package_dir) {
filename <- file.path(package_dir, "docProps/custom.xml")
if( !file.exists(filename) ) {
if (!file.exists(filename)) {
filename <- system.file(package = "officer", "template/custom.xml")
}
doc <- read_xml(filename)
all_children <- xml_children(doc)

pid_values <- vapply(all_children, xml_attr, NA_character_, "pid")
name_values <- vapply(all_children, xml_attr, NA_character_, "name")
value_values <- vapply(all_children, function(x) {as.character(xml_child(x, 1))}, NA_character_)
value_values <- vapply(all_children, function(x) {
as.character(xml_child(x, 1))
}, NA_character_)
value_values <- gsub("<vt:lpwstr/>", "", value_values, fixed = TRUE)
value_values <- gsub("<vt:lpwstr>", "", value_values, fixed = TRUE)
value_values <- gsub("</vt:lpwstr>", "", value_values, fixed = TRUE)
str <- c(pid_values, name_values, value_values)

z <- matrix(str, ncol = 3,
dimnames = list(NULL, c("pid", "name", "value")))
z <- matrix(str,
ncol = 3,
dimnames = list(NULL, c("pid", "name", "value"))
)
z <- list(data = z)
class(z) <- "custom_properties"
z
}

`[<-.custom_properties` <- function( x, i, j, value ){
if( !i %in% x$data[,"name"] ) {
if(nrow(x$data)<1) {
`[<-.custom_properties` <- function(x, i, j, value) {
if (!i %in% x$data[, "name"]) {
if (nrow(x$data) < 1) {
pid <- 2
} else {
pid <- max(as.integer(x$data[, "pid"])) + 1L
}
new <- matrix(c(as.character(pid), i, value), ncol = 3)
x$data <- rbind(x$data, new)
} else {
x$data[x$data[,"name"] %in% i, j] <- value
x$data[x$data[, "name"] %in% i, j] <- value
}
x
}
`[.custom_properties` <- function( x, i, j ){
if(nrow(x$data) < 1) {
`[.custom_properties` <- function(x, i, j) {
if (nrow(x$data) < 1) {
return(character())
}
if(missing(i)) {
if (missing(i)) {
x$data[, j]
} else {
x$data[x$data[,"name"] %in% i, j]
x$data[x$data[, "name"] %in% i, j]
}
}

write_custom_properties <- function(custom_props, package_dir){
write_custom_properties <- function(custom_props, package_dir) {
xml_props <- sprintf(
"<property fmtid=\"{D5CDD505-2E9C-101B-9397-08002B2CF9AE}\" pid=\"%s\" name=\"%s\"><vt:lpwstr>%s</vt:lpwstr></property>",
custom_props$data[,1],
custom_props$data[,2],
custom_props$data[,3])
custom_props$data[, 1],
custom_props$data[, 2],
custom_props$data[, 3]
)

xml_ <- c("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>",
"<Properties ",
"xmlns=\"http://schemas.openxmlformats.org/officeDocument/2006/custom-properties\" ",
"xmlns:vt=\"http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes\">",
xml_props,
"</Properties>")
props_dir = file.path(package_dir, "docProps")
xml_ <- c(
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>",
"<Properties ",
"xmlns=\"http://schemas.openxmlformats.org/officeDocument/2006/custom-properties\" ",
"xmlns:vt=\"http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes\">",
xml_props,
"</Properties>"
)
props_dir <- file.path(package_dir, "docProps")
dir.create(props_dir, recursive = TRUE, showWarnings = FALSE)
filename <- file.path(props_dir, "custom.xml")
writeLines(enc2utf8(xml_), filename, useBytes=TRUE)
writeLines(enc2utf8(xml_), filename, useBytes = TRUE)
invisible()
}

0 comments on commit b32af49

Please sign in to comment.