Class: Sheet2hash::Workbook

Inherits:
Object
  • Object
show all
Includes:
Errors, Options
Defined in:
lib/sheet2hash.rb

Overview

Sheet2hash::Workbook converts Excel or Spreadsheet into Ruby Hash.

Author:

  • Wei-Ming Wu

Instance Method Summary collapse

Methods included from Options

#process_options

Constructor Details

#initialize(path, opts = {}) ⇒ Workbook

Creates a Workbook.



16
17
18
19
20
21
# File 'lib/sheet2hash.rb', line 16

def initialize path, opts = {}
  @workbook = Roo::Spreadsheet.open path
  @opts = process_options opts
  @sheet_opts = {}
  set_sheet_attributes
end

Instance Method Details

#sheetString

Returns the name of current sheet.

Returns:

  • (String)

    the name of current sheet



58
59
60
# File 'lib/sheet2hash.rb', line 58

def sheet
  @workbook.default_sheet
end

#sheetsArray

Returns all sheets of this Workbook.

Returns:

  • (Array)

    all sheets of this Workbook



51
52
53
# File 'lib/sheet2hash.rb', line 51

def sheets
  @workbook.sheets
end

#to_aObject

Converts current sheet to an Array of Hash.

Parameters:

  • Array (Array)

    of Hash



77
78
79
80
81
82
83
84
85
# File 'lib/sheet2hash.rb', line 77

def to_a
  ary = []
  @rows.each do |row|
    record = []
    @columns.each { |col| record << trim_int_cell(@workbook.cell(row, col)) }
    ary << Hash[ @header.zip record ]
  end
  ary
end

#to_hHash

Converts all sheets to a Hash, sheet names are the keys, values are Array of Hash.

Returns:

  • (Hash)

    sheet names are the keys, values are Array of Hash



65
66
67
68
69
70
71
72
# File 'lib/sheet2hash.rb', line 65

def to_h
  hash = {}
  sheets.each do |sheet|
    turn_to sheet
    hash[sheet] = to_a
  end
  hash
end

#turn_to(sheet, sheet_opts = {}) ⇒ Object

Turns to specified sheet of this Workbook.

Parameters:

  • sheet (String, Integer)

    the name or index(1-based) of a sheet

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

    the options of turn_to

Options Hash (sheet_opts):

  • :header (Array, Integer)

    using an Array to store the header of this sheet or choosing an index(1-based) of rows as the header

  • :start (Integer)

    the index(1-based) of rows to start

  • :end (Integer)

    the index(1-based) of rows to end

  • :keep_row (Integer)

    the indices(1-based) of rows to keep

  • :skip_row (Integer)

    the indices(1-based) of rows to skip

  • :keep_col (Integer)

    the indices(1-based) of columns to keep

  • :skip_col (Integer)

    the indices(1-based) of columns to skip



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/sheet2hash.rb', line 34

def turn_to sheet, sheet_opts = {}
  if sheet.kind_of?(Integer) && sheet <= sheets.size
    @workbook.default_sheet = sheets[sheet - 1]
    @sheet_opts = process_options sheet_opts
    set_sheet_attributes
  elsif sheet.kind_of?(String) && sheets.include?(sheet)
    @workbook.default_sheet = sheet
    @sheet_opts = process_options sheet_opts
    set_sheet_attributes
  else
    raise SheetNotFoundError
  end
end