class Shoji::Excel::Reader
Public Class Methods
new(filename_or_content)
click to toggle source
# File lib/shoji/excel/reader.rb, line 7 def initialize(filename_or_content) @filename_or_content = filename_or_content end
valid_file?(filename_or_content)
click to toggle source
# 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
Public Instance Methods
foreach(opts = {}, &block)
click to toggle source
# 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 = {})
click to toggle source
# 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 = {})
click to toggle source
# File lib/shoji/excel/reader.rb, line 22 def rows(opts = {}) r = [] foreach(opts) do |row| r << row end r end
Private Instance Methods
process_rows(worksheet, opts = {}, &block)
click to toggle source
# File lib/shoji/excel/reader.rb, line 55 def process_rows(worksheet, opts = {}, &block) max = opts[:limit] idx = 0 idx -= 1 if opts[:use_header] worksheet.each do |row| cells = [] row.each do |c| cells << c end block.call(cells) idx += 1 break if max && max <= idx end end