Paxa / fast_excel

Ultra Fast Excel Writer for Ruby
MIT License
336 stars 59 forks source link

auto-width: On close set empty columns to default width #96

Closed steffansluis closed 11 months ago

steffansluis commented 1 year ago

When auto-width is enabled, having an empty column (e.g. a column width only nil or '' values) causes NoMethodError: undefined method '+' for nil:NilClass because the corresponding value in @column_widths ends up as nil. This PR adds a test-case that fails on that condition, and also fixes the problem that causes the error. The fix works by replacing nil or 0 with the default column width like is also done here. To be able to test it and to allow and consuming code to access the final column width, I decided to use transform_values! so the final changes are reflected in @column_widths and as such in sheet.calculated_column_widths, even though the workbook is already closed.

I considered a fix here by replacing > with >= to allow the resulting value to become 0, however this would not work because of the + 0.2. That + 0.2 is also not reflected in the final @column_widths, however that was already not the case with the way the test(s) work, so I figured this would be the most intuitive way to fix the implementation while still allowing a simple test.