-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathsheet.rb
More file actions
209 lines (182 loc) · 7.06 KB
/
sheet.rb
File metadata and controls
209 lines (182 loc) · 7.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# frozen_string_literal: true
require 'zip/filesystem'
require 'nokogiri'
module Creek
class Creek::Sheet
include Creek::Utils
HEADERS_ROW_NUMBER = '1'
SPREADSHEETML_URI = 'http://schemas.openxmlformats.org/spreadsheetml/2006/main'
attr_accessor :with_headers
attr_reader :book,
:name,
:sheetid,
:state,
:visible,
:rid,
:index,
:headers
def initialize(book, name, sheetid, state, visible, rid, sheetfile)
@book = book
@name = name
@sheetid = sheetid
@visible = visible
@rid = rid
@state = state
@sheetfile = sheetfile
@images_present = false
end
##
# Preloads images info (coordinates and paths) from related drawing.xml and drawing rels.
# Must be called before #rows method if you want to have images included.
# Returns self so you can chain the calls (sheet.with_images.rows).
def with_images
@drawingfile = extract_drawing_filepath
if @drawingfile
@drawing = Creek::Drawing.new(@book, @drawingfile.sub('..', 'xl'))
@images_present = @drawing.has_images?
end
self
end
##
# Extracts images for a cell to a temporary folder.
# Returns array of Pathnames for the cell.
# Returns nil if images asre not found for the cell or images were not preloaded with #with_images.
def images_at(cell)
@drawing.images_at(cell) if @images_present
end
##
# Provides an Enumerator that returns a hash representing each row.
# The key of the hash is the column ID and the value is the value of the cell.
def simple_rows
rows_generator false, true
end
##
# Provides an Enumerator that returns a hash representing each row.
# The key of the hash is the Cell id and the value is the value of the cell.
def rows
rows_generator false, false
end
##
# Provides an Enumerator that returns a hash representing each row.
# The hash contains meta data of the row and a 'cells' embended hash which contains the cell contents.
def rows_with_meta_data
rows_generator true, false
end
##
# Provides an Enumerator that returns a hash representing each row.
# The hash contains meta data of the row and a 'cells' embended hash which contains the cell contents.
def simple_rows_with_meta_data
rows_generator true, true
end
private
##
# Returns a hash per row that includes the cell ids and values.
# Empty cells will be also included in the hash with a nil value.
def rows_generator(include_meta_data = false, use_simple_rows_format = false)
path = (@sheetfile.start_with? '/xl/' or @sheetfile.start_with? 'xl/') ? @sheetfile : "xl/#{@sheetfile}"
return unless @book.files.file.exist?(path)
# SAX parsing, Each element in the stream comes through as two events:
# one to open the element and one to close it.
opener = Nokogiri::XML::Reader::TYPE_ELEMENT
closer = Nokogiri::XML::Reader::TYPE_END_ELEMENT
Enumerator.new do |y|
@headers = nil
row = nil
cells = {}
cell = nil
cell_type = nil
cell_style_idx = nil
@book.files.file.open(path) do |xml|
prefix = ''
name_row = 'row'
name_c = 'c'
name_v = 'v'
name_t = 't'
Nokogiri::XML::Reader.from_io(xml).each do |node|
if prefix.empty? && node.namespaces.any?
namespace = node.namespaces.detect { |_key, uri| uri == SPREADSHEETML_URI }
prefix = if namespace && namespace[0].start_with?('xmlns:')
namespace[0].delete_prefix('xmlns:') + ':'
else
''
end
name_row = "#{prefix}row"
name_c = "#{prefix}c"
name_v = "#{prefix}v"
name_t = "#{prefix}t"
end
node_name = node.name
node_type = node.node_type
if node_name == name_row && node_type == opener
row = node.attributes
row['cells'] = {}
cells = {}
y << (include_meta_data ? row : cells) if node.self_closing?
elsif node_name == name_row && node_type == closer
processed_cells = fill_in_empty_cells(cells, row['r'], cell, use_simple_rows_format)
@headers = processed_cells if with_headers && row['r'] == HEADERS_ROW_NUMBER
if @images_present
processed_cells.each do |cell_name, cell_value|
next unless cell_value.nil?
processed_cells[cell_name] = images_at(cell_name)
end
end
row['cells'] = processed_cells
y << (include_meta_data ? row : processed_cells)
elsif node_name == name_c && node_type == opener
cell_type = node.attributes['t']
cell_style_idx = node.attributes['s']
cell = node.attributes['r']
elsif (node_name == name_v || node_name == name_t) && node_type == opener
unless cell.nil?
node.read
cells[cell] = convert(node.value, cell_type, cell_style_idx)
end
end
end
end
end
end
def convert(value, type, style_idx)
style = @book.style_types[style_idx.to_i]
Creek::Styles::Converter.call(value, type, style, converter_options)
end
def converter_options
@converter_options ||= {
shared_strings: @book.shared_strings.dictionary,
base_date: @book.base_date
}
end
##
# The unzipped XML file does not contain any node for empty cells.
# Empty cells are being padded in using this function
def fill_in_empty_cells(cells, row_number, last_col, use_simple_rows_format)
new_cells = {}
return new_cells if cells.empty?
last_col = last_col.gsub(row_number, '')
('A'..last_col).to_a.each do |column|
id = cell_id(column, use_simple_rows_format, row_number)
new_cells[id] = cells["#{column}#{row_number}"]
end
new_cells
end
##
# Find drawing filepath for the current sheet.
# Sheet xml contains drawing relationship ID.
# Sheet relationships xml contains drawing file's location.
def extract_drawing_filepath
# Read drawing relationship ID from the sheet.
sheet_filepath = "xl/#{@sheetfile}"
drawing = parse_xml(sheet_filepath).css('drawing').first
return if drawing.nil?
drawing_rid = drawing.attributes['id'].value
# Read sheet rels to find drawing file's location.
sheet_rels_filepath = expand_to_rels_path(sheet_filepath)
parse_xml(sheet_rels_filepath).css("Relationship[@Id='#{drawing_rid}']").first.attributes['Target'].value
end
def cell_id(column, use_simple_rows_format, row_number)
return "#{column}#{row_number}" unless use_simple_rows_format
(with_headers && headers) ? headers[column] : column
end
end
end