summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorRandy Morgan <[email protected]>2012-03-28 23:44:15 +0900
committerRandy Morgan <[email protected]>2012-03-28 23:44:15 +0900
commitda054ee2f47261f70ebc18f133dc303acd810581 (patch)
treeb35eff352135a60b7d8a440fd0411b48a8b94369
parent30621f2ce9c88b242524929b184dfeebf89181aa (diff)
downloadcaxlsx-da054ee2f47261f70ebc18f133dc303acd810581.tar.gz
caxlsx-da054ee2f47261f70ebc18f133dc303acd810581.zip
implement column object - still needs to be tied in to a rewrite of autofit_data
-rw-r--r--lib/axlsx/workbook/workbook.rb1
-rw-r--r--lib/axlsx/workbook/worksheet/col.rb113
-rw-r--r--test/workbook/worksheet/tc_col.rb59
3 files changed, 173 insertions, 0 deletions
diff --git a/lib/axlsx/workbook/workbook.rb b/lib/axlsx/workbook/workbook.rb
index a8e0a1af..7f557ef7 100644
--- a/lib/axlsx/workbook/workbook.rb
+++ b/lib/axlsx/workbook/workbook.rb
@@ -5,6 +5,7 @@ require 'axlsx/workbook/worksheet/date_time_converter.rb'
require 'axlsx/workbook/worksheet/cell.rb'
require 'axlsx/workbook/worksheet/page_margins.rb'
require 'axlsx/workbook/worksheet/row.rb'
+require 'axlsx/workbook/worksheet/col.rb'
require 'axlsx/workbook/worksheet/worksheet.rb'
require 'axlsx/workbook/shared_strings_table.rb'
require 'axlsx/workbook/worksheet/table.rb'
diff --git a/lib/axlsx/workbook/worksheet/col.rb b/lib/axlsx/workbook/worksheet/col.rb
new file mode 100644
index 00000000..e9be61a6
--- /dev/null
+++ b/lib/axlsx/workbook/worksheet/col.rb
@@ -0,0 +1,113 @@
+# encoding: UTF-8
+module Axlsx
+
+ # The Col class defines column attributes for columns in sheets.
+ class Col
+
+ # First column affected by this 'column info' record.
+ # @return [Integer]
+ attr_reader :min
+
+ # Last column affected by this 'column info' record.
+ # @return [Integer]
+ attr_reader :max
+
+ # Flag indicating if the specified column(s) is set to 'best fit'. 'Best fit' is set to true under these conditions:
+ # The column width has never been manually set by the user, AND The column width is not the default width
+ # 'Best fit' means that when numbers are typed into a cell contained in a 'best fit' column, the column width should
+ # automatically resize to display the number. [Note: In best fit cases, column width must not be made smaller, only larger. end note]
+ # @return [Boolean]
+ attr_reader :bestFit
+
+ # Flag indicating if the outlining of the affected column(s) is in the collapsed state.
+ # @return [Boolean]
+ attr_reader :collapsed
+
+ # Flag indicating if the affected column(s) are hidden on this worksheet.
+ # @return [Boolean]
+ attr_reader :hidden
+
+ # Outline level of affected column(s). Range is 0 to 7.
+ # @return [Integer]
+ attr_reader :outlineLevel
+
+ # Flag indicating if the phonetic information should be displayed by default for the affected column(s) of the worksheet.
+ # @return [Boolean]
+ attr_reader :phonetic
+
+ # Default style for the affected column(s). Affects cells not yet allocated in the column(s). In other words, this style applies to new columns.
+ # @return [Integer]
+ attr_reader :style
+
+ # The width of the column
+ # @return [Numeric]
+ attr_reader :width
+
+ # @return [Boolean]
+ attr_reader :customWidth
+
+ # @see Col#collapsed
+ def collapsed=(v)
+ Axlsx.validate_boolean(v)
+ @collapsed = v
+ end
+
+ # @see Col#hidden
+ def hidden=(v)
+ Axlsx.validate_boolean(v)
+ @hidden = v
+ end
+
+ # @see Col#outline
+ def outlineLevel=(v)
+ Axlsx.validate_boolean(v)
+ @outlineLevel = v
+ end
+
+ # @see Col#phonetic
+ def phonetic=(v)
+ Axlsx.validate_boolean(v)
+ @phonetic = v
+ end
+
+ # @see Col#style
+ def style=(v)
+ Axlsx.validate_unsigned_int(v)
+ @style = v
+ end
+
+ # @see Col#width
+ def width=(v)
+ Axlsx.validate_unsigned_numeric(v)
+ @customWidth = @bestFit = true
+ @width = v
+ end
+
+ # Create a new Col objects
+ # @param min First column affected by this 'column info' record.
+ # @param max Last column affected by this 'column info' record.
+ # @option options [Boolean] collapsed see Col#collapsed
+ # @option options [Boolean] hidden see Col#hidden
+ # @option options [Boolean] outlineLevel see Col#outlineLevel
+ # @option options [Boolean] phonetic see Col#phonetic
+ # @option options [Integer] style see Col#style
+ # @option options [Numeric] width see Col#width
+ def initialize(min, max, options={})
+ Axlsx.validate_unsigned_int(max)
+ Axlsx.validate_unsigned_int(min)
+ @min = min
+ @max = max
+ options.each do |o|
+ self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}="
+ end
+ end
+
+ # Serialize this columns data to an xml string
+ # @return [String]
+ def to_xml_string(str = '')
+ attrs = self.attribute_values.reject{ |key, value| value == nil }
+ str << '<col ' << attrs.map { |key, value| "#{key}='#{value}' " }.join << '/>'
+ end
+
+ end
+end
diff --git a/test/workbook/worksheet/tc_col.rb b/test/workbook/worksheet/tc_col.rb
new file mode 100644
index 00000000..b28e26e9
--- /dev/null
+++ b/test/workbook/worksheet/tc_col.rb
@@ -0,0 +1,59 @@
+require 'tc_helper.rb'
+
+class TestCol < Test::Unit::TestCase
+
+ def setup
+ @col = Axlsx::Col.new 1, 1
+ end
+
+ def test_min_max_required
+ assert_raise(ArgumentError, 'min and max must be specified when creating a new column') { Axlsx::Col.new }
+ assert_raise(ArgumentError, 'min and max must be specified when creating a new column') { Axlsx::Col.new nil, nil }
+ assert_nothing_raised { Axlsx::Col.new 1, 1 }
+ end
+
+ def test_bestFit
+ assert_equal(@col.bestFit, nil)
+ assert_raise(NoMethodError, 'bestFit is read only') { @col.bestFit = 'bob' }
+ @col.width = 1.999
+ assert_equal(@col.bestFit, true, 'bestFit should be true when width has been set')
+ end
+
+ def test_collapsed
+ assert_equal(@col.collapsed, nil)
+ assert_raise(ArgumentError, 'collapsed must be boolean(ish)') { @col.collapsed = 'bob' }
+ assert_nothing_raised('collapsed must be boolean(ish)') { @col.collapsed = true }
+ end
+
+ def test_customWidth
+ assert_equal(@col.customWidth, nil)
+ @col.width = 3
+ assert_raise(NoMethodError, 'customWidth is read only') { @col.customWidth = 3 }
+ assert_equal(@col.customWidth, true, 'customWidth is true when width is set')
+ end
+
+ def test_hidden
+ assert_equal(@col.hidden, nil)
+ assert_raise(ArgumentError, 'hidden must be boolean(ish)') { @col.hidden = 'bob' }
+ assert_nothing_raised(ArgumentError, 'hidden must be boolean(ish)') { @col.hidden = true }
+ end
+
+ def test_outlineLevel
+ assert_equal(@col.outlineLevel, nil)
+ assert_raise(ArgumentError, 'outline level cannot be negative') { @col.outlineLevel = -1 }
+ assert_raise(ArgumentError, 'outline level cannot be greater than 7') { @col.outlineLevel = 8 }
+ assert_nothing_raised('can set outlineLevel') { @col.outlineLevel = 1 }
+ end
+
+ def test_phonetic
+ assert_equal(@col.phonetic, nil)
+ assert_raise(ArgumentError, 'phonetic must be boolean(ish)') { @col.phonetic = 'bob' }
+ assert_nothing_raised(ArgumentError, 'phonetic must be boolean(ish)') { @col.phonetic = true }
+ end
+
+ def test_style
+ assert_equal(@col.style, nil)
+ #TODO check that the style specified is actually in the styles xfs collection
+ end
+
+end