Class: Excelsioroo

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, sheet = nil) ⇒ Excelsioroo

Returns a new instance of Excelsioroo.



7
8
9
10
11
# File 'lib/excelsioroo.rb', line 7

def initialize filename, sheet = nil
  @spreadsheet = check_file filename
  sheet = @spreadsheet.sheets.first unless !sheet.nil?
  @spreadsheet.default_sheet = sheet
end

Instance Attribute Details

#spreadsheetObject (readonly)

Returns the value of attribute spreadsheet.



5
6
7
# File 'lib/excelsioroo.rb', line 5

def spreadsheet
  @spreadsheet
end

Instance Method Details

#check_file(filename) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/excelsioroo.rb', line 13

def check_file filename
  case filename
  when /.xls/
    document = Excel.new filename
  when /.xlsx/
    document = Excelx.new filename
  when /.ods/
    document = Openoffice.new filename
  end
  document
end

#get_data(start_column, start_row, end_column, end_row) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/excelsioroo.rb', line 25

def get_data start_column, start_row, end_column, end_row
  data, rows = [], []

  # Traverse the rows and columns
  start_row.to_i.upto end_row.to_i do |row|
    start_column.upto end_column do |column|
      data << "#{@spreadsheet.cell(row, column)} "
    end
    rows << data
  end
  puts JSON.generate(rows)
end