Class: ExcelUtils::Workbooks::Excel

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Excel.



7
8
9
10
11
# File 'lib/excel_utils/workbooks/excel.rb', line 7

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.



5
6
7
# File 'lib/excel_utils/workbooks/excel.rb', line 5

def filename
  @filename
end

#normalize_column_namesObject (readonly)

Returns the value of attribute normalize_column_names.



5
6
7
# File 'lib/excel_utils/workbooks/excel.rb', line 5

def normalize_column_names
  @normalize_column_names
end

Instance Method Details

#[](sheet_name) ⇒ Object



21
22
23
# File 'lib/excel_utils/workbooks/excel.rb', line 21

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

#sheetsObject



13
14
15
16
17
18
19
# File 'lib/excel_utils/workbooks/excel.rb', line 13

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

#to_hObject



25
26
27
28
29
# File 'lib/excel_utils/workbooks/excel.rb', line 25

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