lib/axlsx/workbook/worksheet/cell.rb in axlsx-1.0.8 vs lib/axlsx/workbook/worksheet/cell.rb in axlsx-1.0.9

- old
+ new

@@ -1,5 +1,6 @@ +# -*- coding: utf-8 -*- module Axlsx # A cell in a worksheet. # Cell stores inforamation requried to serialize a single worksheet cell to xml. You must provde the Row that the cell belongs to and the cells value. The data type will automatically be determed if you do not specify the :type option. The default style will be applied if you do not supply the :style option. Changing the cell's type will recast the value to the type specified. Altering the cell's value via the property accessor will also automatically cast the provided value to the cell's type. # @example Manually creating and manipulating Cell objects # ws = Workbook.new.add_worksheet @@ -23,11 +24,11 @@ class Cell # The index of the cellXfs item to be applied to this cell. # @return [Integer] # @see Axlsx::Styles - attr_accessor :style + attr_reader :style # The row this cell belongs to. # @return [Row] attr_reader :row @@ -40,17 +41,29 @@ # @note # If the value provided cannot be cast into the type specified, type is changed to :string and the following logic is applied. # :string to :integer or :float, type coversions always return 0 or 0.0 # :string, :integer, or :float to :time conversions always return the original value as a string and set the cells type to :string. # No support is currently implemented for parsing time strings. - # - attr_accessor :type + attr_reader :type + # @see type + def type=(v) + RestrictionValidator.validate "Cell.type", [:time, :float, :integer, :string], v + @type=v + self.value = @value + end + # The value of this cell. # @return casted value based on cell's type attribute. - attr_accessor :value + attr_reader :value + # @see value + def value=(v) + #TODO: consider doing value based type determination first? + @value = cast_value(v) + end + # @param [Row] row The row this cell belongs to. # @param [Any] value The value associated with this cell. # @option options [Symbol] type The intended data type for this cell. If not specified the data type will be determined internally based on the vlue provided. # @option options [Integer] style The index of the cellXfs item to be applied to this cell. If not specified, the default style (0) will be applied. def initialize(row, value="", options={}) @@ -89,33 +102,29 @@ count = @styles.cellXfs.size raise ArgumentError, "Invalid cellXfs id" unless v < count @style = v end - def type=(v) - RestrictionValidator.validate "Cell.type", [:time, :float, :integer, :string], v - @type=v - self.value = @value - end - def value=(v) - #TODO: consider doing value based type determination first? - @value = cast_value(v) - end # Serializes the cell # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to. # @return [String] xml text for the cell # @note # Shared Strings are not used in this library. All values are set directly in the each sheet. def to_xml(xml) - if @type == :string - #NOTE not sure why, but xml.t @v renders the text as html entities of unicode data - xml.c(:r => r, :t=>:inlineStr, :s=>style) { xml.is { xml << "<t>#{value}</t>" } } + # Both 1.8 and 1.9 return the same 'fast_xf' + # &#12491;&#12507;&#12531;&#12468; + # &#12491;&#12507;&#12531;&#12468; + + # however nokogiri does a nice 'force_encoding' which we shall remove! + if @type == :string + xml.c(:r => r, :t=>:inlineStr, :s=>style) { xml.is { xml.t @value.to_s } } else xml.c(:r => r, :s => style) { xml.v value } end end + private # assigns the owning row for this cell. def row=(v) DataTypeValidator.validate "Cell.row", Row, v; @row=v end