Skip to content

Commit

Permalink
read and write chartEx
Browse files Browse the repository at this point in the history
  • Loading branch information
JanMarvin committed Nov 27, 2022
1 parent 7653321 commit f42c522
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 12 deletions.
25 changes: 24 additions & 1 deletion R/class-workbook.R
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,13 @@ wbWorkbook <- R6::R6Class(
private$set_current_sheet(newSheetIndex)
sheetId <- private$get_sheet_id_max() # checks for length of worksheets

if (!all(self$charts$chartEx == "")) {
warning(
"The file you have loaded contains chart extensions. At the moment,",
" cloning worksheets can damage the output."
)
}

# not the best but a quick fix
new_raw <- new
new <- replace_legal_chars(new)
Expand Down Expand Up @@ -5555,7 +5562,7 @@ wbWorkbook <- R6::R6Class(

if (!file.exists(xlchartsDir)) {
dir.create(xlchartsDir, recursive = TRUE)
if (any(self$rels != ""))
if (any(self$charts$rels != "") || any(self$charts$relsEx != ""))
dir.create(xlchartsRelsDir, recursive = TRUE)
}

Expand All @@ -5570,6 +5577,15 @@ wbWorkbook <- R6::R6Class(
)
}

if (self$charts$chartEx[crt] != "") {
ct <- c(ct, sprintf('<Override PartName="/xl/charts/chartEx%s.xml" ContentType="application/vnd.ms-office.chartex+xml"/>', crt))

write_file(
body = self$charts$chartEx[crt],
fl = file.path(xlchartsDir, stri_join("chartEx", crt, ".xml"))
)
}

if (self$charts$colors[crt] != "") {
ct <- c(ct, sprintf('<Override PartName="/xl/charts/colors%s.xml" ContentType="application/vnd.ms-office.chartcolorstyle+xml"/>', crt))

Expand All @@ -5594,6 +5610,13 @@ wbWorkbook <- R6::R6Class(
fl = file.path(xlchartsRelsDir, stri_join("chart", crt, ".xml.rels"))
)
}

if (self$charts$relsEx[crt] != "") {
write_file(
body = self$charts$relsEx[crt],
fl = file.path(xlchartsRelsDir, stri_join("chartEx", crt, ".xml.rels"))
)
}
}

}
Expand Down
40 changes: 29 additions & 11 deletions R/wb_load.R
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,11 @@ wb_load <- function(file, xlsxFile = NULL, sheet, data_only = FALSE) {

# charts
chartsXML <- grep_xml("xl/charts/chart[0-9]+\\.xml$")
chartExsXML <- grep_xml("xl/charts/chartEx[0-9]+\\.xml$")
chartsXML_colors <- grep_xml("xl/charts/colors[0-9]+\\.xml$")
chartsXML_styles <- grep_xml("xl/charts/style[0-9]+\\.xml$")
chartsRels <- grep_xml("xl/charts/_rels")
chartsRels <- grep_xml("xl/charts/_rels/chart[0-9]+.xml.rels")
chartExsRels <- grep_xml("xl/charts/_rels/chartEx[0-9]+.xml.rels")
chartSheetsXML <- grep_xml("xl/chartsheets/sheet[0-9]+\\.xml")

# tables
Expand Down Expand Up @@ -558,38 +560,54 @@ wb_load <- function(file, xlsxFile = NULL, sheet, data_only = FALSE) {
}

## xl\chart
if (!data_only && length(chartsXML)) {
if (!data_only && (length(chartsXML)) || length(chartExsXML)) {

# Not every chart has chart, color, style and rel. We read the file names
# into charts first and replace the file name with the content in a second
# run.
empty_chr <- vector("character", length(chartsXML))

# There are some newer charts (presumably all x14), that are written as
# chartEX and ofc they are counted starting at 1. So the total number of
# charts is chartsXML + chartsExXML.

chart_num <- length(chartsXML) + length(chartExsXML)
empty_chr <- vector("character", chart_num)
charts <- data.frame(
chart = empty_chr,
colors = empty_chr,
style = empty_chr,
rels = empty_chr
chart = empty_chr,
chartEx = empty_chr,
colors = empty_chr,
style = empty_chr,
rels = empty_chr,
relsEx = empty_chr
)

chartsXML_id <- filename_id(chartsXML)
chartExsXML_id <- filename_id(chartExsXML)
chartsXML_colors_id <- filename_id(chartsXML_colors)
chartsXML_styles_id <- filename_id(chartsXML_styles)
chartsRels_id <- filename_id(chartsRels)
chartExsRels_id <- filename_id(chartExsRels)

charts$chart[chartsXML_id] <- names(chartsXML_id)
charts$chartEx[chartExsXML_id] <- names(chartExsXML_id)
charts$colors[chartsXML_colors_id] <- names(chartsXML_colors_id)
charts$style[chartsXML_styles_id] <- names(chartsXML_styles_id)
charts$rels[chartsRels_id] <- names(chartsRels_id)
charts$relsEx[chartExsRels_id] <- names(chartExsRels_id)

crt_ch <- charts$chart != ""
crt_ex <- charts$chartEx != ""
crt_rl <- charts$rels != ""
crt_re <- charts$relsEx != ""
crt_co <- charts$colors != ""
crt_st <- charts$style != ""

charts$chart[crt_ch] <- read_xml_files(charts$chart[crt_ch])
charts$colors[crt_co] <- read_xml_files(charts$colors[crt_co])
charts$style[crt_st] <- read_xml_files(charts$style[crt_st])
charts$rels[crt_rl] <- read_xml_files(charts$rels[crt_rl])
charts$chart[crt_ch] <- read_xml_files(charts$chart[crt_ch])
charts$chartEx[crt_ex] <- read_xml_files(charts$chartEx[crt_ex])
charts$colors[crt_co] <- read_xml_files(charts$colors[crt_co])
charts$style[crt_st] <- read_xml_files(charts$style[crt_st])
charts$rels[crt_rl] <- read_xml_files(charts$rels[crt_rl])
charts$relsEx[crt_re] <- read_xml_files(charts$relsEx[crt_re])

wb$charts <- charts

Expand Down
15 changes: 15 additions & 0 deletions tests/testthat/test-read_sources.R
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,18 @@ test_that("calcChain is updated", {
expect_equal(exp, got)

})

test_that("read workbook with chart extension", {

skip_if_offline()

fl <- "https://github.com/JanMarvin/openxlsx-data/raw/main/charts.xlsx"

wb <- wb_load(fl)

expect_warning(
wb$clone_worksheet(),
"The file you have loaded contains chart extensions. At the moment, cloning worksheets can damage the output."
)

})

0 comments on commit f42c522

Please sign in to comment.