simple_xlsx_reader 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/lib/simple_xlsx_reader.rb +22 -3
- data/lib/simple_xlsx_reader/version.rb +1 -1
- data/test/date1904.xlsx +0 -0
- data/test/date1904_test.rb +13 -0
- metadata +5 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 94422da0193805c579ba37c7c3e58b35a996dfbc
|
4
|
+
data.tar.gz: a9c5e1f01acc0c60165a13adc1af087743a60935
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 33338f8fcf3c180ea346548061598953842358a21acd6d97bf451c07d8655f179af0cf7b7791f7c9de1a8411578e3623faab178b3cd74893aaf6d040a7abde96
|
7
|
+
data.tar.gz: 50035b920f6811eed88c318c17b47bf8823aa1ac4bf114af3bc29174edcf08ebd5d16902177aa6a48b70f8e70a745249bb8494101f9f310f24d5f5d5bbc13f27
|
data/CHANGELOG.md
CHANGED
data/lib/simple_xlsx_reader.rb
CHANGED
@@ -100,6 +100,9 @@ module SimpleXlsxReader
|
|
100
100
|
##
|
101
101
|
# For internal use; translates source xml to Sheet objects.
|
102
102
|
class Mapper < Struct.new(:xml)
|
103
|
+
DATE_SYSTEM_1900 = Date.new(1899, 12, 30)
|
104
|
+
DATE_SYSTEM_1904 = Date.new(1904, 1, 1)
|
105
|
+
|
103
106
|
def load_sheets
|
104
107
|
sheet_toc.each_with_index.map do |(sheet_name, _sheet_number), i|
|
105
108
|
parse_sheet(sheet_name, xml.sheets[i]) # sheet_number is *not* the index into xml.sheets
|
@@ -149,7 +152,8 @@ module SimpleXlsxReader
|
|
149
152
|
|
150
153
|
cell = begin
|
151
154
|
self.class.cast(xvalue && xvalue.text.strip, type, style,
|
152
|
-
:shared_strings => shared_strings
|
155
|
+
:shared_strings => shared_strings,
|
156
|
+
:base_date => base_date)
|
153
157
|
rescue => e
|
154
158
|
if !SimpleXlsxReader.configuration.catch_cell_load_errors
|
155
159
|
error = CellLoadError.new(
|
@@ -343,10 +347,10 @@ module SimpleXlsxReader
|
|
343
347
|
# the trickiest. note that all these formats can vary on
|
344
348
|
# whether they actually contain a date, time, or datetime.
|
345
349
|
when :date, :time, :date_time
|
346
|
-
|
350
|
+
days_since_date_system_start, fraction_of_24 = value.split('.')
|
347
351
|
|
348
352
|
# http://stackoverflow.com/questions/10559767/how-to-convert-ms-excel-date-from-float-to-date-format-in-ruby
|
349
|
-
date =
|
353
|
+
date = options.fetch(:base_date, DATE_SYSTEM_1900) + Integer(days_since_date_system_start)
|
350
354
|
|
351
355
|
if fraction_of_24 # there is a time associated
|
352
356
|
fraction_of_24 = "0.#{fraction_of_24}".to_f
|
@@ -372,6 +376,21 @@ module SimpleXlsxReader
|
|
372
376
|
end
|
373
377
|
end
|
374
378
|
|
379
|
+
## Returns the base_date from which to calculate dates.
|
380
|
+
# Defaults to 1900 (minus two days due to excel quirk), but use 1904 if
|
381
|
+
# it's set in the Workbook's workbookPr.
|
382
|
+
# http://msdn.microsoft.com/en-us/library/ff530155(v=office.12).aspx
|
383
|
+
def base_date
|
384
|
+
@base_date ||=
|
385
|
+
begin
|
386
|
+
return DATE_SYSTEM_1900 if xml.workbook == nil
|
387
|
+
xml.workbook.xpath("//workbook/workbookPr[@date1904]").each do |workbookPr|
|
388
|
+
return DATE_SYSTEM_1904 if workbookPr["date1904"] =~ /true|1/i
|
389
|
+
end
|
390
|
+
DATE_SYSTEM_1900
|
391
|
+
end
|
392
|
+
end
|
393
|
+
|
375
394
|
# Map of non-custom numFmtId to casting symbol
|
376
395
|
NumFmtMap = {
|
377
396
|
0 => :string, # General
|
data/test/date1904.xlsx
ADDED
Binary file
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
describe SimpleXlsxReader do
|
4
|
+
let(:date1904_file) { File.join(File.dirname(__FILE__), 'date1904.xlsx') }
|
5
|
+
let(:subject) { SimpleXlsxReader::Document.new(date1904_file) }
|
6
|
+
|
7
|
+
it 'supports converting dates with the 1904 date system' do
|
8
|
+
subject.to_hash.must_equal({
|
9
|
+
"date1904" => [[Date.parse("2014-05-01")]]
|
10
|
+
})
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_xlsx_reader
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Woody Peterson
|
@@ -82,6 +82,8 @@ files:
|
|
82
82
|
- lib/simple_xlsx_reader.rb
|
83
83
|
- lib/simple_xlsx_reader/version.rb
|
84
84
|
- simple_xlsx_reader.gemspec
|
85
|
+
- test/date1904.xlsx
|
86
|
+
- test/date1904_test.rb
|
85
87
|
- test/datetime_test.rb
|
86
88
|
- test/datetimes.xlsx
|
87
89
|
- test/performance_test.rb
|
@@ -114,6 +116,8 @@ signing_key:
|
|
114
116
|
specification_version: 4
|
115
117
|
summary: Read xlsx data the Ruby way
|
116
118
|
test_files:
|
119
|
+
- test/date1904.xlsx
|
120
|
+
- test/date1904_test.rb
|
117
121
|
- test/datetime_test.rb
|
118
122
|
- test/datetimes.xlsx
|
119
123
|
- test/performance_test.rb
|