From 30621f2ce9c88b242524929b184dfeebf89181aa Mon Sep 17 00:00:00 2001 From: Randy Morgan Date: Wed, 28 Mar 2012 23:31:38 +0900 Subject: doc fix --- lib/axlsx/util/validators.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'lib') diff --git a/lib/axlsx/util/validators.rb b/lib/axlsx/util/validators.rb index ce810e54..d4913074 100644 --- a/lib/axlsx/util/validators.rb +++ b/lib/axlsx/util/validators.rb @@ -5,11 +5,11 @@ module Axlsx # Perform validation # @param [String] name The name of what is being validatied. This is included in the error message # @param [Array] choices The list of choices to validate against - # @param [Any] v The value to be validated + # @param [Any] v The value to be validated # @raise [ArgumentError] Raised if the value provided is not in the list of choices. # @return [Boolean] true if validation succeeds. def self.validate(name, choices, v) - raise ArgumentError, (ERR_RESTRICTION % [v.to_s, name, choices.inspect]) unless choices.include?(v) + raise ArgumentError, (ERR_RESTRICTION % [v.to_s, name, choices.inspect]) unless choices.include?(v) true end end @@ -55,7 +55,7 @@ module Axlsx # 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 + # @raise [ArgumentError] raised if the value is not a Fixnun, Integer, Float 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 }) @@ -68,7 +68,7 @@ module Axlsx end # Requires that the value is a form that can be evaluated as a boolean in an xml document. - # The value must be an instance of Fixnum, String, Integer, Symbol, TrueClass or FalseClass and + # The value must be an instance of Fixnum, String, Integer, Symbol, TrueClass or FalseClass and # it must be one of 0, 1, "true", "false", :true, :false, true, false, "0", or "1" # @param [Any] v The value validated def self.validate_boolean(v) @@ -79,13 +79,13 @@ module Axlsx # @param [Any] v The value validated def self.validate_string(v) DataTypeValidator.validate :string, String, v - end + end # Requires that the value is a Float # @param [Any] v The value validated def self.validate_float(v) DataTypeValidator.validate :float, Float, v - end + end # Requires that the value is valid pattern type. # valid pattern types must be one of :none, :solid, :mediumGray, :darkGray, :lightGray, :darkHorizontal, :darkVertical, :darkDown, -- cgit v1.2.3 From da054ee2f47261f70ebc18f133dc303acd810581 Mon Sep 17 00:00:00 2001 From: Randy Morgan Date: Wed, 28 Mar 2012 23:44:15 +0900 Subject: implement column object - still needs to be tied in to a rewrite of autofit_data --- lib/axlsx/workbook/workbook.rb | 1 + lib/axlsx/workbook/worksheet/col.rb | 113 ++++++++++++++++++++++++++++++++++++ test/workbook/worksheet/tc_col.rb | 59 +++++++++++++++++++ 3 files changed, 173 insertions(+) create mode 100644 lib/axlsx/workbook/worksheet/col.rb create mode 100644 test/workbook/worksheet/tc_col.rb (limited to 'lib') 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 << '' + 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 -- cgit v1.2.3