Class: RubyXL::Workbook

Inherits:
Object
  • Object
show all
Defined in:
lib/workbook/readers/xlsx_reader.rb,
lib/workbook/readers/xlsx_reader.rb

Instance Method Summary (collapse)

Instance Method Details

- (Boolean) is_date_format?(num_fmt)

Improves upon date format detection

Returns:

  • (Boolean)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
# File 'lib/workbook/readers/xlsx_reader.rb', line 9

def is_date_format?(num_fmt)
  num_fmt.downcase!
  skip_chars = ['$', '-', '+', '/', '(', ')', ':', ' ']
  num_chars = ['0', '#', '?']
  non_date_formats = ['0.00e+00', '##0.0e+0', 'general', '@']
  date_chars = ['y','m','d','h','s']

  state = 0
  s = ''
  num_fmt.split(//).each do |c|
    if state == 0
      if c == '"'
        state = 1
      elsif ['\\', '_', '*'].include?(c)
        state = 2
      elsif skip_chars.include?(c)
        next
      else
        s << c
      end
    elsif state == 1
      if c == '"'
        state = 0
      end
    elsif state == 2
      state = 0
    end
  end
  s.gsub!(/\[[^\]]*\]/, '')
  if non_date_formats.include?(s)
    return false
  end
  separator = ';'
  got_sep = 0
  date_count = 0
  num_count = 0
  s.split(//).each do |c|
    if date_chars.include?(c)
      date_count += 1
    elsif num_chars.include?(c)
      num_count += 1
    elsif c == separator
      got_sep = 1
    end
  end
  if date_count > 0 && num_count == 0
    return true
  elsif num_count > 0 && date_count == 0
    return false
  elsif date_count
    # ambiguous result
  elsif got_sep == 0
    # constant result
  end
  return date_count > num_count
end

- (Object) num_fmts_by_id



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/workbook/readers/xlsx_reader.rb', line 71

def num_fmts_by_id
  return @num_fmts_hash unless @num_fmts_hash.nil?
  @num_fmts_hash={1=>{:attributes=>{:formatCode=>'0'}},
2=>{:attributes=>{:formatCode=>'0.00'}},
3=>{:attributes=>{:formatCode=>'#, ##0'}},
4=>{:attributes=>{:formatCode=>'#, ##0.00'}},
5=>{:attributes=>{:formatCode=>'$#, ##0_);($#, ##0)'}},
6=>{:attributes=>{:formatCode=>'$#, ##0_);[Red]($#, ##0)'}},
7=>{:attributes=>{:formatCode=>'$#, ##0.00_);($#, ##0.00)'}},
8=>{:attributes=>{:formatCode=>'$#, ##0.00_);[Red]($#, ##0.00)'}},
9=>{:attributes=>{:formatCode=>'0%'}},
10=>{:attributes=>{:formatCode=>'0.00%'}},
11=>{:attributes=>{:formatCode=>'0.00E+00'}},
12=>{:attributes=>{:formatCode=>'# ?/?'}},
13=>{:attributes=>{:formatCode=>'# ??/??'}},
14=>{:attributes=>{:formatCode=>'m/d/yyyy'}},
15=>{:attributes=>{:formatCode=>'d-mmm-yy'}},
16=>{:attributes=>{:formatCode=>'d-mmm'}},
17=>{:attributes=>{:formatCode=>'mmm-yy'}},
18=>{:attributes=>{:formatCode=>'h:mm AM/PM'}},
19=>{:attributes=>{:formatCode=>'h:mm:ss AM/PM'}},
20=>{:attributes=>{:formatCode=>'h:mm'}},
21=>{:attributes=>{:formatCode=>'h:mm:ss'}},
22=>{:attributes=>{:formatCode=>'m/d/yyyy h:mm'}},
37=>{:attributes=>{:formatCode=>'#, ##0_);(#, ##0)'}},
38=>{:attributes=>{:formatCode=>'#, ##0_);[Red](#, ##0)'}},
39=>{:attributes=>{:formatCode=>'#, ##0.00_);(#, ##0.00)'}},
40=>{:attributes=>{:formatCode=>'#, ##0.00_);[Red](#, ##0.00)'}},
45=>{:attributes=>{:formatCode=>'mm:ss'}},
46=>{:attributes=>{:formatCode=>'[h]:mm:ss'}},
47=>{:attributes=>{:formatCode=>'mm:ss.0'}},
48=>{:attributes=>{:formatCode=>'##0.0E+0'}},
49=>{:attributes=>{:formatCode=>'@'}}}
  if num_fmts and num_fmts[:numFmt]
    num_fmts[:numFmt].each do |num_fmt|
      @num_fmts_hash[num_fmt[:attributes][:numFmtId]]=num_fmt
    end
  end
  return @num_fmts_hash
end