Class: ExcelReader

Inherits:
Object
  • Object
show all
Defined in:
lib/makesheets/reader.rb

Overview

Contains functionalities for reading content from Excel

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, sheet, skiprows, readrows, shtcol) ⇒ ExcelReader

:notnew:



8
9
10
11
12
13
14
# File 'lib/makesheets/reader.rb', line 8

def initialize(filename, sheet, skiprows, readrows, shtcol) #:notnew:
    @filename = filename
    @shtcol = shtcol
    @data = []
    @data_shtcols = [] # container to hold all unique data of makesheet col
    set_file_credentials(sheet, skiprows, readrows)
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



6
7
8
# File 'lib/makesheets/reader.rb', line 6

def data
  @data
end

#data_shtcolsObject (readonly)

Returns the value of attribute data_shtcols.



6
7
8
# File 'lib/makesheets/reader.rb', line 6

def data_shtcols
  @data_shtcols
end

#headersObject (readonly)

Returns the value of attribute headers.



6
7
8
# File 'lib/makesheets/reader.rb', line 6

def headers
  @headers
end

Instance Method Details

#readObject

Iterates over rows and stores the data as list of hashes



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/makesheets/reader.rb', line 28

def read
    puts "[Info]: Data reading on progress..."
    @lrow.times do |i|
        row_idx = i + 1
        next if row_idx <= @skiprows+1
        row_data = @sht.row(row_idx)
        row_dict = {}
        @headers.each_with_index do |h, j|
            temp_dict = { h => row_data[j] }
            @data_shtcols << row_data[j] if h == @shtcol
            row_dict.merge!(temp_dict)
        end
        @data << row_dict
        break if row_idx > @readrows
    end
    len_before = @data_shtcols.length
    @data_shtcols.uniq!
    len_after = @data_shtcols.length
    puts "[Info]: Column data count=#{len_before} [Bef] #{len_after} [Aft]"
end