Class: SimpleExcel::Worksheet

Inherits:
Object
  • Object
show all
Defined in:
lib/simple-excel/worksheet.rb

Defined Under Namespace

Classes: FileWithoutData, HeadersDoNotMatch

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(excel, headers, check_headers = true) ⇒ Object

Create new instance of SimpleExcel::Worksheet

Parameters:

  • excel (Object)

    Instance of Spreadsheet

  • headers (Array)

    Headers excel file

  • check_headers (Boolean) (defaults to: true)

    Check headers



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/simple-excel/worksheet.rb', line 17

def initialize(excel, headers, check_headers = true)
  @excel, @headers = excel, headers

  if check_headers
    @excel.worksheets.each_with_index do |sheet, index|
      @sheet = @excel.worksheet(index)
      return @sheet if @headers == sheet.row(0)
    end
    raise HeadersDoNotMatch, "File headers do not coincide with #{@headers}"
  else
    @sheet = @excel.worksheet(0)
  end
end

Instance Attribute Details

#excelObject (readonly)



8
9
10
# File 'lib/simple-excel/worksheet.rb', line 8

def excel
  @excel
end

#headersObject (readonly)



8
9
10
# File 'lib/simple-excel/worksheet.rb', line 8

def headers
  @headers
end

#sheetObject (readonly)



8
9
10
# File 'lib/simple-excel/worksheet.rb', line 8

def sheet
  @sheet
end

Instance Method Details

#each {|row| ... } ⇒ Object

Pass all rows excel file

Yields:

  • (row)

    Row excel file

Yield Parameters:

  • Hash (Hash)

    of values with headers

Raises:



40
41
42
43
44
45
46
# File 'lib/simple-excel/worksheet.rb', line 40

def each
  raise FileWithoutData, 'File contains no data' if no_data?
  (1..@sheet.last_row_index).each do |i|
    row = get_values(i)
    yield row
  end
end

#to_sString

Returns Class name.

Returns:

  • (String)

    Class name



32
33
34
# File 'lib/simple-excel/worksheet.rb', line 32

def to_s
  self.class
end