rubyXL 1.2.1 → 1.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.2.1
1
+ 1.2.2
@@ -30,7 +30,7 @@ module RubyXL
30
30
  if @workbook.num_fmts
31
31
  num_fmt_id = xf_id()[:numFmtId]
32
32
  num_fmt = @workbook.num_fmts[:numFmt].select { |f| f[:attributes][:numFmtId] == num_fmt_id }[0].andand[:attributes].andand[:formatCode]
33
- if num_fmt && is_date_format?(num_fmt)
33
+ if num_fmt && workbook.date_num_fmt?(num_fmt)
34
34
  return true
35
35
  end
36
36
  end
@@ -7,22 +7,29 @@ module RubyXL
7
7
 
8
8
  class Parser
9
9
  attr_reader :data_only, :num_sheets
10
-
10
+ @@parsed_column_hash ={}
11
11
  # converts cell string (such as "AA1") to matrix indices
12
12
  def Parser.convert_to_index(cell_string)
13
13
  index = Array.new(2)
14
14
  index[0]=-1
15
15
  index[1]=-1
16
16
  if(cell_string =~ /^([A-Z]+)(\d+)$/)
17
- one = $1.to_s
18
- row = Integer($2) - 1 #-1 for 0 indexing
17
+
18
+ one = $1
19
+ row = $2.to_i - 1 #-1 for 0 indexing
19
20
  col = 0
20
21
  i = 0
21
- one = one.reverse #because of 26^i calculation
22
- one.each_byte do |c|
23
- int_val = c - 64 #converts A to 1
24
- col += int_val * 26**(i)
25
- i=i+1
22
+ if @@parsed_column_hash[one].nil?
23
+ puts "||#{one}||"
24
+ two = one.reverse #because of 26^i calculation
25
+ two.each_byte do |c|
26
+ int_val = c - 64 #converts A to 1
27
+ col += int_val * 26**(i)
28
+ i=i+1
29
+ end
30
+ @@parsed_column_hash[one] = col
31
+ else
32
+ col = @@parsed_column_hash[one]
26
33
  end
27
34
  col -= 1 #zer0 index
28
35
  index[0] = row
@@ -13,11 +13,13 @@ require 'date'
13
13
 
14
14
  module RubyXL
15
15
  class Workbook
16
+ include Enumerable
16
17
  attr_accessor :worksheets, :filepath, :creator, :modifier, :created_at,
17
18
  :modified_at, :company, :application, :appversion, :num_fmts, :fonts, :fills,
18
19
  :borders, :cell_xfs, :cell_style_xfs, :cell_styles, :shared_strings, :calc_chain,
19
20
  :num_strings, :size, :date1904, :external_links, :style_corrector, :drawings,
20
- :worksheet_rels, :printer_settings, :macros, :colors, :shared_strings_XML, :defined_names
21
+ :worksheet_rels, :printer_settings, :macros, :colors, :shared_strings_XML, :defined_names, :column_lookup_hash
22
+
21
23
 
22
24
  APPLICATION = 'Microsoft Macintosh Excel'
23
25
  APPVERSION = '12.0000'
@@ -57,6 +59,7 @@ module RubyXL
57
59
  @colors = nil
58
60
  @shared_strings_XML = nil
59
61
  @defined_names = nil
62
+ @column_lookup_hash = {}
60
63
 
61
64
  begin
62
65
  @created_at = DateTime.parse(created_at).strftime('%Y-%m-%dT%TZ')
@@ -75,6 +78,10 @@ module RubyXL
75
78
  return worksheets[worksheet]
76
79
  end
77
80
 
81
+ def each
82
+ worksheets.each{|i| yield i}
83
+ end
84
+
78
85
  #filepath of xlsx file (including file itself)
79
86
  def write(filepath=@filepath)
80
87
  validate_before_write
@@ -214,6 +221,70 @@ module RubyXL
214
221
  compare_date - 1 + num
215
222
  end
216
223
 
224
+ def date_num_fmt?(num_fmt)
225
+ @num_fmt_date_hash ||= {}
226
+ if @num_fmt_date_hash[num_fmt].nil?
227
+ @num_fmt_date_hash[num_fmt] = is_date_format?(num_fmt)
228
+ end
229
+ return @num_fmt_date_hash[num_fmt]
230
+ end
231
+
232
+ def is_date_format?(num_fmt)
233
+ skip_chars = ['$', '-', '+', '/', '(', ')', ':', ' ']
234
+ num_chars = ['0', '#', '?']
235
+ non_date_formats = ['0.00E+00', '##0.0E+0', 'General', 'GENERAL', 'general', '@']
236
+ date_chars = ['y','m','d','h','s']
237
+
238
+ state = 0
239
+ s = ''
240
+ num_fmt.split(//).each do |c|
241
+ if state == 0
242
+ if c == '"'
243
+ state = 1
244
+ elsif ['\\', '_', '*'].include?(c)
245
+ state = 2
246
+ elsif skip_chars.include?(c)
247
+ next
248
+ else
249
+ s << c
250
+ end
251
+ elsif state == 1
252
+ if c == '"'
253
+ state = 0
254
+ end
255
+ elsif state == 2
256
+ state = 0
257
+ end
258
+ end
259
+ s.gsub!(/\[[^\]]*\]/, '')
260
+ if non_date_formats.include?(s)
261
+ return false
262
+ end
263
+ separator = ';'
264
+ got_sep = 0
265
+ date_count = 0
266
+ num_count = 0
267
+ s.split(//).each do |c|
268
+ if date_chars.include?(c)
269
+ date_count += 1
270
+ elsif num_chars.include?(c)
271
+ num_count += 1
272
+ elsif c == separator
273
+ got_sep = 1
274
+ end
275
+ end
276
+ if date_count > 0 && num_count == 0
277
+ return true
278
+ elsif num_count > 0 && date_count == 0
279
+ return false
280
+ elsif date_count
281
+ # ambiguous result
282
+ elsif got_sep == 0
283
+ # constant result
284
+ end
285
+ return date_count > num_count
286
+ end
287
+
217
288
  #gets style object from style array given index
218
289
  def get_style(style_index)
219
290
  if !@cell_xfs[:xf].is_a?Array
@@ -1,5 +1,6 @@
1
1
  module RubyXL
2
2
  class Worksheet < PrivateClass
3
+ include Enumerable
3
4
 
4
5
  attr_accessor :sheet_name, :sheet_data, :cols, :merged_cells, :pane,
5
6
  :validations, :sheet_view, :legacy_drawing, :extLst, :workbook, :row_styles
@@ -26,6 +27,10 @@ class Worksheet < PrivateClass
26
27
  return @sheet_data[row]
27
28
  end
28
29
 
30
+ def each
31
+ @sheet_data.each {|i| yield i}
32
+ end
33
+
29
34
  #returns 2d array of just the cell values (without style or formula information)
30
35
  def extract_data
31
36
  return @sheet_data.map {|row| row.map {|c| if c.is_a?(Cell) then c.value else nil end}}
@@ -51,6 +56,8 @@ class Worksheet < PrivateClass
51
56
  header_row.each_with_index do |header_cell, index|
52
57
  next if header_cell.nil? || header_cell.value.nil?
53
58
  header = header_cell.value.to_s
59
+ table_hash[:sorted_headers]||=[]
60
+ table_hash[:sorted_headers] << header
54
61
  table_hash[header] = []
55
62
 
56
63
  original_row = row_num + 1
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{rubyXL}
8
- s.version = "1.2.1"
8
+ s.version = "1.2.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Vivek Bhagwat"]
12
- s.date = %q{2012-01-11}
12
+ s.date = %q{2012-01-13}
13
13
  s.description = %q{rubyXL is a gem which allows the parsing, creation, and manipulation of Microsoft Excel (.xlsx/.xlsm) Documents}
14
14
  s.email = %q{bhagwat.vivek@gmail.com}
15
15
  s.extra_rdoc_files = [
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubyXL
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 1
8
8
  - 2
9
- - 1
9
+ - 2
10
10
  segments_generated: true
11
- version: 1.2.1
11
+ version: 1.2.2
12
12
  platform: ruby
13
13
  authors:
14
14
  - Vivek Bhagwat
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2012-01-11 00:00:00 -05:00
19
+ date: 2012-01-13 00:00:00 -05:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency