class SimpleExcel

Constants

EXTENSIONS_AVAILABLE
VERSION

Attributes

excel[R]

@attr_reader [Object] excel Instance of Spreadsheet::Excel::Workbook @attr_reader [Array] headers Headers excel file @attr_reader [Object] worksheet Instance of SimpleExcel::Worksheet

headers[R]

@attr_reader [Object] excel Instance of Spreadsheet::Excel::Workbook @attr_reader [Array] headers Headers excel file @attr_reader [Object] worksheet Instance of SimpleExcel::Worksheet

worksheet[R]

@attr_reader [Object] excel Instance of Spreadsheet::Excel::Workbook @attr_reader [Array] headers Headers excel file @attr_reader [Object] worksheet Instance of SimpleExcel::Worksheet

Public Class Methods

new(path_to_file_or_url, headers) click to toggle source

Create new instance of SimpleExcel::Core

@todo Check the supported extensions Before opening the file to check the supported extension Use SimpleExcel::Core::EXTENSIONS_AVAILABLE

@param [String] path_to_file_or_url The path or link to excel file @param [Array] headers Headers excel file

@return [Object] Instance of SimpleExcel::Core

# File lib/simple-excel.rb, line 34
def initialize(path_to_file_or_url, headers)
  raise ArgumentError, 'Path to file or url a blank' if path_to_file_or_url.blank?

  # Open URL or file
  file = if /^http/ =~ path_to_file_or_url
           begin
             open(path_to_file_or_url)
           rescue
             raise InvalidUrl, "Invalid URL: #{path_to_file_or_url}"
           end
         else
           begin
             File.open(path_to_file_or_url)
           rescue Errno::ENOENT
             raise FileNotFound, "File '#{path_to_file_or_url}' not found"
           end
         end

  begin
    @excel = Spreadsheet.open(file)
  rescue
    raise UnsupportedExcelFile, "File '#{path_to_file_or_url}' not supported"
  end

  raise RequiredHeaders, 'Headers is required' if headers.blank?
  raise HeadersMustBeAnArray, 'Headers are not an array' unless headers.instance_of? Array
  @headers = headers
end

Public Instance Methods

each(check_headers = true, check_fields = true, exclude = []) { |row| ... } click to toggle source

Pass all rows excel file

@param [Boolean] check_headers Check headers @param [Boolean] check_fields Check filled fields @param [Array] exclude Exclude checking headers

@yield [row] Row excel file @yieldparam [Hash] Hash of values with headers

# File lib/simple-excel.rb, line 76
def each(check_headers = true, check_fields = true, exclude = [])
  @worksheet = SimpleExcel::Worksheet.new(@excel, @headers, check_headers)

  @worksheet.each do |row|
    checking_fill_fields(row, exclude) if check_fields
    yield row
  end
end
save_to_output(path) click to toggle source

Save the excel file to the output

@param [String] path Path to file @return [Object] Instance of SimpleExcel::ExtStringIO

# File lib/simple-excel.rb, line 89
def save_to_output(path)
  output = SimpleExcel::ExtStringIO.new
  @excel.write(output)
  output.filepath = path
  return output
end
to_s() click to toggle source

@return [String] Class name

# File lib/simple-excel.rb, line 64
def to_s
  self.class
end

Private Instance Methods

checking_fill_fields(row, exclude = []) click to toggle source

Checking fill fields excluding some fields

@param [Hash] row Row excel file @param [Array] exclude Exclude checking headers

# File lib/simple-excel.rb, line 102
def checking_fill_fields(row, exclude = [])
  check_headers = @headers.clone
  if exclude.present?
    raise ExcludeMustBeAnArray, 'Headers are not an array' unless exclude.instance_of? Array
    exclude.each{ |column| check_headers.delete(column) }
  end

  check_headers.each do |name|
    raise NotFilledField, "Field '#{name}' is not filled" if row[name].blank?
  end
end