Class: Stockboy::Readers::Spreadsheet

Inherits:
Stockboy::Reader show all
Defined in:
lib/stockboy/readers/spreadsheet.rb

Overview

Parse an Excel spreadsheet

Backed by Roo gem. See roo for other configuration options.

Instance Attribute Summary collapse

Attributes inherited from Stockboy::Reader

#encoding

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}, &block) ⇒ Spreadsheet

Initialize a new Spreadsheet reader

Parameters:

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


75
76
77
78
79
80
81
82
83
84
85
# File 'lib/stockboy/readers/spreadsheet.rb', line 75

def initialize(opts={}, &block)
  super
  @format = opts[:format] || :xls
  @sheet  = opts[:sheet]  || :first
  @first_row = opts[:first_row]
  @last_row  = opts[:last_row]
  @header_row  = opts[:header_row]
  @headers = opts[:headers]
  @options = opts[:options] || {}
  DSL.new(self).instance_eval(&block) if block_given?
end

Instance Attribute Details

#first_rowInteger

Line number of first data row, starts counting at 1, like in Excel

Returns:

  • (Integer)


43
# File 'lib/stockboy/readers/spreadsheet.rb', line 43

dsl_attr :first_row

#formatSymbol

Spreadsheet format

Returns:

  • (Symbol)

    :xls or :xslx



19
# File 'lib/stockboy/readers/spreadsheet.rb', line 19

dsl_attr :format

#header_rowInteger

Line number to look for headers, starts counting at 1, like in Excel

When specified without first_row, then the next row becomes the first data row by default.

Returns:

  • (Integer)


36
# File 'lib/stockboy/readers/spreadsheet.rb', line 36

dsl_attr :header_row

#headersArray

Override to set headers manually

When specified, the first spreadsheet row is the default first data row, unless specified by first_row.

Returns:

  • (Array)


60
# File 'lib/stockboy/readers/spreadsheet.rb', line 60

dsl_attr :headers

#last_rowInteger

Line number of last data row, use negative numbers to count back from end

Returns:

  • (Integer)


50
# File 'lib/stockboy/readers/spreadsheet.rb', line 50

dsl_attr :last_row

#optionsHash

Options passed to underlying Roo library

Returns:

  • (Hash)


67
# File 'lib/stockboy/readers/spreadsheet.rb', line 67

dsl_attr :options

#sheetInteger

Spreadsheet sheet number, defaults to first

Returns:

  • (Integer)


26
# File 'lib/stockboy/readers/spreadsheet.rb', line 26

dsl_attr :sheet

Instance Method Details

#parse(content) ⇒ Object



87
88
89
90
91
92
93
94
95
# File 'lib/stockboy/readers/spreadsheet.rb', line 87

def parse(content)
  with_spreadsheet_tempfile(content) do |table|
    headers = table_headers(table)

    enum_data_rows(table).inject([]) do |rows, i|
      rows << Hash[headers.zip(table.row(i))]
    end
  end
end