excelinator 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,146 @@
1
+ Excelinator
2
+ ===========
3
+ Small gem for generating _real_ Excel spreadsheets from existing CSV data and
4
+ HTML tables that fully supports UTF-8 characters.
5
+
6
+ Why?
7
+ ----
8
+ Well, when you're starting up and things are small and then things grow and
9
+ then people want reports and they'd like them in Excel most of the time and
10
+ then you realize you can just throw some cheap CSV views at them and Excel
11
+ will be fine with them and then you get even bigger and you start getting,
12
+ like, friggin' _international_ and then the French dudes are like, "My umlauts
13
+ look like chewed croissant" and you try prefixing your CSV with a UTF-8 BOM
14
+ and that makes some Excels happy (Windows) but not others (Mac) and you think
15
+ man I totally want to just, y'know, _eat_ the croissant like it was meant to
16
+ be eaten y'know? and there's a zillion spreadsheet gems out there already
17
+ except they all like merge back to that one spreadsheet gem, and you could go
18
+ XMLDoc maybe, but then it's just like lemme use the CSV I've already got and
19
+ try to not make me work much?
20
+
21
+ No Rails Required
22
+ -----------------
23
+ The heart of this gem has a couple of small methods to handle the
24
+ transformations. If that's all you need, you're good to go.
25
+
26
+ ###CSV
27
+
28
+ Call `Excelinator.csv_to_xls(csv_content)`. The csv_content will be parsed by
29
+ FasterCSV and converted to Excel spreadsheet contents ready to be saved to
30
+ file or sent across the wire.
31
+
32
+ ###HTML
33
+
34
+ Call `Excelinator.html_as_xls(html_content)`. The table element from the HTML
35
+ content is extracted, a meta tag indicating utf-8 encoding is prepended and
36
+ that's it. The resulting content isn't actually an Excel spreadsheet, just the
37
+ HTML data. But write this out to a file with an .xls extension and Excel will
38
+ open the contents and translate the `<table>` for you, formatting and all.
39
+
40
+ _NOTE: While some spreadsheet programs (e.g. Google Docs) will not translate
41
+ HTML tables like this, both Excel on Windows and Mac will as well as
42
+ OpenOffice._
43
+
44
+ But I Need Rails
45
+ ----------------
46
+ As you wish. As always, [TMTOWTDI](http://en.wikipedia.org/wiki/There's_more_than_one_way_to_do_it),
47
+ but here are a few usage options. All examples work in Rails 2 and 3, except
48
+ where noted.
49
+
50
+ If you want to make an explicit xls view that has CSV content in it:
51
+
52
+ ```ruby
53
+ class FooController < ApplicationController
54
+ def report
55
+ respond_to do |format|
56
+ format.html
57
+ format.csv
58
+ format.xls { render :xls => 'foo_report.xls' }
59
+ end
60
+ end
61
+ end
62
+ ```
63
+
64
+ Rails 2 doesn't support custom renderers, but the guts of our :xls renderer
65
+ are mixed into the controllers, so you can call it directly this way:
66
+
67
+ ```ruby
68
+ format.xls { send_xls_data 'foo_report.xls' }
69
+ ```
70
+
71
+ If you want to re-use an existing HTML view:
72
+
73
+ ```ruby
74
+ format.xls { send_xls_data 'foo_report.xls', :file => 'foo/report.html.erb' }
75
+ ```
76
+ _`:template` also works in place of `:file` in Rails 3. `render :xls =>` also
77
+ works in place of `send_xls_data` in Rails 3._
78
+
79
+ Also note, `send_xls_data` (the guts of `render :xls`) will parse the given
80
+ content and detect CSV or HTML, so no need to specify which is being passed in.
81
+
82
+ You can even go with just an explicit xls view and no controller code, but
83
+ you'll need to convert the CSV content yourself inside the view:
84
+
85
+ ```ruby
86
+ <%= Excelinator.csv_to_xls(render :file => 'foo/xls_view.csv.erb').html_safe %>
87
+ ```
88
+ _`:template` works in place of `:file` here as well in Rails 3._
89
+
90
+ Or ... refactor the CSV content to a format-less partial:
91
+
92
+ ```ruby
93
+ # _report.erb
94
+ <%= generate_csv_report %>
95
+
96
+ # report.csv.erb
97
+ <%= render :partial => 'report' %>
98
+
99
+ # report.xls.erb
100
+ <%= Excelinator.csv_to_xls(render :partial => 'report').html_safe %>
101
+ ```
102
+
103
+ There are test apps included in the source repo that exercise these different
104
+ options.
105
+
106
+ FAQ
107
+ ---
108
+
109
+ ###You lied when you said "_real_ Excel spreadsheets from ... HTML tables." What about converting HTML tables to a _real_ Excel file?
110
+
111
+ I did, and I apologize. Lemme know when your pull request is ready.
112
+
113
+ ###Are there any options to re-use CSV/HTML views with No additional controller/view code?
114
+
115
+ I've tinkered with it, but it requires a bit of duck punching of the Rails
116
+ rendering code.
117
+
118
+ ###What if I want to generate a real Excel spreadsheet from scratch with all sorts of awesome in it?
119
+
120
+ This gem uses `spreadsheet` under the covers. There are also others that
121
+ support a wide variety of Excel features:
122
+
123
+ - http://spreadsheet.rubyforge.org/
124
+ - http://surpass.ananelson.com/
125
+ - https://github.com/randym/axlsx
126
+
127
+ With any of these, you can create specific .xls views and have them use the
128
+ classes in these gems that let you define a Workbook with multiple Worksheets
129
+ with rows and columns of formatted formulas.
130
+
131
+ For support higher up the ladder within Rails and/or ActiveRecord, here are a
132
+ few options I've found, though I can't vouch for any. Search rubygems and
133
+ github for 'spreadsheet' 'excel' and 'xls' and you'll find lots of additional
134
+ projects. Most appear to use either the above Spreadsheet gem or generate
135
+ XMLDoc.
136
+
137
+ - https://github.com/glebm/to_spreadsheet
138
+ - https://github.com/splendeo/to_xls
139
+ - https://github.com/asanghi/excel_rails
140
+ - https://github.com/hallelujah/rails-excel
141
+ - https://github.com/liangwenke/to_xls-rails
142
+
143
+ ###Some of the links in the test Rails apps don't work
144
+
145
+ They're not all supposed to work. Think of it more as a workshop for example
146
+ code.
@@ -0,0 +1,12 @@
1
+ require 'excelinator/xls'
2
+ require 'excelinator/rails'
3
+ require 'excelinator/version'
4
+ require 'spreadsheet'
5
+ require 'fastercsv'
6
+
7
+ if defined?(Rails)
8
+ Excelinator::Rails.setup
9
+ class ActionController::Base
10
+ include Excelinator::Rails::ACMixin
11
+ end
12
+ end
@@ -0,0 +1,25 @@
1
+ module Excelinator
2
+ module Rails
3
+ def self.setup
4
+ require 'action_controller'
5
+
6
+ Mime::Type.register Excelinator::MIME_TYPE, :xls
7
+
8
+ self.add_renderer if ::Rails::VERSION::MAJOR >= 3
9
+ end
10
+
11
+ def self.add_renderer
12
+ ActionController::Renderers.add :xls do |filename, options|
13
+ send_xls_data(filename, options)
14
+ end
15
+ end
16
+
17
+ module ACMixin
18
+ def send_xls_data(filename, options={})
19
+ content = render_to_string(options)
20
+ xls_content = Excelinator.convert_content(content)
21
+ send_data(xls_content, :filename => filename, :type => Excelinator::MIME_TYPE, :disposition => 'inline')
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,3 @@
1
+ module Excelinator
2
+ VERSION = '1.0.0'
3
+ end
@@ -0,0 +1,39 @@
1
+ module Excelinator
2
+ MIME_TYPE = 'application/vnd.ms-excel'
3
+
4
+ # Detects HTML table content (with a rather stupid regex: /<table/) and re-uses it, or attempts to convert from
5
+ # CSV if HTML not detected.
6
+ def self.convert_content(content)
7
+ content =~ /<table/ ? Excelinator.html_as_xls(content) : Excelinator.csv_to_xls(content)
8
+ end
9
+
10
+ def self.csv_to_xls(csv_content)
11
+ ary = FasterCSV.parse(csv_content)
12
+ book = Spreadsheet::Workbook.new
13
+ sheet = book.create_worksheet
14
+ ary.each_with_index do |row_ary, index|
15
+ row = sheet.row(index)
16
+ row.push(*row_ary)
17
+ end
18
+ content = ''
19
+ ios = StringIO.new(content)
20
+ book.write(ios)
21
+ content
22
+ end
23
+
24
+ # This only strips a <table> out of the html and adds a meta tag for utf-8 support. Excel will open an .xls file
25
+ # with this content and grok it correctly (including formatting); however, many alternate spreadsheet applications
26
+ # will not do this.
27
+ #
28
+ # If the html_content is very large, the default behavior of scanning out the table contents will consume a lot
29
+ # of memory. If the :do_not_strip option is passed, this expensive scan call will be skipped and the entire
30
+ # contents will be returned.
31
+ #
32
+ # If you don't have need of utf-8 encoding, or want to prepend that yourself, there's no need to use this method.
33
+ def self.html_as_xls(html_content, options={})
34
+ encoding_meta_tag = '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">'
35
+ encoding_meta_tag + (options.delete(:do_not_strip) ? html_content : html_content.scan(/<table.*\/table>/mi).to_s)
36
+ end
37
+
38
+ # def self.html_to_xls might be nice to do - convert html table to _real_ xls file
39
+ end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: excelinator
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease:
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - chrismo
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-08-03 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: spreadsheet
23
+ prerelease: false
24
+ version_requirements: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - "="
28
+ - !ruby/object:Gem::Version
29
+ hash: 23
30
+ segments:
31
+ - 0
32
+ - 6
33
+ - 8
34
+ version: 0.6.8
35
+ type: :runtime
36
+ requirement: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: fastercsv
39
+ prerelease: false
40
+ version_requirements: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 3
46
+ segments:
47
+ - 0
48
+ version: "0"
49
+ type: :runtime
50
+ requirement: *id002
51
+ - !ruby/object:Gem::Dependency
52
+ name: rake
53
+ prerelease: false
54
+ version_requirements: &id003 !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ hash: 3
60
+ segments:
61
+ - 0
62
+ version: "0"
63
+ type: :development
64
+ requirement: *id003
65
+ - !ruby/object:Gem::Dependency
66
+ name: rspec
67
+ prerelease: false
68
+ version_requirements: &id004 !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ hash: 3
74
+ segments:
75
+ - 0
76
+ version: "0"
77
+ type: :development
78
+ requirement: *id004
79
+ description: convert your csv data and html tables to excel data
80
+ email:
81
+ - chrismo@clabs.org
82
+ executables: []
83
+
84
+ extensions: []
85
+
86
+ extra_rdoc_files: []
87
+
88
+ files:
89
+ - README.md
90
+ - lib/excelinator.rb
91
+ - lib/excelinator/rails.rb
92
+ - lib/excelinator/version.rb
93
+ - lib/excelinator/xls.rb
94
+ has_rdoc: true
95
+ homepage: https://github.com/livingsocial/excelinator
96
+ licenses: []
97
+
98
+ post_install_message:
99
+ rdoc_options: []
100
+
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ hash: 3
109
+ segments:
110
+ - 0
111
+ version: "0"
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ hash: 3
118
+ segments:
119
+ - 0
120
+ version: "0"
121
+ requirements: []
122
+
123
+ rubyforge_project:
124
+ rubygems_version: 1.6.2
125
+ signing_key:
126
+ specification_version: 3
127
+ summary: Excel Converter
128
+ test_files: []
129
+