Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete Worksheet from xlsx File #255

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added data/deletingWs.xlsx
Binary file not shown.
1 change: 1 addition & 0 deletions src/XLSX.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ include("worksheet.jl")
include("cell.jl")
include("styles.jl")
include("write.jl")
include("delete.jl")

end # module XLSX
93 changes: 93 additions & 0 deletions src/delete.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#=
Delete a worksheet by using either its name or ID; this action will generate a new file
containing the remaining worksheets without altering the original file.
When a worksheet is removed, the data types of cells, cell formatting,
and the relationships between worksheets will not be preserved in the new file.
=#

# https://github.com/felipenoris/XLSX.jl/issues/80
using XLSX
function delete_ws_by_name(file_path::AbstractString, sheet_name::AbstractString, new_file::AbstractString)
wb = readxlsx(file_path)
if hassheet(wb, sheet_name)
deleted_sheet_id= getsheet(wb, sheet_name).sheetId
sheet_count = sheetcount(wb)
if sheet_count == 1
openxlsx(new_file, mode="w") do xf
end
return "Because the file has just this sheet '$sheet_name', an empty file was created."
end
openxlsx(new_file, mode="w") do xf
end
names = sheetnames(wb)
sheet_id_new_file = 1
for id in 1:sheet_count
if id == deleted_sheet_id
continue
end
openxlsx(new_file, mode="rw") do xf
if sheet_id_new_file == 1
if xf[sheet_id_new_file] != names[id]
rename!(xf[sheet_id_new_file], names[id])
end
else
addsheet!(xf, names[id])
end
ws_dimension = get_dimension(wb[id])
bottom = row_number(ws_dimension.stop)
right = column_number(ws_dimension.stop)
for r in 1:bottom
for c in 1:right
cell_data = getdata(wb[id], r, c)
setdata!(xf[sheet_id_new_file], r, c, cell_data)
end
end
end
sheet_id_new_file += 1
end
else
error("The Sheet '$sheet_name' is not exists in the file!")
end
end


function delete_ws_by_id(file_path::AbstractString, sheet_id::Int64, new_file::AbstractString)
wb = readxlsx(file_path)
sheet_count = sheetcount(wb)
if sheet_id <= sheet_count
if sheet_count == 1
openxlsx(new_file, mode="w") do xf
end
return "Because the file has just this sheet '$sheet_id', an empty file was created."
end
openxlsx(new_file, mode="w") do xf
end
sheet_id_new_file = 1
for id in 1:sheet_count
if id == sheet_id
continue
end
openxlsx(new_file, mode="rw") do xf
if sheet_id_new_file == 1
if xf[sheet_id_new_file] != wb[id].name
rename!(xf[sheet_id_new_file], wb[id].name)
end
else
addsheet!(xf, wb[id].name)
end
ws_dimension = get_dimension(wb[id])
bottom = row_number(ws_dimension.stop)
right = column_number(ws_dimension.stop)
for r in 1:bottom
for c in 1:right
cell_data = getdata(wb[id], r, c)
setdata!(xf[sheet_id_new_file], r, c, cell_data)
end
end
end
sheet_id_new_file += 1
end
else
error("The Sheet '$sheet_id' is not exists in the file!")
end
end
20 changes: 20 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1995,3 +1995,23 @@ end
end
end
end



@testset "delete.jl Worksheet by Name" begin
wb = XLSX.readxlsx("data\\deletingWs.xlsx")
XLSX.delete_ws_by_name("data\\deletingWs.xlsx", "Sheet1", "data\\testDeletingWsByName.xlsx")
wb_after_deleting = XLSX.readxlsx("data\\testDeletingWsByName.xlsx")
sheet_count = XLSX.sheetcount(wb)
new_file_sheet_count = XLSX.sheetcount(wb_after_deleting)
@test sheet_count == new_file_sheet_count + 1
end

@testset "delete.jl Worksheet by Id" begin
wb = XLSX.readxlsx("data\\deletingWs.xlsx")
XLSX.delete_ws_by_id("data\\deletingWs.xlsx", 1, "data\\testDeletingWsById.xlsx")
wb_after_deleting = XLSX.readxlsx("data\\testDeletingWsById.xlsx")
sheet_count = XLSX.sheetcount(wb)
new_file_sheet_count = XLSX.sheetcount(wb_after_deleting)
@test sheet_count == new_file_sheet_count + 1
end