Class: ExcelUtils::Workbook

Inherits:
Object
  • Object
show all
Defined in:
lib/excel_utils/workbook.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, normalize_column_names: false, extension: nil) ⇒ Workbook

Returns a new instance of Workbook.



6
7
8
9
10
# File 'lib/excel_utils/workbook.rb', line 6

def initialize(filename, normalize_column_names: false, extension: nil)
  @filename = filename
  @normalize_column_names = normalize_column_names
  @spreadsheet = Roo::Spreadsheet.open filename, extension: extension
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



4
5
6
# File 'lib/excel_utils/workbook.rb', line 4

def filename
  @filename
end

#normalize_column_namesObject (readonly)

Returns the value of attribute normalize_column_names.



4
5
6
# File 'lib/excel_utils/workbook.rb', line 4

def normalize_column_names
  @normalize_column_names
end

Instance Method Details

#[](sheet_name) ⇒ Object



18
19
20
# File 'lib/excel_utils/workbook.rb', line 18

def [](sheet_name)
  sheets.detect { |sheet| sheet.name == sheet_name }
end

#sheetsObject



12
13
14
15
16
# File 'lib/excel_utils/workbook.rb', line 12

def sheets
  @sheets ||= spreadsheet.sheets.map do |name| 
    Sheet.new name, spreadsheet, normalize_column_names: normalize_column_names
  end
end

#to_hObject



22
23
24
25
26
# File 'lib/excel_utils/workbook.rb', line 22

def to_h
  sheets.each_with_object({}) do |sheet, hash|
    hash[sheet.name] = sheet.to_a
  end
end