ooxl 0.0.1.5.3 → 0.0.1.5.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/bin/console +3 -3
- data/lib/ooxl/ooxl.rb +54 -32
- data/lib/ooxl/version.rb +1 -1
- data/lib/ooxl/xl_objects/relationships.rb +20 -7
- metadata +3 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: feba34a102bb13498ce6cfbbdad7ae2d9f02e4d5f872a92a1bd80a5f74c59d83
|
4
|
+
data.tar.gz: 94ab7441bbbe828a0a4fe0f3d98b97f62d2ae25f6aa052ce657f1567b868abca
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8653a2a872e73055569eff3f7114a24ca76769f0251dd71e52bfb2a15e5c81f51fd6f4fd9ad8ce9d046099c2f07a44f16ab61e7e1d02380f98b6474e132571d4
|
7
|
+
data.tar.gz: 8e12f9a5b9f101a01ab33b991c6923f32daa926e16c3ffd2a9ce984b2a94d7e9a9e3a3cf8714b709051004af336fbd7d132c92e5f7de14bddc9bb5f9f0dc50fc
|
data/bin/console
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
3
|
require "bundler/setup"
|
4
|
-
require "
|
4
|
+
require "./lib/ooxl"
|
5
5
|
|
6
6
|
# You can add fixtures and/or initialization code here to make experimenting
|
7
7
|
# with your gem easier. You can also use a different console, if you like.
|
@@ -10,5 +10,5 @@ require "ooxml_excel"
|
|
10
10
|
# require "pry"
|
11
11
|
# Pry.start
|
12
12
|
|
13
|
-
require "
|
14
|
-
|
13
|
+
require "pry"
|
14
|
+
Pry.start
|
data/lib/ooxl/ooxl.rb
CHANGED
@@ -3,20 +3,33 @@ class OOXL
|
|
3
3
|
include ListHelper
|
4
4
|
attr_reader :filename
|
5
5
|
|
6
|
-
def initialize(
|
6
|
+
def initialize(filepath = nil, contents: nil, **options)
|
7
7
|
@workbook = nil
|
8
8
|
@sheets = {}
|
9
9
|
@styles = []
|
10
10
|
@comments = {}
|
11
|
-
@
|
11
|
+
@workbook_relationships = nil
|
12
|
+
@sheet_relationships = {}
|
12
13
|
@options = options
|
13
14
|
@tables = []
|
14
|
-
|
15
|
-
|
15
|
+
|
16
|
+
@filename = filepath && File.basename(filepath)
|
17
|
+
if contents.present?
|
18
|
+
parse_spreadsheet_contents(contents)
|
19
|
+
elsif filepath.present?
|
20
|
+
parse_spreadsheet_file(filepath)
|
21
|
+
else
|
22
|
+
raise 'no file path or contents were provided'
|
23
|
+
end
|
16
24
|
end
|
17
25
|
|
18
26
|
def self.open(spreadsheet_filepath, options={})
|
19
|
-
new(spreadsheet_filepath, options)
|
27
|
+
new(spreadsheet_filepath, **options)
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.parse(spreadsheet_contents, options={})
|
31
|
+
spreadsheet_contents.force_encoding('ASCII-8BIT') if spreadsheet_contents.respond_to?(:force_encoding)
|
32
|
+
new(nil, contents: spreadsheet_contents, **options)
|
20
33
|
end
|
21
34
|
|
22
35
|
def sheets(skip_hidden: false)
|
@@ -36,7 +49,7 @@ class OOXL
|
|
36
49
|
sheet_meta = @workbook.sheets.find { |sheet| sheet[:name] == sheet_name }
|
37
50
|
raise "No #{sheet_name} in workbook." if sheet_meta.nil?
|
38
51
|
|
39
|
-
sheet_index = sheet_meta[:
|
52
|
+
sheet_index = @workbook_relationships[sheet_meta[:relationship_id]].scan(/\d+/).first
|
40
53
|
sheet = @sheets.fetch(sheet_index)
|
41
54
|
|
42
55
|
# shared variables
|
@@ -75,37 +88,46 @@ class OOXL
|
|
75
88
|
end
|
76
89
|
|
77
90
|
def fetch_comments(sheet_index)
|
78
|
-
relationship = @
|
91
|
+
relationship = @sheet_relationships[sheet_index]
|
79
92
|
@comments[relationship.comment_id] if relationship.present?
|
80
93
|
end
|
81
94
|
|
82
|
-
def
|
95
|
+
def parse_spreadsheet_file(file_path)
|
96
|
+
Zip::File.open(file_path) { |zip| parse_zip(zip) }
|
97
|
+
end
|
98
|
+
|
99
|
+
def parse_spreadsheet_contents(file_contents)
|
100
|
+
# open_buffer works for strings and IO streams
|
101
|
+
Zip::File.open_buffer(file_contents) { |zip| parse_zip(zip) }
|
102
|
+
end
|
103
|
+
|
104
|
+
def parse_zip(spreadsheet_zip)
|
83
105
|
shared_strings = []
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
shared_strings << shared_string_node.xpath('r/t|t').map { |value_node| value_node.text}.join('')
|
98
|
-
end
|
99
|
-
when /xl\/tables\/.*?/i
|
100
|
-
@tables << OOXL::Table.new(entry.get_input_stream.read)
|
101
|
-
when "xl/workbook.xml"
|
102
|
-
@workbook = OOXL::Workbook.load_from_stream(entry.get_input_stream.read)
|
103
|
-
when /xl\/worksheets\/_rels\/sheet\d+\.xml\.rels/
|
104
|
-
sheet_id = entry.name.scan(/sheet(\d+)/).flatten.first
|
105
|
-
@relationships[sheet_id] = Relationships.new(entry.get_input_stream.read)
|
106
|
-
else
|
107
|
-
# unsupported for now..
|
106
|
+
spreadsheet_zip.each do |entry|
|
107
|
+
case entry.name
|
108
|
+
when /xl\/worksheets\/sheet(\d+)?\.xml/
|
109
|
+
sheet_id = entry.name.scan(/xl\/worksheets\/sheet(\d+)?\.xml/).flatten.first
|
110
|
+
@sheets[sheet_id] = OOXL::Sheet.new(entry.get_input_stream.read, shared_strings, @options)
|
111
|
+
when /xl\/styles\.xml/
|
112
|
+
@styles = OOXL::Styles.load_from_stream(entry.get_input_stream.read)
|
113
|
+
when /xl\/comments(\d+)?\.xml/
|
114
|
+
comment_id = entry.name.scan(/xl\/comments(\d+)\.xml/).flatten.first
|
115
|
+
@comments[comment_id] = OOXL::Comments.load_from_stream(entry.get_input_stream.read)
|
116
|
+
when "xl/sharedStrings.xml"
|
117
|
+
Nokogiri.XML(entry.get_input_stream.read).remove_namespaces!.xpath('sst/si').each do |shared_string_node|
|
118
|
+
shared_strings << shared_string_node.xpath('r/t|t').map { |value_node| value_node.text}.join('')
|
108
119
|
end
|
120
|
+
when /xl\/tables\/.*?/i
|
121
|
+
@tables << OOXL::Table.new(entry.get_input_stream.read)
|
122
|
+
when "xl/workbook.xml"
|
123
|
+
@workbook = OOXL::Workbook.load_from_stream(entry.get_input_stream.read)
|
124
|
+
when /xl\/worksheets\/_rels\/sheet\d+\.xml\.rels/
|
125
|
+
sheet_id = entry.name.scan(/sheet(\d+)/).flatten.first
|
126
|
+
@sheet_relationships[sheet_id] = Relationships.new(entry.get_input_stream.read)
|
127
|
+
when /xl\/_rels\/workbook\.xml\.rels/
|
128
|
+
@workbook_relationships = Relationships.new(entry.get_input_stream.read)
|
129
|
+
else
|
130
|
+
# unsupported for now..
|
109
131
|
end
|
110
132
|
end
|
111
133
|
end
|
data/lib/ooxl/version.rb
CHANGED
@@ -1,29 +1,41 @@
|
|
1
1
|
class OOXL
|
2
2
|
class Relationships
|
3
3
|
SUPPORTED_TYPES = ['http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments']
|
4
|
+
|
4
5
|
def initialize(relationships_node)
|
5
|
-
@
|
6
|
+
@relationships = []
|
6
7
|
parse_relationships(relationships_node)
|
7
8
|
end
|
8
9
|
|
9
10
|
def comment_id
|
10
|
-
|
11
|
+
comment_target = by_type('comments').first
|
12
|
+
comment_target && extract_file_reference(comment_target)
|
13
|
+
end
|
14
|
+
|
15
|
+
def [](id)
|
16
|
+
@relationships.find { |rel| rel.id == id }&.target
|
17
|
+
end
|
18
|
+
|
19
|
+
def by_type(type)
|
20
|
+
@relationships.select { |rel| rel.type == type }.map(&:target)
|
11
21
|
end
|
12
22
|
|
13
23
|
private
|
24
|
+
|
14
25
|
def parse_relationships(relationships_node)
|
15
26
|
relationships_node = Nokogiri.XML(relationships_node).remove_namespaces!
|
16
27
|
relationships_node.xpath('//Relationship').each do |relationship_node|
|
17
28
|
relationship_type = relationship_node.attributes["Type"].value
|
18
29
|
target = relationship_node.attributes["Target"].value
|
19
|
-
|
20
|
-
|
21
|
-
|
30
|
+
id = extract_number(relationship_node.attributes["Id"].value)
|
31
|
+
type = extract_type(relationship_type)
|
32
|
+
target = relationship_node.attributes["Target"].value
|
33
|
+
@relationships << Relationship.new(id, type, target)
|
22
34
|
end
|
23
35
|
end
|
24
36
|
|
25
|
-
def
|
26
|
-
|
37
|
+
def extract_number(str)
|
38
|
+
str.scan(/(\d+)/).flatten.first
|
27
39
|
end
|
28
40
|
|
29
41
|
def extract_type(type)
|
@@ -34,6 +46,7 @@ class OOXL
|
|
34
46
|
file.scan(/(\d+)\.[\w]/).flatten.first
|
35
47
|
end
|
36
48
|
|
49
|
+
Relationship = Struct.new(:id, :type, :target)
|
37
50
|
end
|
38
51
|
end
|
39
52
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ooxl
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.1.5.
|
4
|
+
version: 0.0.1.5.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- James Mones
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-10-
|
11
|
+
date: 2020-10-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -168,8 +168,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
168
168
|
- !ruby/object:Gem::Version
|
169
169
|
version: '0'
|
170
170
|
requirements: []
|
171
|
-
|
172
|
-
rubygems_version: 2.6.12
|
171
|
+
rubygems_version: 3.0.3
|
173
172
|
signing_key:
|
174
173
|
specification_version: 4
|
175
174
|
summary: OOXL Excel - Parse Excel Spreadsheets (xlsx, xlsm).
|