Class: Cosmos::ExcelSpreadsheet::ExcelWorksheet

Inherits:
Object
  • Object
show all
Defined in:
lib/cosmos/win32/excel.rb

Overview

Class to allow easy manipulation to the data in an Excel worksheet

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(worksheet) ⇒ ExcelWorksheet

Returns a new instance of ExcelWorksheet.

Parameters:

  • worksheet (WIN32OLE)

    The underlying Excel worksheet object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/cosmos/win32/excel.rb', line 24

def initialize(worksheet)
  @name = worksheet.name
  @num_rows = worksheet.UsedRange.rows.count
  @num_columns = worksheet.UsedRange.columns.count

  # Get Excel Data from Worksheet
  @data = worksheet.UsedRange.value

  # Build a lookup table based on the first column
  @lkup = {}
  if @data
    @data.each do |row|
      @lkup[row[0]] = row[1..-1]
    end
  end
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



21
22
23
# File 'lib/cosmos/win32/excel.rb', line 21

def data
  @data
end

#nameObject (readonly)

Returns the value of attribute name.



21
22
23
# File 'lib/cosmos/win32/excel.rb', line 21

def name
  @name
end

#num_columnsObject (readonly)

Returns the value of attribute num_columns.



21
22
23
# File 'lib/cosmos/win32/excel.rb', line 21

def num_columns
  @num_columns
end

#num_rowsObject (readonly)

Returns the value of attribute num_rows.



21
22
23
# File 'lib/cosmos/win32/excel.rb', line 21

def num_rows
  @num_rows
end

Instance Method Details

#[](index) ⇒ ExcelWorksheet

Access the lookup values by string or the raw data by index

Parameters:

  • index (String|Integer)

    Name of the first column or index

Returns:



49
50
51
52
53
54
55
# File 'lib/cosmos/win32/excel.rb', line 49

def [](index)
  if index.is_a? String
    @lkup[index]
  else
    @data[index]
  end
end

#keysObject



41
42
43
# File 'lib/cosmos/win32/excel.rb', line 41

def keys
  @lkup.keys
end