summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorRandy Morgan <[email protected]>2012-02-13 22:28:58 +0900
committerRandy Morgan <[email protected]>2012-02-13 22:28:58 +0900
commit80459ca70930ee8522b8547669bbbe7aef5a12a4 (patch)
treedc5495352ba3e3b5dbb2c977e32c55f371397ab3
parenta78481741fa7b6bf999461a12cc5c9dce026ea49 (diff)
downloadcaxlsx-80459ca70930ee8522b8547669bbbe7aef5a12a4.tar.gz
caxlsx-80459ca70930ee8522b8547669bbbe7aef5a12a4.zip
Improve accessibility and stringency for column_widths editing.
-rw-r--r--README.md2
-rw-r--r--examples/example.rb2
-rw-r--r--lib/axlsx/util/validators.rb8
-rw-r--r--lib/axlsx/workbook/worksheet/worksheet.rb14
-rw-r--r--test/workbook/worksheet/tc_worksheet.rb10
5 files changed, 33 insertions, 3 deletions
diff --git a/README.md b/README.md
index a8fc8f8a..23fe446a 100644
--- a/README.md
+++ b/README.md
@@ -262,7 +262,7 @@ To install Axlsx, use the following command:
wb.add_worksheet(:name => "custom column widths") do |sheet|
sheet.add_row ["I use auto_fit and am very wide", "I use a custom width and am narrow"]
- sheet.auto_fit_data[1][:fixed] = 3
+ sheet.column_widths [nil, 3]
end
##Validate and Serialize
diff --git a/examples/example.rb b/examples/example.rb
index 9e4b0277..552a5fc9 100644
--- a/examples/example.rb
+++ b/examples/example.rb
@@ -191,7 +191,7 @@
wb.add_worksheet(:name => "custom column widths") do |sheet|
sheet.add_row ["I use autowidth and am very wide", "I use a custom width and am narrow"]
- sheet.auto_fit_data[1][:fixed] = 3
+ sheet.column_widths [nil, 3]
end
##Validate and Serialize
diff --git a/lib/axlsx/util/validators.rb b/lib/axlsx/util/validators.rb
index 9accd3db..ce810e54 100644
--- a/lib/axlsx/util/validators.rb
+++ b/lib/axlsx/util/validators.rb
@@ -53,6 +53,14 @@ module Axlsx
DataTypeValidator.validate(:unsigned_int, [Fixnum, Integer], v, lambda { |arg| arg.respond_to?(:>=) && arg >= 0 })
end
+ # Requires that the value is a Fixnum Integer or Float and is greater or equal to 0
+ # @param [Any] v The value validated
+ # @raise [ArgumentError] raised if the value is not a Fixnum or Integer value greater or equal to 0
+ # @return [Boolean] true if the data is valid
+ def self.validate_unsigned_numeric(v)
+ DataTypeValidator.validate("Invalid column width", [Fixnum, Integer, Float], v, lambda { |arg| arg.respond_to?(:>=) && arg >= 0 })
+ end
+
# Requires that the value is a Fixnum or Integer
# @param [Any] v The value validated
def self.validate_int(v)
diff --git a/lib/axlsx/workbook/worksheet/worksheet.rb b/lib/axlsx/workbook/worksheet/worksheet.rb
index 11a2292e..aa701c9f 100644
--- a/lib/axlsx/workbook/worksheet/worksheet.rb
+++ b/lib/axlsx/workbook/worksheet/worksheet.rb
@@ -217,7 +217,19 @@ module Axlsx
end
end
end
-
+
+ # lets you specify a fixed width for any valid column in the worksheet.
+ # Axlsx is sparse, so if you have not set data for a column, you cannot set the width.
+ # Setting a fixed column width to nil will revert the behaviour back to calculating the width for you.
+ # @option options [Array] a array of column widths. Use nil memebers do default to the auto-fit behaviour. Values should be nil or an unsigned Integer, Float or Fixnum
+ def column_widths(options=[])
+ options.each_with_index do |value, index|
+ raise ArgumentError, "Invalid column specification" unless index < @auto_fit_data.size
+ Axlsx::validate_unsigned_numeric(value) unless value == nil
+ @auto_fit_data[index][:fixed] = value
+ end
+ end
+
# Adds a chart to this worksheets drawing. This is the recommended way to create charts for your worksheet. This method wraps the complexity of dealing with ooxml drawing, anchors, markers graphic frames chart objects and all the other dirty details.
# @param [Class] chart_type
# @option options [Array] start_at
diff --git a/test/workbook/worksheet/tc_worksheet.rb b/test/workbook/worksheet/tc_worksheet.rb
index 3e5bc4c4..a6f9b304 100644
--- a/test/workbook/worksheet/tc_worksheet.rb
+++ b/test/workbook/worksheet/tc_worksheet.rb
@@ -150,6 +150,16 @@ class TestWorksheet < Test::Unit::TestCase
assert_equal(@ws.send(:auto_width, {:sz=>11, :longest => "This is a really long string", :fixed=>0.2}), 0.2, "fixed rules!")
end
+ def test_set_column_width
+ @ws.add_row ["chasing windmills", "penut"]
+ assert_equal(@ws.auto_fit_data[0][:fixed], nil, 'no fixed by default')
+ @ws.column_widths [nil, 0.5]
+ assert_equal(@ws.auto_fit_data[1][:fixed], 0.5, 'eat my width')
+ assert_raise(ArgumentError, 'reject invalid columns') { @ws.column_widths [2, 7, nil] }
+ assert_raise(ArgumentError, 'only accept unsigned ints') { @ws.column_widths [2, 7, -1] }
+ assert_raise(ArgumentError, 'only accept Integer, Float or Fixnum') { @ws.column_widths [2, 7, "-1"] }
+ end
+
def test_merge_cells
assert(@ws.merged_cells.is_a?(Array))