1+ # frozen_string_literal: true
2+
13require 'zip/filesystem'
24require 'nokogiri'
35
46module Creek
57 class Creek ::Sheet
68 include Creek ::Utils
79
10+ HEADERS_ROW_NUMBER = '1'
11+
12+ attr_accessor :with_headers
813 attr_reader :book ,
914 :name ,
1015 :sheetid ,
1116 :state ,
1217 :visible ,
1318 :rid ,
14- :index
15-
19+ :index ,
20+ :headers
1621
17- def initialize book , name , sheetid , state , visible , rid , sheetfile
22+ def initialize ( book , name , sheetid , state , visible , rid , sheetfile )
1823 @book = book
1924 @name = name
2025 @sheetid = sheetid
@@ -46,7 +51,6 @@ def images_at(cell)
4651 @drawing . images_at ( cell ) if @images_present
4752 end
4853
49-
5054 ##
5155 # Provides an Enumerator that returns a hash representing each row.
5256 # The key of the hash is the column ID and the value is the value of the cell.
@@ -89,35 +93,37 @@ def rows_generator include_meta_data=false, use_simple_rows_format=false
8993 closer = Nokogiri ::XML ::Reader ::TYPE_END_ELEMENT
9094 Enumerator . new do |y |
9195 row , cells , cell = nil , { } , nil
92- cell_type = nil
96+ cell_type = nil
9397 cell_style_idx = nil
9498 @book . files . file . open ( path ) do |xml |
9599 Nokogiri ::XML ::Reader . from_io ( xml ) . each do |node |
96- if ( node . name . eql? 'row' ) and ( node . node_type . eql? opener )
100+ if node . name == 'row' && node . node_type == opener
97101 row = node . attributes
98- row [ 'cells' ] = Hash . new
99- cells = Hash . new
102+ row [ 'cells' ] = { }
103+ cells = { }
100104 y << ( include_meta_data ? row : cells ) if node . self_closing?
101- elsif ( node . name . eql? 'row' ) and ( node . node_type . eql? closer )
105+ elsif node . name == 'row' && node . node_type == closer
102106 processed_cells = fill_in_empty_cells ( cells , row [ 'r' ] , cell , use_simple_rows_format )
107+ @headers = processed_cells if row [ 'r' ] == HEADERS_ROW_NUMBER
103108
104109 if @images_present
105110 processed_cells . each do |cell_name , cell_value |
106111 next unless cell_value . nil?
112+
107113 processed_cells [ cell_name ] = images_at ( cell_name )
108114 end
109115 end
110116
111117 row [ 'cells' ] = processed_cells
112118 y << ( include_meta_data ? row : processed_cells )
113- elsif ( node . name . eql? 'c' ) and ( node . node_type . eql? opener )
119+ elsif node . name == 'c' && node . node_type == opener
114120 cell_type = node . attributes [ 't' ]
115121 cell_style_idx = node . attributes [ 's' ]
116122 cell = node . attributes [ 'r' ]
117- elsif ( [ 'v' , 't' ] . include? node . name ) and ( node . node_type . eql? opener )
123+ elsif %w[ v t ] . include? ( node . name ) && node . node_type == opener
118124 unless cell . nil?
119125 node . read
120- cells [ ( use_simple_rows_format ? cell . tr ( "0-9" , "" ) : cell ) ] = convert ( node . value , cell_type , cell_style_idx )
126+ cells [ cell ] = convert ( node . value , cell_type , cell_style_idx )
121127 end
122128 end
123129 end
@@ -142,15 +148,13 @@ def converter_options
142148 # The unzipped XML file does not contain any node for empty cells.
143149 # Empty cells are being padded in using this function
144150 def fill_in_empty_cells ( cells , row_number , last_col , use_simple_rows_format )
145- new_cells = Hash . new
151+ new_cells = { }
152+ return new_cells if cells . empty?
146153
147- unless cells . empty?
148- last_col = last_col . gsub ( row_number , '' )
149-
150- ( "A" ..last_col ) . to_a . each do |column |
151- id = use_simple_rows_format ? "#{ column } " : "#{ column } #{ row_number } "
152- new_cells [ id ] = cells [ id ]
153- end
154+ last_col = last_col . gsub ( row_number , '' )
155+ ( 'A' ..last_col ) . to_a . each do |column |
156+ id = cell_id ( column , use_simple_rows_format , row_number )
157+ new_cells [ id ] = cells [ "#{ column } #{ row_number } " ]
154158 end
155159
156160 new_cells
@@ -172,5 +176,11 @@ def extract_drawing_filepath
172176 sheet_rels_filepath = expand_to_rels_path ( sheet_filepath )
173177 parse_xml ( sheet_rels_filepath ) . css ( "Relationship[@Id='#{ drawing_rid } ']" ) . first . attributes [ 'Target' ] . value
174178 end
179+
180+ def cell_id ( column , use_simple_rows_format , row_number = '' )
181+ return "#{ column } #{ row_number } " unless use_simple_rows_format
182+
183+ with_headers && headers ? headers [ column ] : column
184+ end
175185 end
176186end
0 commit comments