xsv 0.1.1 → 0.1.2
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 +4 -4
- data/Gemfile.lock +1 -1
- data/lib/xsv/helpers.rb +14 -0
- data/lib/xsv/sheet.rb +23 -5
- data/lib/xsv/version.rb +1 -1
- data/lib/xsv.rb +1 -0
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 2eed2bd4654d89a75f817365a919f2ca4c8f4f9bee17e63f5fc938b574826696
|
|
4
|
+
data.tar.gz: ddfa4e09f2d9378a79943cef340b1af20ae49764b64bcb28478b672d676e4565
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 6b79348311d076d9397b7cd0629ac4502dc8a3410835b68682e4410af64dea2603d6ea2a68157e0a0bd933f9cdc3b3780034e645d7df9036d7696bda927b858d
|
|
7
|
+
data.tar.gz: e40fa7237b12c4d83639f797ba38c9928e6d412bb658b0fa6855dc2bbca6004d89f36a306c74b207e453d2a5e3597b7e45fd76b464c1c5b3a45a716d09d876f0
|
data/Gemfile.lock
CHANGED
data/lib/xsv/helpers.rb
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
module Xsv
|
|
2
|
+
module Helpers
|
|
3
|
+
# Return the index number for the given Excel column name
|
|
4
|
+
def column_index(col)
|
|
5
|
+
val = 0
|
|
6
|
+
while col.length > 0
|
|
7
|
+
val *= 26
|
|
8
|
+
val += (col[0].ord - "A".ord + 1)
|
|
9
|
+
col = col[1..-1]
|
|
10
|
+
end
|
|
11
|
+
return val - 1
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
data/lib/xsv/sheet.rb
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
module Xsv
|
|
2
2
|
class Sheet
|
|
3
|
+
include Xsv::Helpers
|
|
4
|
+
|
|
3
5
|
attr_reader :xml
|
|
4
6
|
|
|
5
7
|
def initialize(workbook, xml)
|
|
@@ -17,7 +19,9 @@ module Xsv
|
|
|
17
19
|
def each_row(read_headers: false)
|
|
18
20
|
@parse_headers if read_headers
|
|
19
21
|
|
|
20
|
-
@xml.css("sheetData row").
|
|
22
|
+
@xml.css("sheetData row").each_with_index do |row_xml, i|
|
|
23
|
+
next if i == 0 && @headers.any?
|
|
24
|
+
|
|
21
25
|
yield(parse_row(row_xml))
|
|
22
26
|
end
|
|
23
27
|
|
|
@@ -50,14 +54,14 @@ module Xsv
|
|
|
50
54
|
row = []
|
|
51
55
|
end
|
|
52
56
|
|
|
53
|
-
|
|
54
|
-
next if @headers.any? && i == 0
|
|
57
|
+
next_index = 0
|
|
55
58
|
|
|
59
|
+
xml.css("c").each do |c_xml|
|
|
56
60
|
value = case c_xml["t"]
|
|
57
61
|
when "s"
|
|
58
62
|
@workbook.shared_strings[c_xml.css("v").inner_text.to_i]
|
|
59
63
|
when "str"
|
|
60
|
-
c_xml.css("v").inner_text
|
|
64
|
+
c_xml.css("v").inner_text.to_s
|
|
61
65
|
when "e" # N/A
|
|
62
66
|
nil
|
|
63
67
|
when nil
|
|
@@ -66,11 +70,25 @@ module Xsv
|
|
|
66
70
|
raise Xsv::Error, "Encountered unknown column type #{c_xml["t"]}"
|
|
67
71
|
end
|
|
68
72
|
|
|
73
|
+
# Determine column position and pad row with nil values
|
|
74
|
+
col_index = column_index(c_xml["r"].scan(/^[A-Z]+/).first)
|
|
75
|
+
|
|
76
|
+
(col_index - next_index).times do
|
|
77
|
+
if @headers.any?
|
|
78
|
+
row[@headers[next_index]] = nil
|
|
79
|
+
else
|
|
80
|
+
row << nil
|
|
81
|
+
end
|
|
82
|
+
next_index += 1
|
|
83
|
+
end
|
|
84
|
+
|
|
69
85
|
if @headers.any?
|
|
70
|
-
row[@headers[
|
|
86
|
+
row[@headers[next_index]] = value
|
|
71
87
|
else
|
|
72
88
|
row << value
|
|
73
89
|
end
|
|
90
|
+
|
|
91
|
+
next_index += 1
|
|
74
92
|
end
|
|
75
93
|
|
|
76
94
|
row
|
data/lib/xsv/version.rb
CHANGED
data/lib/xsv.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: xsv
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Martijn Storck
|
|
@@ -98,6 +98,7 @@ files:
|
|
|
98
98
|
- bin/setup
|
|
99
99
|
- lib/xsv.rb
|
|
100
100
|
- lib/xsv/file.rb
|
|
101
|
+
- lib/xsv/helpers.rb
|
|
101
102
|
- lib/xsv/sheet.rb
|
|
102
103
|
- lib/xsv/version.rb
|
|
103
104
|
- xsv.gemspec
|