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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 72289ba7eb6dba7ef0e3feec1b329f59d5ed9547
4
- data.tar.gz: 568bfdd6c97d43698e665eac15f0e453fac151e3
2
+ SHA256:
3
+ metadata.gz: feba34a102bb13498ce6cfbbdad7ae2d9f02e4d5f872a92a1bd80a5f74c59d83
4
+ data.tar.gz: 94ab7441bbbe828a0a4fe0f3d98b97f62d2ae25f6aa052ce657f1567b868abca
5
5
  SHA512:
6
- metadata.gz: 4a8c5d321345da254a3c397dccc77d090435408f895c7b16b2877d6d24f59721c9cb1caffd4e39337e8ebec02495f15e3c0133f0e0faa6d58cb5f602ee419951
7
- data.tar.gz: e4e7aca211e24fb842b959b431850671191aed6292f7f5ef7cd8c31267564dde3ab467c9aca19351f2f3012c1d24e7b9cee6a8a231510b4073b1b7b077ef480e
6
+ metadata.gz: 8653a2a872e73055569eff3f7114a24ca76769f0251dd71e52bfb2a15e5c81f51fd6f4fd9ad8ce9d046099c2f07a44f16ab61e7e1d02380f98b6474e132571d4
7
+ data.tar.gz: 8e12f9a5b9f101a01ab33b991c6923f32daa926e16c3ffd2a9ce984b2a94d7e9a9e3a3cf8714b709051004af336fbd7d132c92e5f7de14bddc9bb5f9f0dc50fc
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require "bundler/setup"
4
- require "ooxml_excel"
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 "irb"
14
- IRB.start
13
+ require "pry"
14
+ Pry.start
@@ -3,20 +3,33 @@ class OOXL
3
3
  include ListHelper
4
4
  attr_reader :filename
5
5
 
6
- def initialize(spreadsheet_filepath, options={})
6
+ def initialize(filepath = nil, contents: nil, **options)
7
7
  @workbook = nil
8
8
  @sheets = {}
9
9
  @styles = []
10
10
  @comments = {}
11
- @relationships = {}
11
+ @workbook_relationships = nil
12
+ @sheet_relationships = {}
12
13
  @options = options
13
14
  @tables = []
14
- @filename = File.basename(spreadsheet_filepath)
15
- parse_spreadsheet_contents(spreadsheet_filepath)
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[:sheet_id]
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 = @relationships[sheet_index]
91
+ relationship = @sheet_relationships[sheet_index]
79
92
  @comments[relationship.comment_id] if relationship.present?
80
93
  end
81
94
 
82
- def parse_spreadsheet_contents(spreadsheet)
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
- Zip::File.open(spreadsheet) do |spreadsheet_zip|
85
- spreadsheet_zip.each do |entry|
86
- case entry.name
87
- when /xl\/worksheets\/sheet(\d+)?\.xml/
88
- sheet_id = entry.name.scan(/xl\/worksheets\/sheet(\d+)?\.xml/).flatten.first
89
- @sheets[sheet_id] = OOXL::Sheet.new(entry.get_input_stream.read, shared_strings, @options)
90
- when /xl\/styles\.xml/
91
- @styles = OOXL::Styles.load_from_stream(entry.get_input_stream.read)
92
- when /xl\/comments(\d+)?\.xml/
93
- comment_id = entry.name.scan(/xl\/comments(\d+)\.xml/).flatten.first
94
- @comments[comment_id] = OOXL::Comments.load_from_stream(entry.get_input_stream.read)
95
- when "xl/sharedStrings.xml"
96
- Nokogiri.XML(entry.get_input_stream.read).remove_namespaces!.xpath('sst/si').each do |shared_string_node|
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
@@ -1,3 +1,3 @@
1
1
  class OOXL
2
- VERSION = "0.0.1.5.3"
2
+ VERSION = "0.0.1.5.4"
3
3
  end
@@ -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
- @types = {}
6
+ @relationships = []
6
7
  parse_relationships(relationships_node)
7
8
  end
8
9
 
9
10
  def comment_id
10
- @types['comments']
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
- if supported_type?(relationship_type)
20
- @types[extract_type(relationship_type)] = extract_file_reference(target)
21
- end
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 supported_type?(type)
26
- SUPPORTED_TYPES.include?(type)
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.3
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-01 00:00:00.000000000 Z
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
- rubyforge_project:
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).