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



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/cosmos/win32/excel.rb', line 33

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.



30
31
32
# File 'lib/cosmos/win32/excel.rb', line 30

def data
  @data
end

#nameObject (readonly)

Returns the value of attribute name.



30
31
32
# File 'lib/cosmos/win32/excel.rb', line 30

def name
  @name
end

#num_columnsObject (readonly)

Returns the value of attribute num_columns.



30
31
32
# File 'lib/cosmos/win32/excel.rb', line 30

def num_columns
  @num_columns
end

#num_rowsObject (readonly)

Returns the value of attribute num_rows.



30
31
32
# File 'lib/cosmos/win32/excel.rb', line 30

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:



58
59
60
61
62
63
64
# File 'lib/cosmos/win32/excel.rb', line 58

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

#keysObject



50
51
52
# File 'lib/cosmos/win32/excel.rb', line 50

def keys
  @lkup.keys
end