|
| 1 | +# Creek - Stream parser for large Excel (xlsx and xlsm) files. |
| 2 | + |
| 3 | +Creek is a Ruby gem that provides a fast, simple and efficient method of parsing large Excel (xlsx and xlsm) files. |
| 4 | + |
| 5 | + |
| 6 | +## Installation |
| 7 | + |
| 8 | +Creek can be used from the command line or as part of a Ruby web framework. To install the gem using terminal, run the following command: |
| 9 | + |
| 10 | +``` |
| 11 | +gem install creek |
| 12 | +``` |
| 13 | + |
| 14 | +To use it in Rails, add this line to your Gemfile: |
| 15 | + |
| 16 | +```ruby |
| 17 | +gem 'creek' |
| 18 | +``` |
| 19 | + |
| 20 | +## Basic Usage |
| 21 | +Creek can simply parse an Excel file by looping through the rows enumerator: |
| 22 | + |
| 23 | +```ruby |
| 24 | +require 'creek' |
| 25 | +creek = Creek::Book.new 'specs/fixtures/sample.xlsx' |
| 26 | +sheet= creek.sheets[0] |
| 27 | + |
| 28 | +sheet.rows.each do |row| |
| 29 | + puts row # => {"A1"=>"Content 1", "B1"=>nil, C1"=>nil, "D1"=>"Content 3"} |
| 30 | +end |
| 31 | + |
| 32 | +sheet.rows_with_meta_data.each do |row| |
| 33 | + puts row # => {"collapsed"=>"false", "customFormat"=>"false", "customHeight"=>"true", "hidden"=>"false", "ht"=>"12.1", "outlineLevel"=>"0", "r"=>"1", "cells"=>{"A1"=>"Content 1", "B1"=>nil, C1"=>nil, "D1"=>"Content 3"}} |
| 34 | +end |
| 35 | + |
| 36 | +sheet.state # => 'visible' |
| 37 | +sheet.name # => 'Sheet1' |
| 38 | +sheet.rid # => 'rId2' |
| 39 | +``` |
| 40 | + |
| 41 | +## Filename considerations |
| 42 | +By default, Creek will ensure that the file extension is either *.xlsx or *.xlsm, but this check can be circumvented as needed: |
| 43 | + |
| 44 | +```ruby |
| 45 | +path = 'sample-as-zip.zip' |
| 46 | +Creek::Book.new path, :check_file_extension => false |
| 47 | +``` |
| 48 | + |
| 49 | +By default, the Rails [file_field_tag](http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-file_field_tag) uploads to a temporary location and stores the original filename with the StringIO object. (See [this section](http://guides.rubyonrails.org/form_helpers.html#uploading-files) of the Rails Guides for more information.) |
| 50 | + |
| 51 | +Creek can parse this directly without the need for file upload gems such as Carrierwave or Paperclip by passing the original filename as an option: |
| 52 | + |
| 53 | +```ruby |
| 54 | +# Import endpoint in Rails controller |
| 55 | +def import |
| 56 | + file = params[:file] |
| 57 | + Creek::Book.new file.path, check_file_extension: false |
| 58 | +end |
| 59 | +``` |
| 60 | + |
| 61 | +## Parsing images |
| 62 | +Creek does not parse images by default. If you want to parse the images, |
| 63 | +use `with_images` method before iterating over rows to preload images information. If you don't call this method, Creek will not return images anywhere. |
| 64 | + |
| 65 | +Cells with images will be an array of Pathname objects. |
| 66 | +If an image is spread across multiple cells, same Pathname object will be returned for each cell. |
| 67 | + |
| 68 | +```ruby |
| 69 | +sheet.with_images.rows.each do |row| |
| 70 | + puts row # => {"A1"=>[#<Pathname:/var/folders/ck/l64nmm3d4k75pvxr03ndk1tm0000gn/T/creek__drawing20161101-53599-274q0vimage1.jpeg>], "B2"=>"Fluffy"} |
| 71 | +end |
| 72 | +``` |
| 73 | + |
| 74 | +Images for a specific cell can be obtained with images_at method: |
| 75 | + |
| 76 | +```ruby |
| 77 | +puts sheet.images_at('A1') # => [#<Pathname:/var/folders/ck/l64nmm3d4k75pvxr03ndk1tm0000gn/T/creek__drawing20161101-53599-274q0vimage1.jpeg>] |
| 78 | + |
| 79 | +# no images in a cell |
| 80 | +puts sheet.images_at('C1') # => nil |
| 81 | +``` |
| 82 | + |
| 83 | +Creek will most likely return nil for a cell with images if there is no other text cell in that row - you can use *images_at* method for retrieving images in that cell. |
| 84 | + |
| 85 | +## Remote files |
| 86 | + |
| 87 | +```ruby |
| 88 | +remote_url = 'http://dev-builds.libreoffice.org/tmp/test.xlsx' |
| 89 | +Creek::Book.new remote_url, remote: true |
| 90 | +``` |
| 91 | + |
| 92 | +## Contributing |
| 93 | + |
| 94 | +Contributions are welcomed. You can fork a repository, add your code changes to the forked branch, ensure all existing unit tests pass, create new unit tests which cover your new changes and finally create a pull request. |
| 95 | + |
| 96 | +After forking and then cloning the repository locally, install the Bundler and then use it |
| 97 | +to install the development gem dependencies: |
| 98 | + |
| 99 | +``` |
| 100 | +gem install bundler |
| 101 | +bundle install |
| 102 | +``` |
| 103 | + |
| 104 | +Once this is complete, you should be able to run the test suite: |
| 105 | + |
| 106 | +``` |
| 107 | +rake |
| 108 | +``` |
| 109 | + |
| 110 | +## Bug Reporting |
| 111 | + |
| 112 | +Please use the [Issues](https://github.com/pythonicrubyist/creek/issues) page to report bugs or suggest new enhancements. |
| 113 | + |
| 114 | + |
| 115 | +## License |
| 116 | + |
| 117 | +Creek has been published under [MIT License](https://github.com/pythonicrubyist/creek/blob/master/LICENSE.txt) |
0 commit comments