lib/axlsx/workbook/worksheet/col.rb in axlsx-1.3.1 vs lib/axlsx/workbook/worksheet/col.rb in axlsx-1.3.2
- old
+ new
@@ -2,10 +2,31 @@
module Axlsx
# The Col class defines column attributes for columns in sheets.
class Col
+ include Axlsx::OptionsParser
+ include Axlsx::SerializedAttributes
+ # 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
+ parse_options options
+ end
+
+ serializable_attributes :collapsed, :hidden, :outline_level, :phonetic, :style, :width, :min, :max, :best_fit, :custom_width
+
# First column affected by this 'column info' record.
# @return [Integer]
attr_reader :min
# Last column affected by this 'column info' record.
@@ -15,11 +36,12 @@
# 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
+ attr_reader :best_fit
+ alias :bestFit :best_fit
# Flag indicating if the outlining of the affected column(s) is in the collapsed state.
# @return [Boolean]
attr_reader :collapsed
@@ -27,11 +49,12 @@
# @return [Boolean]
attr_reader :hidden
# Outline level of affected column(s). Range is 0 to 7.
# @return [Integer]
- attr_reader :outlineLevel
+ attr_reader :outline_level
+ alias :outlineLevel :outline_level
# Flag indicating if the phonetic information should be displayed by default for the affected column(s) of the worksheet.
# @return [Boolean]
attr_reader :phonetic
@@ -42,11 +65,12 @@
# The width of the column
# @return [Numeric]
attr_reader :width
# @return [Boolean]
- attr_reader :customWidth
+ attr_reader :custom_width
+ alias :customWidth :custom_width
# @see Col#collapsed
def collapsed=(v)
Axlsx.validate_boolean(v)
@collapsed = v
@@ -57,15 +81,16 @@
Axlsx.validate_boolean(v)
@hidden = v
end
# @see Col#outline
- def outlineLevel=(v)
+ def outline_level=(v)
Axlsx.validate_unsigned_numeric(v)
raise ArgumentError, 'outlineLevel must be between 0 and 7' unless 0 <= v && v <= 7
- @outlineLevel = v
+ @outline_level = v
end
+ alias :outlineLevel= :outline_level=
# @see Col#phonetic
def phonetic=(v)
Axlsx.validate_boolean(v)
@phonetic = v
@@ -78,33 +103,14 @@
end
# @see Col#width
def width=(v)
Axlsx.validate_unsigned_numeric(v) unless v == nil
- @customWidth = @bestFit = v != nil
+ @custom_width = @best_fit = v != nil
@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
-
# 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
# to this value and the cell's attributes are ignored.
@@ -120,11 +126,12 @@
# Serialize this columns data to an xml string
# @param [String] str
# @return [String]
def to_xml_string(str = '')
- attrs = self.instance_values.reject{ |key, value| value == nil }
- str << '<col ' << attrs.map { |key, value| '' << key << '="' << value.to_s << '"' }.join(' ') << '/>'
+ str << '<col '
+ serialized_attributes str
+ str << '/>'
end
end
end