xsv 0.3.11 → 0.3.12
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/CHANGELOG.md +4 -0
- data/README.md +16 -0
- data/lib/xsv.rb +2 -0
- data/lib/xsv/relationships_handler.rb +39 -0
- data/lib/xsv/sheet.rb +4 -2
- data/lib/xsv/sheets_ids_handler.rb +49 -0
- data/lib/xsv/version.rb +1 -1
- data/lib/xsv/workbook.rb +28 -1
- metadata +4 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 36eeca38d2791f71dafba9b1ae6308e63b3554efd98e2949448c4715863b7616
|
|
4
|
+
data.tar.gz: cf90d5ff1def79ee8b17877523a5e8233b7c5253801c98eb0e725724a6e3f48d
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: cc203883d561d86a51eb35c11f4c4547929605387a43d38c16eb48f975a2da1ea332c7cbb4aab21e4760424ceb015de2e4d50aeeddd9bf595ccd3bf1f363ac63
|
|
7
|
+
data.tar.gz: b91d7d210c6894c17bfbb2b40dd0c243f2c5228f35c780f8e5d8afba9367303d2df1897ae734bec636f8eab615cda7188d63e63b270b3f543fafc39d91d1aa7e
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
|
@@ -83,6 +83,22 @@ columns with the same name!
|
|
|
83
83
|
`Xsv::Sheet` implements `Enumerable` so you can call methods like `#first`,
|
|
84
84
|
`#filter`/`#select` and `#map` on it.
|
|
85
85
|
|
|
86
|
+
The sheets could be accessed by index or by name:
|
|
87
|
+
|
|
88
|
+
```ruby
|
|
89
|
+
x = Xsv::Workbook.open("sheet.xlsx")
|
|
90
|
+
|
|
91
|
+
sheet = x.sheets[0] # gets sheet by index
|
|
92
|
+
|
|
93
|
+
sheet = x.sheets_by_name('Name').first # gets sheet by name
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
To get all the workbook's sheets names:
|
|
97
|
+
|
|
98
|
+
```ruby
|
|
99
|
+
sheet_names = x.sheets.map(&:name)
|
|
100
|
+
```
|
|
101
|
+
|
|
86
102
|
### Assumptions
|
|
87
103
|
|
|
88
104
|
Since Xsv treats worksheets like csv files it makes certain assumptions about your
|
data/lib/xsv.rb
CHANGED
|
@@ -3,10 +3,12 @@ require "date"
|
|
|
3
3
|
require "ox"
|
|
4
4
|
|
|
5
5
|
require "xsv/helpers"
|
|
6
|
+
require "xsv/relationships_handler"
|
|
6
7
|
require "xsv/shared_strings_parser"
|
|
7
8
|
require "xsv/sheet"
|
|
8
9
|
require "xsv/sheet_bounds_handler"
|
|
9
10
|
require "xsv/sheet_rows_handler"
|
|
11
|
+
require 'xsv/sheets_ids_handler'
|
|
10
12
|
require "xsv/styles_handler"
|
|
11
13
|
require "xsv/version"
|
|
12
14
|
require "xsv/workbook"
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
module Xsv
|
|
3
|
+
# RelationshipsHandler parses the "xl/_rels/workbook.xml.rels" file to get the existing relationships.
|
|
4
|
+
# This is used internally when opening a workbook.
|
|
5
|
+
class RelationshipsHandler < Ox::Sax
|
|
6
|
+
def self.get_relations(io)
|
|
7
|
+
relations = []
|
|
8
|
+
handler = new do |relation|
|
|
9
|
+
relations << relation
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
Ox.sax_parse(handler, io.read)
|
|
13
|
+
return relations
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# Ox::Sax implementation
|
|
17
|
+
|
|
18
|
+
def initialize(&block)
|
|
19
|
+
@block = block
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def start_element(name)
|
|
23
|
+
@relationship = {} if name == :Relationship
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def attr(name, value)
|
|
27
|
+
case name
|
|
28
|
+
when :Id, :Type, :Target
|
|
29
|
+
@relationship[name] = value
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def end_element(name)
|
|
34
|
+
return unless name == :Relationship
|
|
35
|
+
|
|
36
|
+
@block.call(@relationship)
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
data/lib/xsv/sheet.rb
CHANGED
|
@@ -17,7 +17,7 @@ module Xsv
|
|
|
17
17
|
|
|
18
18
|
# Returns the current mode. Call {#parse_headers!} to switch to `:hash` mode
|
|
19
19
|
# @return [Symbol] `:hash` or `:array`
|
|
20
|
-
attr_reader :mode
|
|
20
|
+
attr_reader :id, :mode, :name
|
|
21
21
|
|
|
22
22
|
# Set a number of rows to skip at the top of the sheet (header row offset).
|
|
23
23
|
# For hash mode, do not skip the header row as this will be automatically
|
|
@@ -30,9 +30,11 @@ module Xsv
|
|
|
30
30
|
# @param workbook [Workbook] The Workbook with shared data such as shared strings and styles
|
|
31
31
|
# @param io [IO] A handle to an open worksheet XML file
|
|
32
32
|
# @param size [Number] size of the XML file
|
|
33
|
-
def initialize(workbook, io, size)
|
|
33
|
+
def initialize(workbook, io, size, ids)
|
|
34
34
|
@workbook = workbook
|
|
35
|
+
@id = ids[:sheetId].to_i
|
|
35
36
|
@io = io
|
|
37
|
+
@name = ids[:name]
|
|
36
38
|
@size = size
|
|
37
39
|
@headers = []
|
|
38
40
|
@mode = :array
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
module Xsv
|
|
3
|
+
# SheetsIdsHandler interprets the relevant parts of workbook.xml
|
|
4
|
+
# This is used internally to get the sheets ids, relationship_ids, and names when opening a workbook.
|
|
5
|
+
class SheetsIdsHandler < Ox::Sax
|
|
6
|
+
def self.get_sheets_ids(io)
|
|
7
|
+
sheets_ids = []
|
|
8
|
+
handler = new do |sheet_ids|
|
|
9
|
+
sheets_ids << sheet_ids
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
Ox.sax_parse(handler, io.read)
|
|
13
|
+
return sheets_ids
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# Ox::Sax implementation
|
|
17
|
+
|
|
18
|
+
def initialize(&block)
|
|
19
|
+
@block = block
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def start_element(name)
|
|
23
|
+
return @parsing = true if name == :sheets
|
|
24
|
+
|
|
25
|
+
return unless name == :sheet
|
|
26
|
+
|
|
27
|
+
@sheet_ids = {}
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def attr(name, value)
|
|
31
|
+
return unless @parsing
|
|
32
|
+
|
|
33
|
+
case name
|
|
34
|
+
when :name, :sheetId
|
|
35
|
+
@sheet_ids[name] = value
|
|
36
|
+
when :'r:id'
|
|
37
|
+
@sheet_ids[:r_id] = value
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def end_element(name)
|
|
42
|
+
return @parsing = false if name == :sheets
|
|
43
|
+
|
|
44
|
+
return unless name == :sheet
|
|
45
|
+
|
|
46
|
+
@block.call(@sheet_ids)
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
data/lib/xsv/version.rb
CHANGED
data/lib/xsv/workbook.rb
CHANGED
|
@@ -41,6 +41,8 @@ module Xsv
|
|
|
41
41
|
|
|
42
42
|
fetch_shared_strings
|
|
43
43
|
fetch_styles
|
|
44
|
+
fetch_sheets_ids
|
|
45
|
+
fetch_relationships
|
|
44
46
|
fetch_sheets
|
|
45
47
|
end
|
|
46
48
|
|
|
@@ -56,11 +58,20 @@ module Xsv
|
|
|
56
58
|
@sheets = nil
|
|
57
59
|
@xfs = nil
|
|
58
60
|
@numFmts = nil
|
|
61
|
+
@relationships = nil
|
|
59
62
|
@shared_strings = nil
|
|
63
|
+
@sheets_ids = nil
|
|
60
64
|
|
|
61
65
|
true
|
|
62
66
|
end
|
|
63
67
|
|
|
68
|
+
# Returns an array of sheets for the case of same name sheets.
|
|
69
|
+
# @param [String] name
|
|
70
|
+
# @return [Array<Xsv::Sheet>]
|
|
71
|
+
def sheets_by_name(name)
|
|
72
|
+
@sheets.select { |s| s.name == name }
|
|
73
|
+
end
|
|
74
|
+
|
|
64
75
|
private
|
|
65
76
|
|
|
66
77
|
def fetch_shared_strings
|
|
@@ -80,8 +91,24 @@ module Xsv
|
|
|
80
91
|
@zip.glob("xl/worksheets/sheet*.xml").sort do |a, b|
|
|
81
92
|
a.name[/\d+/].to_i <=> b.name[/\d+/].to_i
|
|
82
93
|
end.each do |entry|
|
|
83
|
-
|
|
94
|
+
rel = @relationships.detect { |r| entry.name.end_with?(r[:Target]) && r[:Type].end_with?('worksheet') }
|
|
95
|
+
sheet_ids = @sheets_ids.detect { |i| i[:r_id] == rel[:Id] }
|
|
96
|
+
@sheets << Xsv::Sheet.new(self, entry.get_input_stream, entry.size, sheet_ids)
|
|
84
97
|
end
|
|
85
98
|
end
|
|
99
|
+
|
|
100
|
+
def fetch_sheets_ids
|
|
101
|
+
stream = @zip.glob("xl/workbook.xml").first.get_input_stream
|
|
102
|
+
@sheets_ids = SheetsIdsHandler.get_sheets_ids(stream)
|
|
103
|
+
|
|
104
|
+
stream.close
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def fetch_relationships
|
|
108
|
+
stream = @zip.glob("xl/_rels/workbook.xml.rels").first.get_input_stream
|
|
109
|
+
@relationships = RelationshipsHandler.get_relations(stream)
|
|
110
|
+
|
|
111
|
+
stream.close
|
|
112
|
+
end
|
|
86
113
|
end
|
|
87
114
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: xsv
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.3.
|
|
4
|
+
version: 0.3.12
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Martijn Storck
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2020-04-
|
|
11
|
+
date: 2020-04-15 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: rubyzip
|
|
@@ -108,10 +108,12 @@ files:
|
|
|
108
108
|
- bin/setup
|
|
109
109
|
- lib/xsv.rb
|
|
110
110
|
- lib/xsv/helpers.rb
|
|
111
|
+
- lib/xsv/relationships_handler.rb
|
|
111
112
|
- lib/xsv/shared_strings_parser.rb
|
|
112
113
|
- lib/xsv/sheet.rb
|
|
113
114
|
- lib/xsv/sheet_bounds_handler.rb
|
|
114
115
|
- lib/xsv/sheet_rows_handler.rb
|
|
116
|
+
- lib/xsv/sheets_ids_handler.rb
|
|
115
117
|
- lib/xsv/styles_handler.rb
|
|
116
118
|
- lib/xsv/version.rb
|
|
117
119
|
- lib/xsv/workbook.rb
|