Module: RemoteTable::RooSpreadsheet

Defined in:
lib/remote_table/file/roo_spreadsheet.rb

Instance Method Summary collapse

Instance Method Details

#each_row(&block) ⇒ Object



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
# File 'lib/remote_table/file/roo_spreadsheet.rb', line 3

def each_row(&block)
  oo = roo_klass.new(path, nil, :ignore)
  oo.default_sheet = sheet.is_a?(Numeric) ? oo.sheets[sheet] : sheet
  column_references = Hash.new
  if headers == false
    # zero-based numeric keys
    for col in (1..oo.last_column)
      column_references[col] = col - 1
    end
  elsif headers.is_a? Array
    # names
    for col in (1..oo.last_column)
      column_references[col] = headers[col - 1]
    end
  else
    # read headers from the file itself
    for col in (1..oo.last_column)
      column_references[col] = oo.cell(header_row, col)
      column_references[col] = oo.cell(header_row - 1, col) if column_references[col].blank? # look up
    end
  end
  first_data_row.upto(oo.last_row) do |raw_row|
    ordered_hash = ActiveSupport::OrderedHash.new
    for col in (1..oo.last_column)
      next if column_references[col].blank?
      ordered_hash[column_references[col]] = oo.cell(raw_row, col).to_s.gsub(/<[^>]+>/, '').strip
    end
    yield ordered_hash if keep_blank_rows or ordered_hash.any? { |k, v| v.present? }
  end
end