lib/axlsx/workbook/worksheet/cell.rb in axlsx-1.0.18 vs lib/axlsx/workbook/worksheet/cell.rb in axlsx-1.1.0

- old
+ new

@@ -1,6 +1,7 @@ # encoding: UTF-8 +require 'cgi' 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,16 +24,20 @@ # @see Worksheet#add_row class Cell # An array of available inline styes. + # TODO change this to a hash where each key defines attr name and validator (and any info the validator requires) + # then move it out to a module so we can re-use in in other classes. + # needs to define bla=(v) and bla methods on the class that hook into a + # set_attr method that kicks the suplied validator and updates the instance_variable + # for the key INLINE_STYLES = ['value', 'type', 'font_name', 'charset', 'family', 'b', 'i', 'strike','outline', 'shadow', 'condense', 'extend', 'u', 'vertAlign', 'sz', 'color', 'scheme'] - # The index of the cellXfs item to be applied to this cell. # @return [Integer] # @see Axlsx::Styles attr_reader :style @@ -67,103 +72,118 @@ 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? + @is_text_run ||= false + end + + # The inline font_name property for the cell # @return [String] attr_reader :font_name # @see font_name - def font_name=(v) Axlsx::validate_string(v); @font_name = v; end + def font_name=(v) set_run_style :validate_string, :font_name, v; end # The inline charset property for the cell # @return [String] attr_reader :charset # @see charset - def charset=(v) Axlsx::validate_unsigned_int(v); @charset = v; end + def charset=(v) set_run_style :validate_unsigned_int, :charset, v; end # The inline family property for the cell # @return [String] attr_reader :family # @see family - def family=(v) Axlsx::validate_string(v); @family = v; end + def family=(v) set_run_style :validate_string, :family, v; end # The inline bold property for the cell # @return [Boolean] attr_reader :b # @see b - def b=(v) Axlsx::validate_boolean(v); @b = v; end + def b=(v) set_run_style :validate_boolean, :b, v; end # The inline italic property for the cell # @return [Boolean] attr_reader :i # @see i - def i=(v) Axlsx::validate_boolean(v); @i = v; end + def i=(v) set_run_style :validate_boolean, :i, v; end # The inline strike property for the cell # @return [Boolean] attr_reader :strike # @see strike - def strike=(v) Axlsx::validate_boolean(v); @strike = v; end + def strike=(v) set_run_style :validate_boolean, :strike, v; end # The inline outline property for the cell # @return [Boolean] attr_reader :outline # @see outline - def outline=(v) Axlsx::validate_boolean(v); @outline = v; end + def outline=(v) set_run_style :validate_boolean, :outline, v; end # The inline shadow property for the cell # @return [Boolean] attr_reader :shadow # @see shadow - def shadow=(v) Axlsx::validate_boolean(v); @shadow = v; end + def shadow=(v) set_run_style :validate_boolean, :shadow, v; end # The inline condense property for the cell # @return [Boolean] attr_reader :condense # @see condense - def condense=(v) Axlsx::validate_boolean(v); @condense = v; end + def condense=(v) set_run_style :validate_boolean, :condense, v; end # The inline extend property for the cell # @return [Boolean] attr_reader :extend # @see extend - def extend=(v) Axlsx::validate_boolean(v); @extend = v; end + def extend=(v) set_run_style :validate_boolean, :extend, v; end # The inline underline property for the cell # @return [Boolean] attr_reader :u # @see u - def u=(v) Axlsx::validate_boolean(v); @u = v; end + def u=(v) set_run_style :validate_boolean, :u, v; end # The inline color property for the cell # @return [Color] attr_reader :color # @param [String] The 8 character representation for an rgb color #FFFFFFFF" def color=(v) @color = v.is_a?(Color) ? v : Color.new(:rgb=>v) + @has_run_style = true end # The inline sz property for the cell # @return [Boolean] attr_reader :sz # @see sz - def sz=(v) Axlsx::validate_unsigned_int(v); @sz = v; end + def sz=(v) set_run_style :validate_unsigned_int, :sz, v; end # The inline vertical alignment property for the cell # this must be one of [:baseline, :subscript, :superscript] # @return [Symbol] attr_reader :vertAlign # @see vertAlign - def vertAlign=(v) RestrictionValidator.validate "Cell.vertAlign", [:baseline, :subscript, :superscript], v; @vertAlign = v; end + def vertAlign=(v) + RestrictionValidator.validate "Cell.vertAlign", [:baseline, :subscript, :superscript], v + set_run_style nil, :vertAlign, v + end # The inline scheme property for the cell # this must be one of [:none, major, minor] # @return [Symbol] attr_reader :scheme # @see scheme - def scheme=(v) RestrictionValidator.validate "Cell.schema", [:none, :major, :minor], v; @scheme = v; end + def scheme=(v) + RestrictionValidator.validate "Cell.schema", [:none, :major, :minor], v + set_run_style nil, :scheme, 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. @@ -200,39 +220,35 @@ # @return [Integer] attr_reader :ssti # equality comparison to test value, type and inline style attributes # this is how we work out if the cell needs to be added or already exists in the shared strings table - def shareable(v) - - #using reject becase 1.8.7 select returns an array... - v_hash = v.instance_values.reject { |key, val| !INLINE_STYLES.include?(key) } - self_hash = self.instance_values.reject { |key, val| !INLINE_STYLES.include?(key) } - # required as color is an object, and the comparison will fail even though both use the same color. - v_hash['color'] = v_hash['color'].instance_values if v_hash['color'] + def shareable_hash + self_hash = {} + INLINE_STYLES.each { |style| self_hash[style] = self.instance_variable_get("@" + style) } self_hash['color'] = self_hash['color'].instance_values if self_hash['color'] - - v_hash == self_hash + self_hash end # @return [Integer] The index of the cell in the containing row. def index @row.cells.index(self) end # @return [String] The alpha(column)numeric(row) reference for this sell. # @example Relative Cell Reference # ws.rows.first.cells.first.r #=> "A1" + # @note this will be discontinued in 1.1.0 - prefer Axlsx.cell_r def r - "#{col_ref}#{@row.index+1}" + "#{Axlsx::col_ref(index)}#{@row.index+1}" end # @return [String] The absolute alpha(column)numeric(row) reference for this sell. # @example Absolute Cell Reference # ws.rows.first.cells.first.r #=> "$A$1" def r_abs - "$#{r.split('').join('$')}" + "$#{r.match(%r{([A-Z]+)([0-9]+)})[1,2].join('$')}" end # @return [Integer] The cellXfs item index applied to this cell. # @raise [ArgumentError] Invalid cellXfs id if the value provided is not within cellXfs items range. def style=(v) @@ -257,104 +273,89 @@ target.r end self.row.worksheet.merge_cells "#{self.r}:#{range_end}" unless range_end.nil? end - # builds an xml text run based on this cells attributes. This is extracted from to_xml so that shared strings can use it. - # @param [Nokogiri::XML::Builder] xml The document builder instance this output will be added to. - # @return [String] the xml for this cell's text run - def run_xml(xml) - if (self.instance_values.keys & INLINE_STYLES).size > 0 - xml.r { - xml.rPr { - xml.rFont(:val=>@font_name) if @font_name - xml.charset(:val=>@charset) if @charset - xml.family(:val=>@family) if @family - xml.b(:val=>@b) if @b - xml.i(:val=>@i) if @i - xml.strike(:val=>@strike) if @strike - xml.outline(:val=>@outline) if @outline - xml.shadow(:val=>@shadow) if @shadow - xml.condense(:val=>@condense) if @condense - xml.extend(:val=>@extend) if @extend - @color.to_xml(xml) if @color - xml.sz(:val=>@sz) if @sz - xml.u(:val=>@u) if @u - # :baseline, :subscript, :superscript - xml.vertAlign(:val=>@vertAlign) if @vertAlign - # :none, major, :minor - xml.scheme(:val=>@scheme) if @scheme - } - xml.t @value.to_s - } + # builds an xml text run based on this cells attributes. + # @param [String] str The string instance this run will be concated to. + # @return [String] + def run_xml_string(str = '') + if is_text_run? + data = self.instance_values.reject{|key, value| value == nil } + keys = data.keys & INLINE_STYLES + keys.delete ['value', 'type'] + str << "<r><rPr>" + keys.each do |key| + case key + when 'font_name' + str << "<rFont val='"<< @font_name << "'/>" + when 'color' + str << data[key].to_xml_string + else + "<" << key.to_s << " val='" << data[key].to_s << "'/>" + end + end + str << "</rPr>" << "<t>" << value.to_s << "</t></r>" else - xml.t @value.to_s + str << "<t>" << value.to_s << "</t>" end + str end # Serializes the cell - # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to. + # @param [Integer] r_index The row index for the cell + # @param [Integer] c_index The cell index in the row. + # @param [String] str The string index the cell content will be appended to. Defaults to empty string. # @return [String] xml text for the cell - def to_xml(xml) - if @type == :string + def to_xml_string(r_index, c_index, str = '') + return str if @value.nil? + str << '<c r="' << Axlsx::cell_r(c_index, r_index) << '" s="' << @style.to_s << '" ' + case @type + when :string #parse formula if @value.start_with?('=') - xml.c(:r => r, :t=>:str, :s=>style) { - xml.f @value.to_s.gsub('=', '') - } + str << 't="str"><f>' << @value.to_s.gsub('=', '') << '</f>' else #parse shared if @ssti - xml.c(:r => r, :s=>style, :t => :s) { xml.v ssti } + str << 't="s"><v>' << @ssti.to_s << '</v>' else - #parse inline string - xml.c(:r => r, :s=>style, :t => :inlineStr) { - xml.is { - run_xml(xml) - } - } + str << 't="inlineStr"><is>' << run_xml_string << '</is>' end end - elsif @type == :date + when :date # TODO: See if this is subject to the same restriction as Time below - v = DateTimeConverter::date_to_serial @value - xml.c(:r => r, :s => style) { xml.v v } - elsif @type == :time - v = DateTimeConverter::time_to_serial @value - xml.c(:r => r, :s => style) { xml.v v } - elsif @type == :boolean - xml.c(:r => r, :s => style, :t => :b) { xml.v value } + str << '><v>' << DateTimeConverter::date_to_serial(@value).to_s << '</v>' + when :time + str << '><v>' << DateTimeConverter::time_to_serial(@value).to_s << '</v>' + when :boolean + str << 't="b"><v>' << @value.to_s << '</v>' else - xml.c(:r => r, :s => style) { xml.v value } + str << '><v>' << @value.to_s << '</v>' end + str << '</c>' end private + # Utility method for setting inline style attributes + def set_run_style( validator, attr, value) + return unless INLINE_STYLES.include?(attr.to_s) + Axlsx.send(validator, value) unless validator == nil + self.instance_variable_set :"@#{attr.to_s}", value + @is_text_run = true + end + # @see ssti def ssti=(v) Axlsx::validate_unsigned_int(v) @ssti = v end # assigns the owning row for this cell. def row=(v) DataTypeValidator.validate "Cell.row", Row, v; @row=v end - # converts the column index into alphabetical values. - # @note This follows the standard spreadsheet convention of naming columns A to Z, followed by AA to AZ etc. - # @return [String] - def col_ref - chars = [] - index = self.index - while index >= 26 do - chars << ((index % 26) + 65).chr - index /= 26 - end - chars << ((chars.empty? ? index : index-1) + 65).chr - chars.reverse.join - end - # Determines the cell type based on the cell value. # @note This is only used when a cell is created but no :type option is specified, the following rules apply: # 1. If the value is an instance of Date, the type is set to :date # 2. If the value is an instance of Time, the type is set to :time # 3. If the value is an instance of TrueClass or FalseClass, the type is set to :boolean @@ -380,10 +381,11 @@ # Cast the value into this cells data type. # @note # About Time - Time in OOXML is *different* from what you might expect. The history as to why is interesting, but you can safely assume that if you are generating docs on a mac, you will want to specify Workbook.1904 as true when using time typed values. # @see Axlsx#date1904 def cast_value(v) + return nil if v.nil? if @type == :date self.style = STYLE_DATE if self.style == 0 v elsif (@type == :time && v.is_a?(Time)) || (@type == :time && v.respond_to?(:to_time)) self.style = STYLE_DATE if self.style == 0 @@ -394,10 +396,10 @@ v.to_i elsif @type == :boolean v ? 1 : 0 else @type = :string - v.to_s + ::CGI.escapeHTML(v.to_s) end end end end