Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set column width to the maximum allowed value #653

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions lib/axlsx/workbook/worksheet/col.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ module Axlsx
# The Col class defines column attributes for columns in sheets.
class Col

# Maximum column width limit in MS Excel is 255 characters
# https://support.microsoft.com/en-us/office/excel-specifications-and-limits-1672b34d-7043-467e-8e27-269d656771c3
MAX_WIDTH = 255

include Axlsx::OptionsParser
include Axlsx::SerializedAttributes
# Create a new Col objects
Expand Down Expand Up @@ -111,10 +115,10 @@ def width=(v)
# TODO!!!
#Axlsx.validate_unsigned_numeric(v) unless v == nil
@custom_width = @best_fit = v != nil
@width = v
@width = v.nil? ? v : [v, MAX_WIDTH].min
end

# updates the width for this col based on the cells autowidth and
# updates the width for this col based on the cells autowidth and
# an optionally specified fixed width
# @param [Cell] cell The cell to use in updating this col's width
# @param [Integer] fixed_width If this is specified the width is set
Expand All @@ -127,8 +131,8 @@ def update_width(cell, fixed_width=nil, use_autowidth=true)
elsif use_autowidth
cell_width = cell.autowidth
self.width = cell_width unless (width || 0) > (cell_width || 0)
end
end
end
end

# Serialize this columns data to an xml string
# @param [String] str
Expand Down
17 changes: 16 additions & 1 deletion test/workbook/worksheet/tc_col.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def setup
end

def test_initialize
options = { :width => 12, :collapsed => true, :hidden => true, :outline_level => 1, :phonetic => true, :style => 1}
options = { :width => 12, :collapsed => true, :hidden => true, :outline_level => 1, :phonetic => true, :style => 1}

col = Axlsx::Col.new 0, 0, options
options.each{ |key, value| assert_equal(col.send(key.to_sym), value) }
Expand Down Expand Up @@ -39,6 +39,21 @@ def test_customWidth
assert_equal(@col.customWidth, true, 'customWidth is true when width is set')
end

def test_widthUnderLimit
@col.width = 3
assert_equal(@col.width, 3, 'width is set to exact value')
end

def test_widthOverLimit
@col.width = 31337
assert_equal(@col.width, 255, 'width is set to maximum allowed value')
end

def test_widthSetToNil
@col.width = nil
assert_equal(@col.width, nil, 'width is set to unspecified value')
end

def test_hidden
assert_equal(@col.hidden, nil)
assert_raise(ArgumentError, 'hidden must be boolean(ish)') { @col.hidden = 'bob' }
Expand Down