Skip to content

Commit

Permalink
minor code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
felipenoris committed Feb 16, 2019
1 parent b0a5e40 commit 73ef285
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 39 deletions.
2 changes: 1 addition & 1 deletion src/XLSX.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ end

const SPREADSHEET_NAMESPACE_XPATH_ARG = [ "xpath" => "http://schemas.openxmlformats.org/spreadsheetml/2006/main" ]

include("structs.jl")
include("types.jl")
include("cellref.jl")
include("sst.jl")
include("stream.jl")
Expand Down
23 changes: 13 additions & 10 deletions src/relationship.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,21 @@ Adds new relationship. Returns new generated rId.
function add_relationship!(wb::Workbook, target::String, _type::String) :: String
xf = get_xlsxfile(wb)
@assert is_writable(xf) "XLSXFile instance is not writable."
got_unique_id = false
id = 1
local rId :: String

while !got_unique_id
got_unique_id = true
rId = string("rId", id)
for r in wb.relationships
if r.Id == rId
got_unique_id = false
id += 1
break
let
got_unique_id = false
id = 1

while !got_unique_id
got_unique_id = true
rId = string("rId", id)
for r in wb.relationships
if r.Id == rId
got_unique_id = false
id += 1
break
end
end
end
end
Expand Down
File renamed without changes.
46 changes: 25 additions & 21 deletions src/worksheet.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,32 @@ function Worksheet(xf::XLSXFile, sheet_element::EzXML.Node)
end

function read_worksheet_dimension(xf::XLSXFile, relationship_id, name) :: CellRange
local result::Union{Nothing, CellRange} = nothing

wb = get_workbook(xf)
target_file = get_relationship_target_by_id("xl", wb, relationship_id)
zip_io, reader = open_internal_file_stream(xf, target_file)

local result::Union{Nothing, CellRange} = nothing
try
# read Worksheet dimension
while EzXML.iterate(reader) != nothing
if EzXML.nodetype(reader) == EzXML.READER_ELEMENT && EzXML.nodename(reader) == "dimension"
@assert EzXML.nodedepth(reader) == 1 "Malformed Worksheet \"$(ws.name)\": unexpected node depth for dimension node: $(EzXML.nodedepth(reader))."
ref_str = reader["ref"]
if is_valid_cellname(ref_str)
result = CellRange("$(ref_str):$(ref_str)")
else
result = CellRange(ref_str)
end

# read Worksheet dimension
while EzXML.iterate(reader) != nothing
if EzXML.nodetype(reader) == EzXML.READER_ELEMENT && EzXML.nodename(reader) == "dimension"
@assert EzXML.nodedepth(reader) == 1 "Malformed Worksheet \"$(ws.name)\": unexpected node depth for dimension node: $(EzXML.nodedepth(reader))."
ref_str = reader["ref"]
if is_valid_cellname(ref_str)
result = CellRange("$(ref_str):$(ref_str)")
else
result = CellRange(ref_str)
break
end

break
end
finally
close(reader)
close(zip_io)
end

close(reader)
close(zip_io)

if result == nothing
error("Couldn't parse worksheet $name dimension.")
else
Expand Down Expand Up @@ -250,13 +252,15 @@ function getcellrange(ws::Worksheet, rng::ColumnRange) :: Array{AbstractCell,2}
columns[i] = Vector{AbstractCell}()
end

left, right = column_bounds(rng)
let
left, right = column_bounds(rng)

for sheetrow in eachrow(ws)
for column in left:right
cell = getcell(sheetrow, column)
c = relative_column_position(cell, rng) # r will be ignored
push!(columns[c], cell)
for sheetrow in eachrow(ws)
for column in left:right
cell = getcell(sheetrow, column)
c = relative_column_position(cell, rng) # r will be ignored
push!(columns[c], cell)
end
end
end

Expand Down
16 changes: 9 additions & 7 deletions src/write.jl
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,12 @@ function update_worksheets_xml!(xl::XLSXFile)
doc_copy = EzXML.parsexml(String(take!(buff)))

# deletes all elements under sheetData
child_nodes = EzXML.findall("/xpath:worksheet/xpath:sheetData/xpath:row", EzXML.root(doc_copy), SPREADSHEET_NAMESPACE_XPATH_ARG)
for c in child_nodes
EzXML.unlink!(c)
let
child_nodes = EzXML.findall("/xpath:worksheet/xpath:sheetData/xpath:row", EzXML.root(doc_copy), SPREADSHEET_NAMESPACE_XPATH_ARG)
for c in child_nodes
EzXML.unlink!(c)
end
end
c = nothing
child_nodes = nothing

# updates sheetData
sheetData_node = EzXML.findfirst("/xpath:worksheet/xpath:sheetData", EzXML.root(doc_copy), SPREADSHEET_NAMESPACE_XPATH_ARG)
Expand Down Expand Up @@ -176,8 +176,10 @@ function update_worksheets_xml!(xl::XLSXFile)
end

# updates worksheet dimension
dimension_node = EzXML.findfirst("/xpath:worksheet/xpath:dimension", EzXML.root(doc_copy), SPREADSHEET_NAMESPACE_XPATH_ARG)
dimension_node["ref"] = string(get_dimension(sheet))
let
dimension_node = EzXML.findfirst("/xpath:worksheet/xpath:dimension", EzXML.root(doc_copy), SPREADSHEET_NAMESPACE_XPATH_ARG)
dimension_node["ref"] = string(get_dimension(sheet))
end

set_worksheet_xml_document!(sheet, doc_copy)
end
Expand Down

0 comments on commit 73ef285

Please sign in to comment.