Skip to content

Commit

Permalink
add consts for max size and assert in writetable! (#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanrboyer authored Dec 7, 2023
1 parent 063b2e2 commit 8e63de8
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/XLSX.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ if !hasmethod(Base.bytesavailable, Tuple{ZipFile.ReadableFile})
end

const SPREADSHEET_NAMESPACE_XPATH_ARG = [ "xpath" => "http://schemas.openxmlformats.org/spreadsheetml/2006/main" ]
const EXCEL_MAX_COLS = 16_384 # total columns supported by Excel per sheet
const EXCEL_MAX_ROWS = 1_048_576 # total rows supported by Excel per sheet (including headers)

include("types.jl")
include("formula.jl")
Expand Down
6 changes: 3 additions & 3 deletions src/cellref.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ end

# Converts column number to a column name. See also XLSX.decode_column_number.
function encode_column_number(column_number::Int) :: String
@assert column_number > 0 && column_number <= 16384 "Column number should be in the range from 1 to 16384."
@assert column_number > 0 && column_number <= EXCEL_MAX_COLS "Column number should be in the range from 1 to 16384."

third_letter_sequence = div(column_number - 26 - 1, 26*26)
column_number = column_number - third_letter_sequence*(26*26)
Expand Down Expand Up @@ -69,7 +69,7 @@ function is_valid_column_name(n::AbstractString) :: Bool
end

column_number = decode_column_number(n)
if column_number < 1 || column_number > 16384
if column_number < 1 || column_number > EXCEL_MAX_COLS
return false
end

Expand Down Expand Up @@ -103,7 +103,7 @@ function is_valid_cellname(n::AbstractString) :: Bool

column_name, row = split_cellname(n)

if row < 1 || row > 1048576
if row < 1 || row > EXCEL_MAX_ROWS
return false
end

Expand Down
2 changes: 2 additions & 0 deletions src/write.jl
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,9 @@ function writetable!(sheet::Worksheet, data, columnnames; anchor_cell::CellRef=C
col_count = length(data)
@assert col_count == length(columnnames) "Column count mismatch between `data` ($col_count columns) and `columnnames` ($(length(columnnames)) columns)."
@assert col_count > 0 "Can't write table with no columns."
@assert col_count <= EXCEL_MAX_COLS "`data` contains $col_count columns, but Excel only supports up to $EXCEL_MAX_COLS; must reduce `data` size"
row_count = length(data[1])
@assert row_count <= EXCEL_MAX_ROWS-1 "`data` contains $row_count rows, but Excel only supports up to $(EXCEL_MAX_ROWS-1); must reduce `data` size"
if col_count > 1
for c in 2:col_count
@assert length(data[c]) == row_count "Row count mismatch between column 1 ($row_count rows) and column $c ($(length(data[c])) rows)."
Expand Down
4 changes: 2 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ end
cn = XLSX.CellRef("XFD1048576")
@test string(cn) == "XFD1048576"
@test XLSX.column_name(cn) == "XFD"
@test XLSX.row_number(cn) == 1048576
@test XLSX.column_number(cn) == 16384
@test XLSX.row_number(cn) == XLSX.EXCEL_MAX_ROWS
@test XLSX.column_number(cn) == XLSX.EXCEL_MAX_COLS

v_column_numbers = [1
, 15
Expand Down

0 comments on commit 8e63de8

Please sign in to comment.