Class: RubyCharts::Loaders::XLSXLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_charts/loaders/xlsx_loader.rb

Class Method Summary collapse

Class Method Details

.load(filepath, options = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ruby_charts/loaders/xlsx_loader.rb', line 6

def self.load(filepath, options = {})
  raise "File not found: #{filepath}" unless File.exist?(filepath)
  
  workbook = Roo::Excelx.new(filepath)
  sheet = options[:sheet] || workbook.default_sheet
  workbook.sheet(sheet)
  
  labels = []
  values = []
  
  (2..workbook.last_row).each do |row|
    label = workbook.cell(row, 1)
    value = workbook.cell(row, 2)
    
    next if label.nil? || value.nil?
    
    labels << label.to_s
    values << value.to_f
  end
  
  {
    labels: labels,
    values: values,
    type: options[:type] || :bar
  }
end