Class: SimpleExcel
- Inherits:
-
Object
- Object
- SimpleExcel
- 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
- #excel ⇒ Object readonly
- #headers ⇒ Object readonly
- #worksheet ⇒ Object readonly
Instance Method Summary collapse
-
#each(check_headers = true, check_fields = true, exclude = []) {|row| ... } ⇒ Object
Pass all rows excel file.
-
#initialize(path_to_file_or_url, headers) ⇒ Object
constructor
Create new instance of SimpleExcel::Core.
-
#save_to_output(path) ⇒ Object
Save the excel file to the output.
-
#to_s ⇒ String
Class name.
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
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
#excel ⇒ Object (readonly)
22 23 24 |
# File 'lib/simple-excel.rb', line 22 def excel @excel end |
#headers ⇒ Object (readonly)
22 23 24 |
# File 'lib/simple-excel.rb', line 22 def headers @headers end |
#worksheet ⇒ Object (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
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
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_s ⇒ String
Returns Class name.
64 65 66 |
# File 'lib/simple-excel.rb', line 64 def to_s self.class end |