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