rubyXL 1.1.10 → 1.1.11

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.1.10
1
+ 1.1.11
@@ -1,7 +1,7 @@
1
1
  module RubyXL
2
2
  class Cell < PrivateClass
3
3
 
4
- attr_accessor :row, :column, :datatype, :style_index, :value, :formula, :worksheet
4
+ attr_accessor :row, :column, :datatype, :style_index, :formula, :worksheet
5
5
  attr_reader :workbook,:formula_attributes
6
6
 
7
7
  def initialize(worksheet,row,column,value=nil,formula=nil,datatype='s',style_index=0, fmla_attr={})
@@ -17,6 +17,83 @@ module RubyXL
17
17
  @formula_attributes = fmla_attr
18
18
  end
19
19
 
20
+ def value
21
+ if is_date?
22
+ return @workbook.num_to_date(@value)
23
+ else
24
+ return @value
25
+ end
26
+ end
27
+
28
+ def is_date?
29
+ if !@value.is_a?(String)
30
+ if @workbook.num_fmts
31
+ num_fmt_id = xf_id()[:numFmtId]
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)
34
+ return true
35
+ end
36
+ end
37
+ end
38
+ return false
39
+ end
40
+
41
+ def is_date_format?(num_fmt)
42
+ skip_chars = ['$', '-', '+', '/', '(', ')', ':', ' ']
43
+ num_chars = ['0', '#', '?']
44
+ non_date_formats = ['0.00E+00', '##0.0E+0', 'General', 'GENERAL', 'general', '@']
45
+ date_chars = ['y','m','d','h','s']
46
+
47
+ state = 0
48
+ s = ''
49
+ num_fmt.split(//).each do |c|
50
+ if state == 0
51
+ if c == '"'
52
+ state = 1
53
+ elsif ['\\', '_', '*'].include?(c)
54
+ state = 2
55
+ elsif skip_chars.include?(c)
56
+ next
57
+ else
58
+ s << c
59
+ end
60
+ elsif state == 1
61
+ if c == '"'
62
+ state = 0
63
+ end
64
+ elsif state == 2
65
+ state = 0
66
+ end
67
+ end
68
+ s.gsub!(/\[[^\]]*\]/, '')
69
+ if non_date_formats.include?(s)
70
+ return false
71
+ end
72
+ separator = ';'
73
+ got_sep = 0
74
+ date_count = 0
75
+ num_count = 0
76
+ s.split(//).each do |c|
77
+ if date_chars.include?(c)
78
+ date_count += 1
79
+ elsif num_chars.include?(c)
80
+ num_count += 1
81
+ elsif c == separator
82
+ got_sep = 1
83
+ end
84
+ end
85
+ if date_count > 0 && num_count == 0
86
+ return true
87
+ elsif num_count > 0 && date_count == 0
88
+ return false
89
+ elsif date_count
90
+ # ambiguous result
91
+ elsif got_sep == 0
92
+ # constant result
93
+ end
94
+ return date_count > num_count
95
+ end
96
+
20
97
  # changes fill color of cell
21
98
  def change_fill(rgb='ffffff')
22
99
  validate_worksheet
@@ -145,6 +222,9 @@ module RubyXL
145
222
  def change_contents(data, formula=nil)
146
223
  validate_worksheet
147
224
  @datatype='str'
225
+ if data.is_a?(Date) || data.is_a?(DateTime)
226
+ data = @workbook.date_to_num(data)
227
+ end
148
228
  if (data.is_a?Integer) || (data.is_a?Float)
149
229
  @datatype = ''
150
230
  end
@@ -305,7 +385,7 @@ module RubyXL
305
385
  end
306
386
 
307
387
  def inspect
308
- str = "(#{@row},#{@column}): #{@value}"
388
+ str = "(#{@row},#{@column}): #{@value}"
309
389
  str += " =#{@formula}" if @formula
310
390
  str += ", datatype = #{@datatype}, style_index = #{@style_index}"
311
391
  return str
@@ -33,6 +33,7 @@ module RubyXL
33
33
 
34
34
 
35
35
  # data_only allows only the sheet data to be parsed, so as to speed up parsing
36
+ # However, using this option will result in date-formatted cells being interpreted as numbers
36
37
  def Parser.parse(file_path, data_only=false)
37
38
  @data_only = data_only
38
39
  files = Parser.decompress(file_path)
@@ -386,7 +387,7 @@ module RubyXL
386
387
 
387
388
  wb.shared_strings_XML = files['sharedString'].to_s
388
389
  wb.defined_names = files['workbook'].css('definedNames').to_s
389
-
390
+ wb.date1904 = files['workbook'].css('workbookPr').attribute('date1904').to_s == '1'
390
391
 
391
392
  wb.worksheets = Array.new(@num_sheets) #array of Worksheet objs
392
393
  wb
@@ -9,6 +9,7 @@ require File.expand_path(File.join(File.dirname(__FILE__),'writer','styles_write
9
9
  require File.expand_path(File.join(File.dirname(__FILE__),'writer','shared_strings_writer'))
10
10
  require File.expand_path(File.join(File.dirname(__FILE__),'writer','worksheet_writer'))
11
11
  require 'rubyXL/zip'
12
+ require 'date'
12
13
 
13
14
  module RubyXL
14
15
  class Workbook
@@ -46,7 +47,7 @@ module RubyXL
46
47
  @calc_chain = nil #unnecessary?
47
48
  @num_strings = 0 #num strings total
48
49
  @size = 0 #num strings in shared_strings array
49
- @date1904 = date1904
50
+ @date1904 = date1904
50
51
  @external_links = nil
51
52
  @style_corrector = nil
52
53
  @drawings = nil
@@ -188,6 +189,28 @@ module RubyXL
188
189
  return filepath
189
190
  end
190
191
 
192
+ def date_to_num(date)
193
+ return nil if date.nil?
194
+ if @date1904
195
+ compare_date = DateTime.parse('December 31, 1903')
196
+ else
197
+ compare_date = DateTime.parse('December 31, 1899')
198
+ end
199
+ # add one day to compare date for erroneous 1900 leap year compatibility
200
+ date.ajd - (compare_date.ajd + 1)
201
+ end
202
+
203
+ def num_to_date(num)
204
+ return nil if num.nil?
205
+ if @date1904
206
+ compare_date = DateTime.parse('December 31, 1903')
207
+ else
208
+ compare_date = DateTime.parse('December 31, 1899')
209
+ end
210
+ # add one day to compare date for erroneous 1900 leap year compatibility
211
+ compare_date + 1 + num
212
+ end
213
+
191
214
  #gets style object from style array given index
192
215
  def get_style(style_index)
193
216
  if !@cell_xfs[:xf].is_a?Array
@@ -1370,7 +1370,8 @@ class Worksheet < PrivateClass
1370
1370
  index = nil
1371
1371
 
1372
1372
  @sheet_data.each_with_index do |row, index|
1373
- original_cells_content = row.map { |cell| cell.nil? ? '' : cell.value.to_s }
1373
+ cells_content = cells_content.map { |header| header.to_s.downcase.strip }
1374
+ original_cells_content = row.map { |cell| cell.nil? ? '' : cell.value.to_s.downcase.strip }
1374
1375
  if (cells_content & original_cells_content).size == cells_content.size
1375
1376
  return index
1376
1377
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{rubyXL}
8
- s.version = "1.1.10"
8
+ s.version = "1.1.11"
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{2011-10-14}
12
+ s.date = %q{2011-11-23}
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 = [
@@ -55,10 +55,11 @@ Gem::Specification.new do |s|
55
55
  s.homepage = %q{http://github.com/gilt/rubyXL}
56
56
  s.licenses = ["MIT"]
57
57
  s.require_paths = ["lib"]
58
- s.rubygems_version = %q{1.4.2}
58
+ s.rubygems_version = %q{1.3.7}
59
59
  s.summary = %q{rubyXL is a gem which allows the parsing, creation, and manipulation of Microsoft Excel (.xlsx/.xlsm) Documents}
60
60
 
61
61
  if s.respond_to? :specification_version then
62
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
62
63
  s.specification_version = 3
63
64
 
64
65
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
@@ -192,6 +192,15 @@ describe RubyXL::Cell do
192
192
  end
193
193
  end
194
194
 
195
+ describe '.value' do
196
+ it 'should return the value of a date' do
197
+ date = Date.parse('January 1, 2011')
198
+ @cell.change_contents(date)
199
+ @cell.should_receive(:is_date?).once.and_return(true)
200
+ @cell.value.should == date
201
+ end
202
+ end
203
+
195
204
  describe '.change_contents' do
196
205
  it 'should cause cell value to match string or number that is passed in' do
197
206
  @cell.change_contents('TEST')
@@ -199,6 +208,14 @@ describe RubyXL::Cell do
199
208
  @cell.formula.should == nil
200
209
  end
201
210
 
211
+ it 'should cause cell value to match a date that is passed in' do
212
+ date = Date.parse('January 1, 2011')
213
+ @cell.change_contents(date)
214
+ @cell.should_receive(:is_date?).once.and_return(true)
215
+ @cell.value.should == date
216
+ @cell.formula.should == nil
217
+ end
218
+
202
219
  it 'should cause cell value and formula to match what is passed in' do
203
220
  @cell.change_contents(nil, 'SUM(A2:A4)')
204
221
  @cell.value.should == nil
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubyXL
3
3
  version: !ruby/object:Gem::Version
4
- hash: 7
5
- prerelease:
4
+ hash: 5
5
+ prerelease: false
6
6
  segments:
7
7
  - 1
8
8
  - 1
9
- - 10
10
- version: 1.1.10
9
+ - 11
10
+ version: 1.1.11
11
11
  platform: ruby
12
12
  authors:
13
13
  - Vivek Bhagwat
@@ -15,11 +15,11 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-10-14 00:00:00 -04:00
18
+ date: 2011-11-23 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
- type: :development
22
+ prerelease: false
23
23
  version_requirements: &id001 !ruby/object:Gem::Requirement
24
24
  none: false
25
25
  requirements:
@@ -29,11 +29,11 @@ dependencies:
29
29
  segments:
30
30
  - 0
31
31
  version: "0"
32
- requirement: *id001
33
- prerelease: false
34
32
  name: shoulda
35
- - !ruby/object:Gem::Dependency
33
+ requirement: *id001
36
34
  type: :development
35
+ - !ruby/object:Gem::Dependency
36
+ prerelease: false
37
37
  version_requirements: &id002 !ruby/object:Gem::Requirement
38
38
  none: false
39
39
  requirements:
@@ -45,11 +45,11 @@ dependencies:
45
45
  - 0
46
46
  - 0
47
47
  version: 1.0.0
48
- requirement: *id002
49
- prerelease: false
50
48
  name: bundler
51
- - !ruby/object:Gem::Dependency
49
+ requirement: *id002
52
50
  type: :development
51
+ - !ruby/object:Gem::Dependency
52
+ prerelease: false
53
53
  version_requirements: &id003 !ruby/object:Gem::Requirement
54
54
  none: false
55
55
  requirements:
@@ -61,11 +61,11 @@ dependencies:
61
61
  - 6
62
62
  - 0
63
63
  version: 1.6.0
64
- requirement: *id003
65
- prerelease: false
66
64
  name: jeweler
67
- - !ruby/object:Gem::Dependency
65
+ requirement: *id003
68
66
  type: :development
67
+ - !ruby/object:Gem::Dependency
68
+ prerelease: false
69
69
  version_requirements: &id004 !ruby/object:Gem::Requirement
70
70
  none: false
71
71
  requirements:
@@ -75,11 +75,11 @@ dependencies:
75
75
  segments:
76
76
  - 0
77
77
  version: "0"
78
- requirement: *id004
79
- prerelease: false
80
78
  name: rcov
81
- - !ruby/object:Gem::Dependency
79
+ requirement: *id004
82
80
  type: :development
81
+ - !ruby/object:Gem::Dependency
82
+ prerelease: false
83
83
  version_requirements: &id005 !ruby/object:Gem::Requirement
84
84
  none: false
85
85
  requirements:
@@ -91,11 +91,11 @@ dependencies:
91
91
  - 4
92
92
  - 4
93
93
  version: 1.4.4
94
- requirement: *id005
95
- prerelease: false
96
94
  name: nokogiri
97
- - !ruby/object:Gem::Dependency
95
+ requirement: *id005
98
96
  type: :development
97
+ - !ruby/object:Gem::Dependency
98
+ prerelease: false
99
99
  version_requirements: &id006 !ruby/object:Gem::Requirement
100
100
  none: false
101
101
  requirements:
@@ -107,11 +107,11 @@ dependencies:
107
107
  - 9
108
108
  - 4
109
109
  version: 0.9.4
110
- requirement: *id006
111
- prerelease: false
112
110
  name: rubyzip
113
- - !ruby/object:Gem::Dependency
111
+ requirement: *id006
114
112
  type: :development
113
+ - !ruby/object:Gem::Dependency
114
+ prerelease: false
115
115
  version_requirements: &id007 !ruby/object:Gem::Requirement
116
116
  none: false
117
117
  requirements:
@@ -123,9 +123,9 @@ dependencies:
123
123
  - 3
124
124
  - 4
125
125
  version: 1.3.4
126
- requirement: *id007
127
- prerelease: false
128
126
  name: rspec
127
+ requirement: *id007
128
+ type: :development
129
129
  description: rubyXL is a gem which allows the parsing, creation, and manipulation of Microsoft Excel (.xlsx/.xlsm) Documents
130
130
  email: bhagwat.vivek@gmail.com
131
131
  executables: []
@@ -200,7 +200,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
200
200
  requirements: []
201
201
 
202
202
  rubyforge_project:
203
- rubygems_version: 1.4.2
203
+ rubygems_version: 1.3.7
204
204
  signing_key:
205
205
  specification_version: 3
206
206
  summary: rubyXL is a gem which allows the parsing, creation, and manipulation of Microsoft Excel (.xlsx/.xlsm) Documents