Class: SimpleExcel

Inherits:
Object
  • Object
show all
Defined in:
lib/simple-excel.rb,
lib/simple-excel/version.rb

Defined Under Namespace

Classes: ExcludeMustBeAnArray, ExtStringIO, FileNotFound, HeadersMustBeAnArray, InvalidUrl, NotFilledField, RequiredHeaders, UnsupportedExcelFile, Worksheet

Constant Summary collapse

EXTENSIONS_AVAILABLE =
%w(xls)
VERSION =
'0.1.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path_to_file_or_url, headers) ⇒ Object

TODO:

Check the supported extensions

Create new instance of SimpleExcel::Core

Before opening the file to check the supported extension Use SimpleExcel::Core::EXTENSIONS_AVAILABLE

Parameters:

  • path_to_file_or_url (String)

    The path or link to excel file

  • headers (Array)

    Headers excel file

Raises:

  • (ArgumentError)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 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

Instance Attribute Details

#excelObject (readonly)



22
23
24
# File 'lib/simple-excel.rb', line 22

def excel
  @excel
end

#headersObject (readonly)



22
23
24
# File 'lib/simple-excel.rb', line 22

def headers
  @headers
end

#worksheetObject (readonly)



22
23
24
# File 'lib/simple-excel.rb', line 22

def worksheet
  @worksheet
end

Instance Method Details

#each(check_headers = true, check_fields = true, exclude = []) {|row| ... } ⇒ Object

Pass all rows excel file

Parameters:

  • check_headers (Boolean) (defaults to: true)

    Check headers

  • check_fields (Boolean) (defaults to: true)

    Check filled fields

  • exclude (Array) (defaults to: [])

    Exclude checking headers

Yields:

  • (row)

    Row excel file

Yield Parameters:

  • Hash (Hash)

    of values with headers



76
77
78
79
80
81
82
83
# 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) ⇒ Object

Save the excel file to the output

Parameters:

  • path (String)

    Path to file

Returns:

  • (Object)

    Instance of SimpleExcel::ExtStringIO



89
90
91
92
93
94
# 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_sString

Returns Class name.

Returns:

  • (String)

    Class name



64
65
66
# File 'lib/simple-excel.rb', line 64

def to_s
  self.class
end