creek 2.3 → 2.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
2
  SHA1:
3
- metadata.gz: fb5f52bba0a71fddfc27213389475ad881b61e33
4
- data.tar.gz: bad18bc3dadfb9412f354a2f527ede8cb16f8828
3
+ metadata.gz: bee3cecfd77048fd524de90b3c59d844295ac470
4
+ data.tar.gz: ea3765e651c0657920c714da61492afce161dcf4
5
5
  SHA512:
6
- metadata.gz: becc2db698fdc35b68279ec7df03c95c9b2d12920e2de4e34c83a724116174dfa81b4730152d5ef72b517b7cb26c4c3b5d6b89887a32f6e61bf62d2364344363
7
- data.tar.gz: c3164d65efb54c5d14d31fcd91fc865db85ae1b032015076a7b80e3b329248148c62b8ec907c9098fef3f34c964bca8b650021b8bfad37062a0c58d7b8f3b4b7
6
+ metadata.gz: 02de0c9ccc20d4489f66fcef6468e06adfba44320e0ccaf08091827706c53331d32fa641736a904c677e55e46c9ddde61894278eb63264d3567a551f4409fba1
7
+ data.tar.gz: '009e354166eba703d3ecd49dbc9b48114d612cd093a6904eb75fe42136016dfe3738f6229c910cd1565cc15e3cc26fbd772626b97ebf22904d59fe5c9ce14a3f'
data/README.md CHANGED
@@ -26,11 +26,19 @@ creek = Creek::Book.new 'spec/fixtures/sample.xlsx'
26
26
  sheet = creek.sheets[0]
27
27
 
28
28
  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"}
30
34
  end
31
35
 
32
36
  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"}}
34
42
  end
35
43
 
36
44
  sheet.state # => 'visible'
@@ -46,18 +46,33 @@ module Creek
46
46
  @drawing.images_at(cell) if @images_present
47
47
  end
48
48
 
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
+
49
57
  ##
50
58
  # Provides an Enumerator that returns a hash representing each row.
51
59
  # The key of the hash is the Cell id and the value is the value of the cell.
52
60
  def rows
53
- rows_generator
61
+ rows_generator false, false
54
62
  end
55
63
 
56
64
  ##
57
65
  # Provides an Enumerator that returns a hash representing each row.
58
66
  # The hash contains meta data of the row and a 'cells' embended hash which contains the cell contents.
59
67
  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
61
76
  end
62
77
 
63
78
  private
@@ -65,7 +80,7 @@ module Creek
65
80
  ##
66
81
  # Returns a hash per row that includes the cell ids and values.
67
82
  # 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
69
84
  path = if @sheetfile.start_with? "/xl/" or @sheetfile.start_with? "xl/" then @sheetfile else "xl/#{@sheetfile}" end
70
85
  if @book.files.file.exist?(path)
71
86
  # SAX parsing, Each element in the stream comes through as two events:
@@ -84,7 +99,7 @@ module Creek
84
99
  cells = Hash.new
85
100
  y << (include_meta_data ? row : cells) if node.self_closing?
86
101
  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)
88
103
 
89
104
  if @images_present
90
105
  processed_cells.each do |cell_name, cell_value|
@@ -99,13 +114,9 @@ module Creek
99
114
  cell_type = node.attributes['t']
100
115
  cell_style_idx = node.attributes['s']
101
116
  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)
107
118
  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)
109
120
  end
110
121
  end
111
122
  end
@@ -129,14 +140,14 @@ module Creek
129
140
  ##
130
141
  # The unzipped XML file does not contain any node for empty cells.
131
142
  # 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)
133
144
  new_cells = Hash.new
134
145
 
135
146
  unless cells.empty?
136
147
  last_col = last_col.gsub(row_number, '')
137
148
 
138
149
  ("A"..last_col).to_a.each do |column|
139
- id = "#{column}#{row_number}"
150
+ id = use_simple_rows_format ? "#{column}" : "#{column}#{row_number}"
140
151
  new_cells[id] = cells[id]
141
152
  end
142
153
  end
@@ -1,3 +1,3 @@
1
1
  module Creek
2
- VERSION = "2.3"
2
+ VERSION = "2.4"
3
3
  end
@@ -65,6 +65,16 @@ describe 'Creek parsing a sample XLSX file' do
65
65
  {'A6'=>'1', 'B6'=>'2', 'C6'=>'3'}, {'A7'=>'Content 15', 'B7'=>'Content 16', 'C7'=>'Content 18', 'D7'=>'Content 19'},
66
66
  {'A8'=>nil, 'B8'=>'Content 20', 'C8'=>nil, 'D8'=>nil, 'E8'=>nil, 'F8'=>'Content 21'},
67
67
  {'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}]
68
78
  end
69
79
 
70
80
  after(:all) do
@@ -83,6 +93,20 @@ describe 'Creek parsing a sample XLSX file' do
83
93
  expect(sheet.rid).to eql 'rId1'
84
94
  end
85
95
 
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
+
86
110
  it 'Parse rows with empty cells successfully.' do
87
111
  rows = Array.new
88
112
  row_count = 0
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: creek
3
3
  version: !ruby/object:Gem::Version
4
- version: '2.3'
4
+ version: '2.4'
5
5
  platform: ruby
6
6
  authors:
7
7
  - pythonicrubyist