Skip to content

Commit

Permalink
auto-width: On close set empty columns to default width
Browse files Browse the repository at this point in the history
  • Loading branch information
steffansluis authored and Paxa committed Dec 29, 2023
1 parent 88badc4 commit 02d6b10
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/fast_excel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ def enable_filters!(start_col: 0, end_col:)

def close
if auto_width?
@column_widths.each do |num, width|
@column_widths.transform_values!{ |width| width || DEF_COL_WIDTH }.each do |num, width|
set_column_width(num, width + 0.2)
end
end
Expand Down
20 changes: 20 additions & 0 deletions test/auto_width_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,24 @@

assert_equal(sheet.calculated_column_widths, {0 => 3.52, 1 => 5.28, 2 => 14.96, 3 => 44.88})
end

it "should set the default column width for an empty column on close" do
workbook = FastExcel.open(constant_memory: false)
sheet = workbook.add_worksheet
sheet.auto_width = true

sheet.append_row([
nil,
"tini",
"Longer",
"Some longer text!",
"This gem is FFI binding for libxlsxwriter C library"
])

assert_equal(sheet.calculated_column_widths, {0 => nil, 1 => 3.52, 2 => 5.28, 3 => 14.96, 4 => 44.88})

workbook.close

assert_equal(sheet.calculated_column_widths, {0 => FastExcel::DEF_COL_WIDTH, 1 => 3.52, 2 => 5.28, 3 => 14.96, 4 => 44.88})
end
end

0 comments on commit 02d6b10

Please sign in to comment.