Class: DYI::Chart::ExcelReader

Inherits:
ArrayReader show all
Defined in:
lib/dyi/chart/excel_reader.rb,
lib/ironruby.rb

Overview

ExcelReader class provides a interface to Microsoft Excel file and data for a chart object. Creating the instance, you should not call new method but ExcelReader.read method.

This class requires that win32ole has been installed your system.

See Also:

Since:

  • 0.0.0

Constant Summary collapse

OFFSET =

Since:

  • 0.0.0

DateTime.now.offset

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ArrayReader

#[], #clear_data, #each, #has_field?, #initialize, #members, #records, #records_size, #series, #values_each, #values_size

Constructor Details

This class inherits a constructor from DYI::Chart::ArrayReader

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class DYI::Chart::ArrayReader

Class Method Details

.read(path, options = {}) ⇒ ExcelReader

Parses Excel file and creates instance of ExcelReader.

Parameters:

  • path (String)

    a path of the Excel file

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :sheet (String, Integer)

    sheet name of data source or sheet number (starting from 1)

  • :row_range (Range)

    a range of rows

  • :column_range (Range)

    a range of columns

  • :schema (Array<Symbol>)

    array of field names. see Overview of ArrayReader.

  • :transposed (Boolean)

    whether the array-of-array is transposed

Returns:

Raises:

  • (NotImplementedError)

    win32ole has not been installed

See Also:

Since:

  • 0.0.0



117
118
119
# File 'lib/dyi/chart/excel_reader.rb', line 117

def read(path, options={})
  new.read(path, options)
end

Instance Method Details

#read(path, options = {}) ⇒ Object

Parses Excel file and sets data.

Parameters:

  • path (String)

    a path of the Excel file

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :sheet (String, Integer)

    sheet name of data source or sheet number (starting from 1)

  • :row_range (Range)

    a range of rows

  • :column_range (Range)

    a range of columns

  • :schema (Array<Symbol>)

    array of field names. see Overview of ArrayReader.

  • :transposed (Boolean)

    whether the array-of-array is transposed

Raises:

  • (NotImplementedError)

    win32ole has not been installed

See Also:

Since:

  • 0.0.0



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/dyi/chart/excel_reader.rb', line 53

def read(path, options={})
  if defined? WIN32OLE
    # for Windows
    path = WIN32OLE.new('Scripting.FileSystemObject').getAbsolutePathName(path)
    excel = WIN32OLE.new('Excel.Application')
    book = excel.workbooks.open(path)
    sheet = book.worksheets.item(options[:sheet] || 1)
    range = sheet.usedRange
    sheet_values = sheet.range(sheet.cells(1,1), sheet.cells(range.end(4).row, range.end(2).column)).value

    jagged_array = []
    sheet_values.get_length(0).times do |i|
      jagged_array << []
      sheet_values.get_length(1).times do |j|
        jagged_array[i] << sheet_values.get_value(i+1, j+1)
      end
    end
    sheet_values = jagged_array
  end

  begin
    super(sheet_values, options)
  ensure
    if defined? WIN32OLE
      book.close(false)
      excel.quit
      excel = sheet = nil
    end
    book = sheet_values = nil
    GC.start
  end
  self
end