summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorochko <[email protected]>2012-02-15 19:05:27 +0900
committerochko <[email protected]>2012-02-15 19:05:27 +0900
commita801abe057cc0bc6ddde0a4a80b388b40d8338b9 (patch)
tree0f08388200d622a4c283fa65c6dd0d1c8d1f8212
parente05bb928442aae6e026f3484745f8b99fda64aa2 (diff)
downloadcaxlsx-a801abe057cc0bc6ddde0a4a80b388b40d8338b9.tar.gz
caxlsx-a801abe057cc0bc6ddde0a4a80b388b40d8338b9.zip
Skip auto fit update or directly set fixed value for column width
-rw-r--r--lib/axlsx/workbook/worksheet/worksheet.rb21
-rw-r--r--test/workbook/worksheet/tc_worksheet.rb10
2 files changed, 27 insertions, 4 deletions
diff --git a/lib/axlsx/workbook/worksheet/worksheet.rb b/lib/axlsx/workbook/worksheet/worksheet.rb
index 5f935428..185cc4c6 100644
--- a/lib/axlsx/workbook/worksheet/worksheet.rb
+++ b/lib/axlsx/workbook/worksheet/worksheet.rb
@@ -171,7 +171,7 @@ module Axlsx
# @option options [Array, Integer] style
def add_row(values=[], options={})
Row.new(self, values, options)
- update_auto_fit_data @rows.last.cells
+ update_auto_fit_data @rows.last.cells, options.delete(:widths)
yield @rows.last if block_given?
@rows.last
end
@@ -311,19 +311,32 @@ module Axlsx
# We store an auto_fit_data item for each column. when a row is added we multiple the font size by the length of the text to
# attempt to identify the longest cell in the column. This is not 100% accurate as it needs to take into account
# any formatting that will be applied to the data, as well as the actual rendering size when the length and size is equal
- # for two cells.
+ # for two cells. If second argument is Array, it will try to set fixed width of each column.
+ # If corresponding argement is falsy, it will try to auto fit. If number is given, it will set column as fixed width.
# @return [Array] of Cell objects
# @param [Array] cells an array of cells
- def update_auto_fit_data(cells)
+ # @param [Array] widths an array of cell widths
+ def update_auto_fit_data(cells, widths=nil)
# TODO delay this until rendering. too much work when we dont know what they are going to do to the sheet.
styles = self.workbook.styles
cellXfs, fonts = styles.cellXfs, styles.fonts
sz = 11
cells.each_with_index do |item, index|
+ col = @auto_fit_data[index] ||= {:longest=>"", :sz=>sz, :fixed=>nil}
+
+ if widths.is_a?(Array)
+ if width = widths[index]
+ # any numeric value in width is fixed width
+ col[:fixed] = width if width.is_a?(Numeric)
+ # any non falsy value in width will skip auto fit updating
+ next
+ end
+ # falsy value in width will continue updating auto fit data
+ end
+
# ignore formula - there is no way for us to know the result
next if item.value.is_a?(String) && item.value.start_with?('=')
- col = @auto_fit_data[index] || {:longest=>"", :sz=>sz, :fixed=>nil}
cell_xf = cellXfs[item.style]
font = fonts[cell_xf.fontId || 0]
sz = item.sz || font.sz || fonts[0].sz
diff --git a/test/workbook/worksheet/tc_worksheet.rb b/test/workbook/worksheet/tc_worksheet.rb
index e50490d0..0295a64f 100644
--- a/test/workbook/worksheet/tc_worksheet.rb
+++ b/test/workbook/worksheet/tc_worksheet.rb
@@ -143,6 +143,16 @@ class TestWorksheet < Test::Unit::TestCase
assert_equal(@ws.auto_fit_data[0], {:sz=>10,:longest=>"mule", :fixed=>nil}, "adding a row updates auto_fit_data if the product of the string length and font is greater for the column")
end
+ def test_set_fixed_width_column
+ big = @ws.workbook.styles.add_style(:sz=>10)
+
+ @ws.add_row ["mule", "donkey", "horse"], :style=>big, :widths => [20, true, false]
+ assert(@ws.auto_fit_data.size == 3, "a data item for each column")
+ assert_equal({:sz=>11, :fixed=>20, :longest=>"" }, @ws.auto_fit_data[0], "adding a row with fixed width updates :fixed attribute")
+ assert_equal({:sz=>11,:longest=>"", :fixed=>nil}, @ws.auto_fit_data[1], "adding a row with fixed with 'true' doesn't try set auto fit data")
+ assert_equal({:sz=>10,:longest=>"horse", :fixed=>nil}, @ws.auto_fit_data[2], "adding a row with falsy fixed value updates auto_fit_data")
+ end
+
def test_auto_width
assert(@ws.send(:auto_width, {:sz=>11, :longest=>"fisheries"}) > @ws.send(:auto_width, {:sz=>11, :longest=>"fish"}), "longer strings get a longer auto_width at the same font size")