Skip to content

Commit

Permalink
Update read.jl to pass through Custom XML internal files (#261)
Browse files Browse the repository at this point in the history
* Update read.jl to pass through Custom XML internal files

Addresses issue #210

* Add example containing customXml internal files

* Read and write template containing customXml 

This is a test for issue #210. It checks that templates that are read can be written again. It needs an example .xlsx file containing internal customXml files to test.
  • Loading branch information
TimG1964 committed Aug 26, 2024
1 parent e80dba0 commit 39400f4
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
Binary file added data/customXml.xlsx
Binary file not shown.
13 changes: 9 additions & 4 deletions src/read.jl
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,10 @@ function open_or_read_xlsx(source::Union{IO, AbstractString}, read_files::Bool,
continue
end

if endswith(f.name, ".xml") || endswith(f.name, ".rels")
# Rather than ignore custom XML internal files here, let them get passed through to write like binaries are.
if !startswith(f.name, "customXml") && (endswith(f.name, ".xml") || endswith(f.name, ".rels"))
#if endswith(f.name, ".xml") || endswith(f.name, ".rels")

# XML file
internal_xml_file_add!(xf, f.name)
if read_files
Expand All @@ -215,16 +218,18 @@ function open_or_read_xlsx(source::Union{IO, AbstractString}, read_files::Bool,
end

# ignore custom XML internal files
if startswith(f.name, "customXml")
continue
end
# no longer needed if these files are passed through like binary files
#if startswith(f.name, "customXml")
# continue
#end

internal_xml_file_read(xf, f.name)
end
elseif read_as_template

# Binary file
# we only read binary files to save the Excel file later
# Custom XML files also now get passed through this way, too
bytes = ZipFile.read(f)
@assert sizeof(bytes) == f.uncompressedsize
xf.binary_data[f.name] = bytes
Expand Down
25 changes: 25 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1113,6 +1113,31 @@ end
isfile(filename_copy) && rm(filename_copy)
end

@testset "CustomXml" begin
# issue #210
# None of the example .xlsx files in the test suite include custoimXml internal files
# but customXml.xlsx does
template = XLSX.open_xlsx_template(joinpath(data_directory, "customXml.xlsx"))
filename_copy = "customXml_copy.xlsx"
for sn in XLSX.sheetnames(template)
sheet = template[sn]
sheet["Q1"] = "Cant" # using an apostrophe here causes a test failure reading the copy - "Evaluated: "Can't" == "Can't""
sheet["Q2"] = "write"
sheet["Q3"] = "this"
sheet["Q4"] = "template"
end
@test XLSX.writexlsx(filename_copy, template, overwrite=true) == nothing # This is where the bug will throw if custoimXml internal files present.
@test isfile(filename_copy)
f_copy = XLSX.readxlsx(filename_copy) # Don't really think this second part is necessary.
test_Xmlread = [["Cant", "write", "this", "template"]]
for sn in XLSX.sheetnames(f_copy)
sheet = template[sn]
data = [[sheet["Q1"], sheet["Q2"], sheet["Q3"], sheet["Q4"]]]
check_test_data(data, test_Xmlread)
end
isfile(filename_copy) && rm(filename_copy)
end

@testset "Edit Template" begin
new_filename = "new_file_from_empty_template.xlsx"
f = XLSX.open_empty_template()
Expand Down

0 comments on commit 39400f4

Please sign in to comment.