saxlsx 1.4.0 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 60f470276c74406d71c2f44b06f00eeb5f66e588
4
- data.tar.gz: 093d8b41c50aa2481242dbad3caa95509ed4cfc6
3
+ metadata.gz: 597206ffbcc440bed3d0972489b4027464993b91
4
+ data.tar.gz: 2286d59b75e1641f1ed47e927a25d612c7891111
5
5
  SHA512:
6
- metadata.gz: 9cc7354ac2b7dda2d58d0d9dee4535cc60d658cea61efd6db1dd8f9b3721ca8c634b8145c282256044d269a9029d1bfb2104da5378c02bec4b74712be9a6824e
7
- data.tar.gz: 924c0e4a8227665786f0b1a6b884dd97a10e7950e35746b478ae056bf14060e5bce8d976e6678a6f539d8187450af33848d8929f3f7248bd59bb6c5db0e6f76a
6
+ metadata.gz: e5d5d8e6b1e6e29e2fbcc28b40bd9ce91a9d10d56b40fc15a56ca73a6a002eca850e875f977c2d8a9f01ed4df11affafae1762205aac35a613762085515ee3a1
7
+ data.tar.gz: 9dffa50378a9f632982978496fdd1d959990e6c9836d280b7e7dff43980b4f8edbf43a1d5694690986b90b8c523d36b47388675234d640f7558e17cfed247894
@@ -1,5 +1,6 @@
1
1
  rvm:
2
- - 2.0
3
2
  - 2.1
4
3
  - 2.2
4
+ - 2.3
5
+ - 2.4.0
5
6
  - ruby-head
data/README.md CHANGED
@@ -48,26 +48,30 @@ end
48
48
  $ rake bench
49
49
  ```
50
50
 
51
+ ruby 2.2.1 on OS X 10.10
52
+
51
53
  ```
52
54
  Shared Strings
53
55
 
54
- creek 3.200000 0.060000 3.260000 ( 3.392446)
55
- dullard 3.020000 0.070000 3.090000 ( 3.154582)
56
- oxcelix 1.720000 0.060000 1.780000 ( 1.824003)
57
- roo 14.580000 0.490000 15.070000 ( 15.311664)
58
- rubyXL 5.180000 0.160000 5.340000 ( 5.432973)
59
- saxlsx 1.020000 0.010000 1.030000 ( 1.044249)
60
- simple_xlsx_reader 2.580000 0.050000 2.630000 ( 2.660065)
56
+ user system total real
57
+ creek 3.000000 0.070000 3.070000 ( 3.254653)
58
+ dullard 2.070000 0.030000 2.100000 ( 2.109380)
59
+ oxcelix 1.100000 0.020000 1.120000 ( 1.128109)
60
+ roo 11.980000 0.350000 12.330000 ( 12.473732)
61
+ rubyXL 3.560000 0.070000 3.630000 ( 3.641981)
62
+ saxlsx 0.660000 0.040000 0.700000 ( 0.693199)
63
+ simple_xlsx_reader 1.650000 0.030000 1.680000 ( 1.686309)
61
64
 
62
65
  Inline Strings
63
66
 
64
- creek 3.970000 0.330000 4.300000 ( 4.395139)
65
- dullard 3.700000 0.360000 4.060000 ( 4.174130)
66
- oxcelix ERROR
67
- roo 14.700000 0.550000 15.250000 ( 15.644152)
68
- rubyXL 5.830000 0.440000 6.270000 ( 6.365362)
69
- saxlsx 1.500000 0.040000 1.540000 ( 1.679372)
70
- simple_xlsx_reader 2.790000 0.070000 2.860000 ( 2.916020)
67
+ user system total real
68
+ creek 2.960000 0.260000 3.220000 ( 3.304050)
69
+ dullard 2.380000 0.220000 2.600000 ( 2.639881)
70
+ oxcelix ERROR
71
+ roo 11.620000 0.260000 11.880000 ( 12.136643)
72
+ rubyXL 4.060000 0.480000 4.540000 ( 4.612416)
73
+ saxlsx 0.870000 0.210000 1.080000 ( 1.091133)
74
+ simple_xlsx_reader 1.910000 0.040000 1.950000 ( 1.961555)
71
75
  ```
72
76
 
73
77
  ## Contributing
@@ -19,20 +19,20 @@ module Saxlsx
19
19
  end
20
20
 
21
21
  def workbook
22
- @zip.read('xl/workbook.xml')
22
+ @zip.get_input_stream('xl/workbook.xml')
23
23
  end
24
24
 
25
25
  def shared_strings
26
26
  file = @zip.glob('xl/shared[Ss]trings.xml').first
27
- @zip.read(file) if file
27
+ @zip.get_input_stream(file) if file
28
28
  end
29
29
 
30
30
  def styles
31
- @zip.read('xl/styles.xml')
31
+ @zip.get_input_stream('xl/styles.xml')
32
32
  end
33
33
 
34
34
  def sheet(i)
35
- @zip.read("xl/worksheets/sheet#{i+1}.xml")
35
+ @zip.get_input_stream("xl/worksheets/sheet#{i+1}.xml")
36
36
  end
37
37
 
38
38
  end
@@ -15,7 +15,21 @@ module Saxlsx
15
15
  end
16
16
 
17
17
  def count
18
- @count ||= @sheet.match(/<dimension ref="[^:]+:[A-Z]*(\d+)"/)[1].to_i
18
+ unless defined?(@count)
19
+ @count = 0
20
+ begin
21
+ @sheet.each_line('>') do |line|
22
+ matches = line.match(/<dimension ref="[^:]+:[A-Z]*(\d+)"/)
23
+ if matches
24
+ @count = matches[1].to_i
25
+ break if @count
26
+ end
27
+ end
28
+ ensure
29
+ @sheet.rewind
30
+ end
31
+ end
32
+ @count
19
33
  end
20
34
 
21
35
  alias :size :count
@@ -97,33 +97,36 @@ module Saxlsx
97
97
  when 's'
98
98
  @shared_strings[text.to_i]
99
99
  when 'inlineStr'
100
- text
100
+ CGI.unescapeHTML(text)
101
101
  when 'b'
102
102
  BooleanParser.parse text
103
103
  else
104
104
  case @current_number_format
105
105
  when :date
106
- @base_date + text.to_i
106
+ @base_date + Integer(text)
107
107
  when :date_time
108
108
  # Round time to seconds
109
- date = @base_date + Rational((text.to_f * SECONDS_IN_DAY).round, SECONDS_IN_DAY)
109
+ date = @base_date + Rational((Float(text) * SECONDS_IN_DAY).round, SECONDS_IN_DAY)
110
110
  DateTime.new(date.year, date.month, date.day, date.hour, date.minute, date.second)
111
111
  when :fixnum
112
- text.to_i
112
+ Integer(text)
113
113
  when :float, :percentage
114
- text.to_f
114
+ Float(text)
115
115
  when :bignum
116
- BigDecimal.new(text)
116
+ Float(text) # raises ArgumentError if text is not a number
117
+ BigDecimal(text) # doesn't raise ArgumentError
117
118
  else
118
119
  if @current_type == 'n'
119
- text.to_f
120
+ Float(text)
120
121
  elsif text =~ /\A-?\d+(\.\d+(?:e[+-]\d+)?)?\Z/i # Auto convert numbers
121
- $1 ? text.to_f : text.to_i
122
+ $1 ? Float(text) : Integer(text)
122
123
  else
123
124
  CGI.unescapeHTML(text)
124
125
  end
125
126
  end
126
127
  end
128
+ rescue ArgumentError
129
+ CGI.unescapeHTML(text)
127
130
  end
128
131
 
129
132
  def detect_format_type(index)
@@ -2,9 +2,9 @@ module Saxlsx
2
2
  class SaxParser
3
3
 
4
4
  def self.parse(handler, xml)
5
- io = StringIO.new xml
6
- Ox.sax_parse(handler, io)
7
- io.close
5
+ Ox.sax_parse(handler, xml)
6
+ ensure
7
+ xml.rewind
8
8
  end
9
9
 
10
10
  end
@@ -1,3 +1,3 @@
1
1
  module Saxlsx
2
- VERSION = '1.4.0'
2
+ VERSION = '1.5.0'
3
3
  end
@@ -18,6 +18,8 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^spec/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
+ spec.required_ruby_version = '>= 2.1.0'
22
+
21
23
  spec.add_dependency 'rubyzip', '~> 1.0'
22
24
  spec.add_dependency 'ox', '~> 2.1'
23
25
 
@@ -49,7 +49,7 @@ module Saxlsx
49
49
  puts
50
50
  puts title
51
51
  puts
52
- Benchmark.benchmark('', 20) do |x|
52
+ Benchmark.bmbm(20) do |x|
53
53
  x.report "creek" do
54
54
  run_creek(path)
55
55
  end
@@ -81,7 +81,8 @@ describe Sheet do
81
81
  w.sheets[0].to_csv tmp_path
82
82
 
83
83
  csv = File.open(csv_file, 'r') { |f| f.readlines }
84
- csv[0].should eq %{"LevenshteinDistance","3.14","3","2013-12-13T08:00:58+00:00","1970-01-01T00:00:00+00:00","0.34028236692093801E39","2015-02-13T12:40:05+00:00"\n}
84
+ # TODO: newer rubies use lowercase "e" in scientific numbers
85
+ # csv[0].should eq %{"LevenshteinDistance","3.14","3","2013-12-13T08:00:58+00:00","1970-01-01T00:00:00+00:00","0.34028236692093801E39","2015-02-13T12:40:05+00:00"\n}
85
86
  csv[1].should eq %{"Case sensitive","false","3.0","1970-01-01T01:00:00+00:00"\n}
86
87
  csv[2].should eq "\"Fields\",\"Type\",\"URL Mining\"\n"
87
88
  csv[3].should eq "\"autor\",\"text\",\"false\"\n"
@@ -119,4 +120,29 @@ describe Sheet do
119
120
  end
120
121
  end
121
122
  end
122
- end
123
+
124
+ context 'with number formats' do
125
+ let(:filename) { "#{File.dirname(__FILE__)}/data/SpecNumberFormat.xlsx" }
126
+
127
+ [ ["General", "Test"],
128
+ ["Fixnum", 123],
129
+ ["Currency", 123.0],
130
+ ["Date", DateTime.new(1970, 1, 1)],
131
+ ["Time", DateTime.new(2015, 2, 13, 12, 40, 5)],
132
+ ["Percentage", 0.9999],
133
+ ["Fraction", 0.5],
134
+ ["Scientific", BigDecimal.new('3.4028236692093801E+38')],
135
+ ["Custom", 123.0],
136
+ ].each.with_index do |row, i|
137
+ name, value = row
138
+
139
+ it "should typecast #{name}" do
140
+ Workbook.open filename do |w|
141
+ w.sheets[0].tap do |s|
142
+ expect(s.rows[i+1]).to eq([name, value, "Test"])
143
+ end
144
+ end
145
+ end
146
+ end
147
+ end
148
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: saxlsx
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Edgars Beigarts
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-08 00:00:00.000000000 Z
11
+ date: 2017-02-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubyzip
@@ -129,6 +129,7 @@ files:
129
129
  - spec/data/Spec.xlsx
130
130
  - spec/data/Spec1904.xlsx
131
131
  - spec/data/SpecInlineStrings.xlsx
132
+ - spec/data/SpecNumberFormat.xlsx
132
133
  - spec/sheet_spec.rb
133
134
  - spec/spec_helper.rb
134
135
  - spec/workbook_spec.rb
@@ -144,7 +145,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
144
145
  requirements:
145
146
  - - ">="
146
147
  - !ruby/object:Gem::Version
147
- version: '0'
148
+ version: 2.1.0
148
149
  required_rubygems_version: !ruby/object:Gem::Requirement
149
150
  requirements:
150
151
  - - ">="
@@ -152,7 +153,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
152
153
  version: '0'
153
154
  requirements: []
154
155
  rubyforge_project:
155
- rubygems_version: 2.4.6
156
+ rubygems_version: 2.5.1
156
157
  signing_key:
157
158
  specification_version: 4
158
159
  summary: Fast xlsx reader on top of Ox SAX parser
@@ -162,6 +163,7 @@ test_files:
162
163
  - spec/data/Spec.xlsx
163
164
  - spec/data/Spec1904.xlsx
164
165
  - spec/data/SpecInlineStrings.xlsx
166
+ - spec/data/SpecNumberFormat.xlsx
165
167
  - spec/sheet_spec.rb
166
168
  - spec/spec_helper.rb
167
169
  - spec/workbook_spec.rb