Class: Shoji::Excel::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/shoji/excel/reader.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename_or_content) ⇒ Reader

Returns a new instance of Reader.



7
8
9
# File 'lib/shoji/excel/reader.rb', line 7

def initialize(filename_or_content)
  @filename_or_content = filename_or_content
end

Class Method Details

.valid_file?(filename_or_content) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
14
15
16
17
18
19
20
# File 'lib/shoji/excel/reader.rb', line 11

def self.valid_file?(filename_or_content)
  valid = true
  begin
    Spreadsheet.open(filename_or_content) do |workbook|
    end
  rescue
    valid = false
  end
  valid
end

Instance Method Details

#foreach(opts = {}, &block) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/shoji/excel/reader.rb', line 44

def foreach(opts = {}, &block)
  sheet_index = opts[:sheet_index] || 0
  Spreadsheet.open(@filename_or_content) do |workbook|
    num_sheets = workbook.worksheets.size
    return [] if num_sheets == 0 || num_sheets <= sheet_index
    worksheet = workbook.worksheet(sheet_index)
    process_rows(worksheet, opts, &block)
  end
end

#row_size(opts = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/shoji/excel/reader.rb', line 30

def row_size(opts = {})
  return @row_size if @row_size
  sheet_index = opts[:sheet_index] || 0
  idx = 0
  Spreadsheet.open(@filename_or_content) do |workbook|
    num_sheets = workbook.worksheets.size
    return [] if num_sheets == 0 || num_sheets <= sheet_index
    worksheet = workbook.worksheet(sheet_index)
    @row_size = worksheet.row_count
    @row_size -= 1 if opts[:use_header]
  end
  @row_size
end

#rows(opts = {}) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/shoji/excel/reader.rb', line 22

def rows(opts = {})
  r = []
  foreach(opts) do |row|
    r << row
  end
  r
end