From 02d6b10cc9c3c63711d9cb25b978a00c9e76f147 Mon Sep 17 00:00:00 2001 From: Steffan Sluis Date: Thu, 26 Oct 2023 21:17:18 +0200 Subject: [PATCH] auto-width: On close set empty columns to default width --- lib/fast_excel.rb | 2 +- test/auto_width_test.rb | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/fast_excel.rb b/lib/fast_excel.rb index fcc0f49..6ba6b40 100644 --- a/lib/fast_excel.rb +++ b/lib/fast_excel.rb @@ -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 diff --git a/test/auto_width_test.rb b/test/auto_width_test.rb index a142298..acdc385 100644 --- a/test/auto_width_test.rb +++ b/test/auto_width_test.rb @@ -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