Skip to content

Commit dcae13e

Browse files
[ creek #61 ] implemented.
1 parent 1ae72a1 commit dcae13e

4 files changed

Lines changed: 58 additions & 15 deletions

File tree

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,19 @@ creek = Creek::Book.new 'spec/fixtures/sample.xlsx'
2626
sheet = creek.sheets[0]
2727

2828
sheet.rows.each do |row|
29-
puts row # => {"A1"=>"Content 1", "B1"=>nil, C1"=>nil, "D1"=>"Content 3"}
29+
puts row # => {"A1"=>"Content 1", "B1"=>nil, "C1"=>nil, "D1"=>"Content 3"}
30+
end
31+
32+
sheet.simple_rows.each do |row|
33+
puts row # => {"A"=>"Content 1", "B"=>nil, "C"=>nil, "D"=>"Content 3"}
3034
end
3135

3236
sheet.rows_with_meta_data.each do |row|
33-
puts row # => {"collapsed"=>"false", "customFormat"=>"false", "customHeight"=>"true", "hidden"=>"false", "ht"=>"12.1", "outlineLevel"=>"0", "r"=>"1", "cells"=>{"A1"=>"Content 1", "B1"=>nil, C1"=>nil, "D1"=>"Content 3"}}
37+
puts row # => {"collapsed"=>"false", "customFormat"=>"false", "customHeight"=>"true", "hidden"=>"false", "ht"=>"12.1", "outlineLevel"=>"0", "r"=>"1", "cells"=>{"A1"=>"Content 1", "B1"=>nil, "C1"=>nil, "D1"=>"Content 3"}}
38+
end
39+
40+
sheet.simple_rows_with_meta_data.each do |row|
41+
puts row # => {"collapsed"=>"false", "customFormat"=>"false", "customHeight"=>"true", "hidden"=>"false", "ht"=>"12.1", "outlineLevel"=>"0", "r"=>"1", "cells"=>{"A"=>"Content 1", "B"=>nil, "C"=>nil, "D"=>"Content 3"}}
3442
end
3543

3644
sheet.state # => 'visible'

lib/creek/sheet.rb

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,26 +46,41 @@ def images_at(cell)
4646
@drawing.images_at(cell) if @images_present
4747
end
4848

49+
50+
##
51+
# Provides an Enumerator that returns a hash representing each row.
52+
# The key of the hash is the column ID and the value is the value of the cell.
53+
def simple_rows
54+
rows_generator false, true
55+
end
56+
4957
##
5058
# Provides an Enumerator that returns a hash representing each row.
5159
# The key of the hash is the Cell id and the value is the value of the cell.
5260
def rows
53-
rows_generator
61+
rows_generator false, false
5462
end
5563

5664
##
5765
# Provides an Enumerator that returns a hash representing each row.
5866
# The hash contains meta data of the row and a 'cells' embended hash which contains the cell contents.
5967
def rows_with_meta_data
60-
rows_generator true
68+
rows_generator true, false
69+
end
70+
71+
##
72+
# Provides an Enumerator that returns a hash representing each row.
73+
# The hash contains meta data of the row and a 'cells' embended hash which contains the cell contents.
74+
def simple_rows_with_meta_data
75+
rows_generator true, true
6176
end
6277

6378
private
6479

6580
##
6681
# Returns a hash per row that includes the cell ids and values.
6782
# Empty cells will be also included in the hash with a nil value.
68-
def rows_generator include_meta_data=false
83+
def rows_generator include_meta_data=false, use_simple_rows_format=false
6984
path = if @sheetfile.start_with? "/xl/" or @sheetfile.start_with? "xl/" then @sheetfile else "xl/#{@sheetfile}" end
7085
if @book.files.file.exist?(path)
7186
# SAX parsing, Each element in the stream comes through as two events:
@@ -84,7 +99,7 @@ def rows_generator include_meta_data=false
8499
cells = Hash.new
85100
y << (include_meta_data ? row : cells) if node.self_closing?
86101
elsif (node.name.eql? 'row') and (node.node_type.eql? closer)
87-
processed_cells = fill_in_empty_cells(cells, row['r'], cell)
102+
processed_cells = fill_in_empty_cells(cells, row['r'], cell, use_simple_rows_format)
88103

89104
if @images_present
90105
processed_cells.each do |cell_name, cell_value|
@@ -99,13 +114,9 @@ def rows_generator include_meta_data=false
99114
cell_type = node.attributes['t']
100115
cell_style_idx = node.attributes['s']
101116
cell = node.attributes['r']
102-
elsif (node.name.eql? 'v') and (node.node_type.eql? opener)
103-
unless cell.nil?
104-
cells[cell] = convert(node.inner_xml, cell_type, cell_style_idx)
105-
end
106-
elsif (node.name.eql? 't') and (node.node_type.eql? opener)
117+
elsif (['v', 't'].include? node.name) and (node.node_type.eql? opener)
107118
unless cell.nil?
108-
cells[cell] = convert(node.inner_xml, cell_type, cell_style_idx)
119+
cells[(use_simple_rows_format ? cell.tr("0-9", "") : cell)] = convert(node.inner_xml, cell_type, cell_style_idx)
109120
end
110121
end
111122
end
@@ -129,14 +140,14 @@ def converter_options
129140
##
130141
# The unzipped XML file does not contain any node for empty cells.
131142
# Empty cells are being padded in using this function
132-
def fill_in_empty_cells(cells, row_number, last_col)
143+
def fill_in_empty_cells(cells, row_number, last_col, use_simple_rows_format)
133144
new_cells = Hash.new
134145

135146
unless cells.empty?
136147
last_col = last_col.gsub(row_number, '')
137148

138149
("A"..last_col).to_a.each do |column|
139-
id = "#{column}#{row_number}"
150+
id = use_simple_rows_format ? "#{column}" : "#{column}#{row_number}"
140151
new_cells[id] = cells[id]
141152
end
142153
end

lib/creek/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module Creek
2-
VERSION = "2.3"
2+
VERSION = "2.4"
33
end

spec/test_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,16 @@
6565
{'A6'=>'1', 'B6'=>'2', 'C6'=>'3'}, {'A7'=>'Content 15', 'B7'=>'Content 16', 'C7'=>'Content 18', 'D7'=>'Content 19'},
6666
{'A8'=>nil, 'B8'=>'Content 20', 'C8'=>nil, 'D8'=>nil, 'E8'=>nil, 'F8'=>'Content 21'},
6767
{'A10' => 0.15, 'B10' => 0.15}]
68+
69+
@expected_simple_rows = [{"A"=>"Content 1", "B"=>nil, "C"=>"Content 2", "D"=>nil, "E"=>"Content 3"},
70+
{"A"=>nil, "B"=>"Content 4", "C"=>nil, "D"=>"Content 5", "E"=>nil, "F"=>"Content 6"},
71+
{},
72+
{"A"=>"Content 7", "B"=>"Content 8", "C"=>"Content 9", "D"=>"Content 10", "E"=>"Content 11", "F"=>"Content 12"},
73+
{"A"=>nil, "B"=>nil, "C"=>nil, "D"=>nil, "E"=>nil, "F"=>nil, "G"=>nil, "H"=>nil, "I"=>nil, "J"=>nil, "K"=>nil, "L"=>nil, "M"=>nil, "N"=>nil, "O"=>nil, "P"=>nil, "Q"=>nil, "R"=>nil, "S"=>nil, "T"=>nil, "U"=>nil, "V"=>nil, "W"=>nil, "X"=>nil, "Y"=>nil, "Z"=>"Z Content", "AA"=>nil, "AB"=>nil, "AC"=>nil, "AD"=>nil, "AE"=>nil, "AF"=>nil, "AG"=>nil, "AH"=>nil, "AI"=>nil, "AJ"=>nil, "AK"=>nil, "AL"=>nil, "AM"=>nil, "AN"=>nil, "AO"=>nil, "AP"=>nil, "AQ"=>nil, "AR"=>nil, "AS"=>nil, "AT"=>nil, "AU"=>nil, "AV"=>nil, "AW"=>nil, "AX"=>nil, "AY"=>nil, "AZ"=>"Content 13"},
74+
{"A"=>"1", "B"=>"2", "C"=>"3"},
75+
{"A"=>"Content 15", "B"=>"Content 16", "C"=>"Content 18", "D"=>"Content 19"},
76+
{"A"=>nil, "B"=>"Content 20", "C"=>nil, "D"=>nil, "E"=>nil, "F"=>"Content 21"},
77+
{"A"=>0.15, "B"=>0.15}]
6878
end
6979

7080
after(:all) do
@@ -83,6 +93,20 @@
8393
expect(sheet.rid).to eql 'rId1'
8494
end
8595

96+
it 'Parse simple rows successfully.' do
97+
rows = Array.new
98+
row_count = 0
99+
@creek.sheets[0].simple_rows.each do |row|
100+
rows << row
101+
row_count += 1
102+
end
103+
(0..8).each do |number|
104+
expect(rows[number]).to eq(@expected_simple_rows[number])
105+
end
106+
expect(row_count).to eq(9)
107+
end
108+
109+
86110
it 'Parse rows with empty cells successfully.' do
87111
rows = Array.new
88112
row_count = 0

0 commit comments

Comments
 (0)