Class: Xlsxtream::Row

Inherits:
Object
  • Object
show all
Defined in:
lib/xlsxtream/row.rb

Constant Summary collapse

ENCODING =
Encoding.find('UTF-8')
NUMBER_PATTERN =
/\A-?[0-9]+(\.[0-9]+)?\z/.freeze
DATE_PATTERN =

ISO 8601 yyyy-mm-dd

/\A[0-9]{4}-[0-9]{2}-[0-9]{2}\z/.freeze
TIME_PATTERN =

ISO 8601 yyyy-mm-ddThh:mm:ss(.s)(Z|+hh:mm|-hh:mm)

/\A[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}(?::[0-9]{2}(?:\.[0-9]{1,9})?)?(?:Z|[+-][0-9]{2}:[0-9]{2})?\z/.freeze
TRUE_STRING =
'true'.freeze
FALSE_STRING =
'false'.freeze
DATE_STYLE =
1
TIME_STYLE =
2

Instance Method Summary collapse

Constructor Details

#initialize(row, rownum, options = {}) ⇒ Row

Returns a new instance of Row.



22
23
24
25
26
27
# File 'lib/xlsxtream/row.rb', line 22

def initialize(row, rownum, options = {})
  @row = row
  @rownum = rownum
  @sst = options[:sst]
  @auto_format = options[:auto_format]
end

Instance Method Details

#to_xmlObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/xlsxtream/row.rb', line 29

def to_xml
  column = String.new('A')
  xml = String.new(%Q{<row r="#{@rownum}">})

  @row.each do |value|
    cid = "#{column}#{@rownum}"
    column.next!

    if @auto_format && value.is_a?(String)
      value = auto_format(value)
    end

    case value
    when Numeric
      xml << %Q{<c r="#{cid}" t="n"><v>#{value}</v></c>}
    when TrueClass, FalseClass
      xml << %Q{<c r="#{cid}" t="b"><v>#{value ? 1 : 0}</v></c>}
    when Time
      xml << %Q{<c r="#{cid}" s="#{TIME_STYLE}"><v>#{time_to_oa_date(value)}</v></c>}
    when DateTime
      xml << %Q{<c r="#{cid}" s="#{TIME_STYLE}"><v>#{datetime_to_oa_date(value)}</v></c>}
    when Date
      xml << %Q{<c r="#{cid}" s="#{DATE_STYLE}"><v>#{date_to_oa_date(value)}</v></c>}
    else
      value = value.to_s

      unless value.empty? # no xml output for for empty strings
        value = value.encode(ENCODING) if value.encoding != ENCODING

        if @sst
          xml << %Q{<c r="#{cid}" t="s"><v>#{@sst[value]}</v></c>}
        else
          xml << %Q{<c r="#{cid}" t="inlineStr"><is><t>#{XML.escape_value(value)}</t></is></c>}
        end
      end
    end
  end

  xml << '</row>'
end