lib/axlsx/workbook/worksheet/cell.rb in axlsx-2.1.0.pre vs lib/axlsx/workbook/worksheet/cell.rb in axlsx-3.0.0.pre

- old
+ new

@@ -35,19 +35,19 @@ # Do not use instance vars if not needed to use less RAM # And do not call parse_options on frequently used options # to get less GC cycles type = options.delete(:type) || cell_type_from_value(value) self.type = type unless type == :string - + val = options.delete(:style) self.style = val unless val.nil? || val == 0 val = options.delete(:formula_value) self.formula_value = val unless val.nil? - - parse_options(options) + parse_options(options) + self.value = value value.cell = self if contains_rich_text? end # this is the cached value for formula cells. If you want the values to render in iOS/Mac OSX preview @@ -62,13 +62,14 @@ # for the key INLINE_STYLES = [:value, :type, :font_name, :charset, :family, :b, :i, :strike, :outline, :shadow, :condense, :extend, :u, :vertAlign, :sz, :color, :scheme].freeze - + + # An array of valid cell types CELL_TYPES = [:date, :time, :float, :integer, :richtext, - :string, :boolean, :iso_8601].freeze + :string, :boolean, :iso_8601, :text].freeze # The index of the cellXfs item to be applied to this cell. # @return [Integer] # @see Axlsx::Styles def style @@ -77,11 +78,11 @@ # The row this cell belongs to. # @return [Row] attr_reader :row - # The cell's data type. Currently only six types are supported, :date, :time, :float, :integer, :string and :boolean. + # The cell's data type. # Changing the type for a cell will recast the value into that type. If no type option is specified in the constructor, the type is # automatically determed. # @see Cell#cell_type_from_value # @return [Symbol] The type of data this cell's value is cast to. # @raise [ArgumentExeption] Cell.type must be one of [:date, time, :float, :integer, :string, :boolean] @@ -91,41 +92,41 @@ # :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. def type defined?(@type) ? @type : :string end - + # @see type def type=(v) RestrictionValidator.validate :cell_type, CELL_TYPES, v @type = v self.value = @value unless !defined?(@value) || @value.nil? end # The value of this cell. # @return [String, Integer, Float, Time, Boolean] casted value based on cell's type attribute. attr_reader :value - + # @see value def value=(v) #TODO: consider doing value based type determination first? @value = cast_value(v) end # Indicates that the cell has one or more of the custom cell styles applied. # @return [Boolean] def is_text_run? defined?(@is_text_run) && @is_text_run && !contains_rich_text? - end - + end + def contains_rich_text? type == :richtext end - + # Indicates if the cell is good for shared string table def plain_string? - type == :string && # String typed + (type == :string || type == :text) && # String typed !is_text_run? && # No inline styles !@value.nil? && # Not nil !@value.empty? && # Not empty !@value.start_with?(?=) # Not a formula end @@ -345,11 +346,13 @@ @name = label end # returns the name of the cell attr_reader :name - + + # Attempts to determine the correct width for this cell's content + # @return [Float] def autowidth return if is_formula? || value.nil? if contains_rich_text? string_width('', font_size) + value.autowidth elsif styles.cellXfs[style].alignment && styles.cellXfs[style].alignment.wrap_text @@ -361,34 +364,34 @@ max_width else string_width(value, font_size) end end - + # Returns the sanatized value # TODO find a better way to do this as it accounts for 30% of # processing time in benchmarking... def clean_value - if type == :string && !Axlsx::trust_input + if (type == :string || type == :text) && !Axlsx::trust_input Axlsx::sanitize(::CGI.escapeHTML(@value.to_s)) else @value.to_s end end private - + def styles row.worksheet.styles end - + # Returns the width of a string according to the current style # This is still not perfect... # - scaling is not linear as font sizes increase def string_width(string, font_size) font_scale = font_size / 10.0 - (string.to_s.count(Worksheet::THIN_CHARS) + 3.0) * (font_size/10.0) + (string.to_s.count(Worksheet::THIN_CHARS) + 3.0) * font_scale end # we scale the font size if bold style is applied to either the style font or # the cell itself. Yes, it is a bit of a hack, but it is much better than using # imagemagick and loading metrics for every character. @@ -450,10 +453,14 @@ when :date self.style = STYLE_DATE if self.style == 0 v when :time self.style = STYLE_DATE if self.style == 0 - v.respond_to?(:to_time) ? v.to_time : v + if !v.is_a?(Time) && v.respond_to?(:to_time) + v.to_time + else + v + end when :float v.to_f when :integer v.to_i when :boolean