to_spreadsheet 1.0.4 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: c7bb1fac67822b9d246906d3969e7edd895cf312
4
- data.tar.gz: 7d4ff08a4596f79467489561eb82c18547e7e87b
2
+ SHA256:
3
+ metadata.gz: 59db3fcd754f0a95dd09f3fe7ffae57462a002745b6993a9fc3baa84014378ed
4
+ data.tar.gz: 831e9e4a81117cb1904bf503a05d1e3bb24ed817f90de0f031d05cb292308143
5
5
  SHA512:
6
- metadata.gz: 15cdd95363abc3f088e80fac952e5d6485772cf622c7f479a22f10503328b95a148d43a50ef823b727714f9b8d10e9dedda04c6ac226c4109d934d866460472f
7
- data.tar.gz: 050ad9400c2843ae6cd023dc2530a02747d510b04b3e5f06647cbf0f9a6f4a224a44f249692a2980f575e4d1347811a24d6c1701ffaa871b913c3ebeda3f034a
6
+ metadata.gz: efe03b92346c0fb021f2d5c4780966bb5267b448e0c209a48a14631af6b8031970d8e09f01c3b5594e72ead6aa89bae257e577ea9f9733bcba059795cbc96b37
7
+ data.tar.gz: '09271ae875d3d5e3ccddf69a5144afabdbf16e481b05633d4dfeb7aec2132fcea2024b233c65971a870c4d0fdc18ddea0bd4c3053c7d4eef10e178a5ec3d87b1'
data/README.md ADDED
@@ -0,0 +1,149 @@
1
+ # to_spreadsheet [![Build Status](https://secure.travis-ci.org/glebm/to_spreadsheet.png?branch=master)](http://travis-ci.org/glebm/to_spreadsheet)
2
+
3
+ to_spreadsheet lets your Rails 3+ app render Excel files using the existing slim/haml/erb/etc views.
4
+
5
+ Installation
6
+ ------------
7
+
8
+ Add it to your Gemfile:
9
+ ```ruby
10
+ gem 'to_spreadsheet'
11
+ ```
12
+
13
+ Usage
14
+ -----
15
+
16
+ In the controller:
17
+ ```ruby
18
+ # my_thingies_controller.rb
19
+ class MyThingiesController < ApplicationController
20
+ respond_to :xlsx, :html
21
+ def index
22
+ @my_items = MyItem.all
23
+ respond_to do |format|
24
+ format.html
25
+ format.xlsx { render xlsx: :index, filename: "my_items_doc" }
26
+ end
27
+ end
28
+ end
29
+ ```
30
+
31
+ In the view partial:
32
+ ```haml
33
+ # _my_items.haml
34
+ %table
35
+ %caption My items
36
+ %thead
37
+ %tr
38
+ %td ID
39
+ %td Name
40
+ %tbody
41
+ - my_items.each do |my_item|
42
+ %tr
43
+ %td.number= my_item.id
44
+ %td= my_item.name
45
+ %tfoot
46
+ %tr
47
+ %td(colspan="2") #{my_items.length}
48
+ ```
49
+
50
+ In the XLSX view:
51
+ ```haml
52
+ # index.xlsx.haml
53
+ = render 'my_items', my_items: @my_items
54
+ ```
55
+
56
+ In the HTML view:
57
+ ```haml
58
+ # index.html.haml
59
+ = link_to 'Download spreadsheet', my_items_url(format: :xlsx)
60
+ = render 'my_items', my_items: @my_items
61
+ ```
62
+
63
+ ### Worksheets
64
+
65
+ Every table in the view will be converted to a separate sheet.
66
+ The sheet title will be assigned to the value of the table’s caption element if it exists.
67
+
68
+ ### Formatting
69
+
70
+ You can define formats in your view file (local to the view) or in the initializer
71
+
72
+ ```ruby
73
+ format_xls 'table.my-table' do
74
+ workbook use_autowidth: true
75
+ sheet orientation: landscape
76
+ format 'th', b: true # bold
77
+ format 'tbody tr', bg_color: lambda { |row| 'ddffdd' if row.row_index.odd? }
78
+ format 'A3:B10', i: true # italic
79
+ format column: 0, width: 35
80
+ format 'td.custom', lambda { |cell| modify cell somehow.}
81
+ # default value (fallback value when value is blank or 0 for integer / float)
82
+ default 'td.price', 10
83
+ end
84
+ ```
85
+
86
+ For the full list of supported properties head here: http://rubydoc.info/github/randym/axlsx/Axlsx/Styles#add_style-instance_method
87
+ In addition, for column formats, Axlsx columnInfo properties are also supported
88
+
89
+ ### Advanced formatting
90
+
91
+ to_spreadsheet [associates](https://github.com/glebm/to_spreadsheet/blob/master/lib/to_spreadsheet/renderer.rb#L33) HTML nodes with Axlsx objects as follows:
92
+
93
+ | HTML tag | Axlsx object |
94
+ |----------|--------------|
95
+ | table | worksheet |
96
+ | tr | row |
97
+ | td, th | cell |
98
+
99
+ For example, to directly manipulate a worksheet:
100
+ ```ruby
101
+ format_xls do
102
+ format 'table' do |worksheet|
103
+ worksheet.add_chart ...
104
+ # to get the associated Nokogiri node:
105
+ el = context.to_xml_node(worksheet)
106
+ end
107
+ end
108
+ ```
109
+
110
+ ### Themes
111
+
112
+ You can define themes, i.e. blocks of formatting code:
113
+ ```ruby
114
+ ToSpreadsheet.theme :zebra do
115
+ format 'tr', bg_color: lambda { |row| 'ddffdd' if row.row_index.odd? }
116
+ end
117
+ ```
118
+
119
+ And then use them:
120
+ ```ruby
121
+ format_xls 'table.zebra', ToSpreadsheet.theme(:zebra)
122
+ ```
123
+
124
+ ### Using along side axlsx-rails
125
+ If you are using [axlsx-rails](https://github.com/straydogstudio/axlsx_rails), :xlsx renderer might have already been defined. In that case define a custome renderer using
126
+ ```ruby
127
+ # app/config/application.rb
128
+ config.to_spreadsheet.renderer = :html2xlsx
129
+ ```
130
+
131
+ And then in controller
132
+ ```ruby
133
+ respond_to do |format|
134
+ format.html2xlsx
135
+ end
136
+ ```
137
+
138
+ ### Types
139
+
140
+ The default theme uses class names on td/th to cast values.
141
+ Here is the list of class to type mapping:
142
+
143
+ | CSS class | Format |
144
+ |------------------|--------------------------|
145
+ | decimal or float | Decimal |
146
+ | num or int | Integer |
147
+ | datetime | DateTime (Chronic.parse) |
148
+ | date | Date (Date.parse) |
149
+ | time | Time (Chronic.parse) |
data/Rakefile CHANGED
@@ -21,3 +21,12 @@ task :write_test_xlsx => :env do
21
21
  ToSpreadsheet::Renderer.to_package(html).serialize(path)
22
22
  puts "Written to #{path}"
23
23
  end
24
+
25
+ desc "Test all rails versions"
26
+ task :test_all_rails do
27
+ Dir.glob("./spec/gemfiles/*{[!.lock]}").each do |gemfile|
28
+ puts "TESTING WITH #{gemfile}"
29
+ system "BUNDLE_GEMFILE=#{gemfile} bundle | grep Installing"
30
+ system "BUNDLE_GEMFILE=#{gemfile} bundle exec rspec"
31
+ end
32
+ end
@@ -1,10 +1,13 @@
1
1
  require 'to_spreadsheet/version'
2
2
  require 'to_spreadsheet/context'
3
3
  require 'to_spreadsheet/renderer'
4
- require 'to_spreadsheet/railtie' if defined?(Rails)
5
4
 
6
5
  module ToSpreadsheet
7
6
  class << self
7
+ def renderer
8
+ @renderer ||= :xlsx
9
+ end
10
+
8
11
  def theme(name, &formats)
9
12
  @themes ||= {}
10
13
  if formats
@@ -16,5 +19,6 @@ module ToSpreadsheet
16
19
  end
17
20
  end
18
21
 
22
+ require 'to_spreadsheet/railtie' if defined?(Rails)
19
23
  require 'to_spreadsheet/themes/default'
20
24
  ToSpreadsheet::Context.global.format_xls ToSpreadsheet.theme(:default)
@@ -1,16 +1,26 @@
1
1
  require 'active_support'
2
2
  require 'action_controller/metal/renderers'
3
- require 'action_controller/metal/responder'
3
+
4
+ # in rails 3.2 it's ActiveSupport::VERSION
5
+ # in rails 4.0+ it's ActiveSupport.version (instance of Gem::Version)
6
+ if ActiveSupport.respond_to?(:version) && ActiveSupport.version.to_s >= "4.2.0"
7
+ # For rails 4.2
8
+ require 'action_controller/responder'
9
+ else
10
+ # For rails 3.2 - rails 4.1
11
+ require 'action_controller/metal/responder'
12
+ end
13
+
4
14
 
5
15
  # This will let us do thing like `render :xlsx => 'index'`
6
16
  # This is similar to how Rails internally implements its :json and :xml renderers
7
- ActionController::Renderers.add :xlsx do |template, options|
17
+ ActionController::Renderers.add ToSpreadsheet.renderer do |template, options|
8
18
  filename = options[:filename] || options[:template] || 'data'
9
19
  data = ToSpreadsheet::Context.with_context ToSpreadsheet::Context.global.merge(ToSpreadsheet::Context.new) do |context|
10
- html = render_to_string(template, options.merge(template: template.to_s, formats: ['xlsx', 'html']))
20
+ html = render_to_string(template, options.merge(template: template.to_s, formats: [:xlsx, :html]))
11
21
  ToSpreadsheet::Renderer.to_data(html, context)
12
22
  end
13
- send_data data, type: :xlsx, disposition: %(attachment; filename="#{filename}.xlsx")
23
+ send_data data, type: ToSpreadsheet.renderer, disposition: %(attachment; filename="#{filename}.xlsx")
14
24
  end
15
25
 
16
26
  class ActionController::Responder
@@ -18,7 +28,7 @@ class ActionController::Responder
18
28
  # respond_to do |format|
19
29
  # format.xlsx
20
30
  # end
21
- def to_xlsx
22
- controller.render xlsx: controller.action_name
31
+ define_method "to_#{ToSpreadsheet.renderer}" do
32
+ controller.render ToSpreadsheet.renderer => controller.action_name
23
33
  end
24
34
  end
@@ -1,2 +1,2 @@
1
1
  require 'action_dispatch/http/mime_type'
2
- Mime::Type.register "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", :xlsx unless Mime::Type.lookup_by_extension(:xlsx)
2
+ Mime::Type.register "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", ToSpreadsheet.renderer unless Mime::Type.lookup_by_extension(ToSpreadsheet.renderer)
@@ -1,10 +1,15 @@
1
1
  require 'rails/railtie'
2
- require 'to_spreadsheet/rails/mime_types'
3
- require 'to_spreadsheet/rails/action_pack_renderers'
4
- require 'to_spreadsheet/rails/view_helpers'
5
2
  module ToSpreadsheet
6
3
  class Railtie < ::Rails::Railtie
7
- initializer "to_spreadsheet.view_helpers" do
4
+ config.to_spreadsheet = ActiveSupport::OrderedOptions.new
5
+ config.to_spreadsheet.renderer = ToSpreadsheet.renderer
6
+
7
+ config.after_initialize do |app|
8
+ ToSpreadsheet.instance_variable_set("@renderer", app.config.to_spreadsheet.renderer)
9
+
10
+ require 'to_spreadsheet/rails/action_pack_renderers'
11
+ require 'to_spreadsheet/rails/view_helpers'
12
+ require 'to_spreadsheet/rails/mime_types'
8
13
  ActionView::Base.send :include, ToSpreadsheet::Rails::ViewHelpers
9
14
  end
10
15
  end
@@ -1,4 +1,4 @@
1
- require 'active_support/core_ext'
1
+ require 'active_support/core_ext/string'
2
2
  module ToSpreadsheet
3
3
  module Rule
4
4
  def self.make(rule_type, selector_type, selector_value, options)
@@ -6,4 +6,4 @@ module ToSpreadsheet
6
6
  klass.new(selector_type, selector_value, options)
7
7
  end
8
8
  end
9
- end
9
+ end
@@ -8,7 +8,7 @@ module ToSpreadsheet
8
8
  wb = sheet.workbook
9
9
  case selector_type
10
10
  when :css
11
- css_match selector_query, context.to_xml_node(sheet) do |xml_node|
11
+ css_match selector_query, context.to_xml_node(sheet) do |xml_node|
12
12
  add_and_apply_style wb, context, context.to_xls_entity(xml_node)
13
13
  end
14
14
  when :row
@@ -18,7 +18,7 @@ module ToSpreadsheet
18
18
  sheet.col_style selector_query, inline_styles if inline_styles.present?
19
19
  apply_col_info sheet.column_info[selector_query]
20
20
  when :range
21
- add_and_apply_style wb, range_match(selector_type, sheet), context
21
+ add_and_apply_style wb, range_match(selector_query, sheet), context
22
22
  end
23
23
  end
24
24
 
@@ -1,3 +1,5 @@
1
+ require 'chronic'
2
+
1
3
  module ToSpreadsheet::Themes
2
4
  module Default
3
5
  ::ToSpreadsheet.theme :default do
@@ -35,4 +37,4 @@ module ToSpreadsheet::Themes
35
37
  }
36
38
  end
37
39
  end
38
- end
40
+ end
@@ -1,3 +1,3 @@
1
1
  module ToSpreadsheet
2
- VERSION = '1.0.4'
2
+ VERSION = '1.1.0'
3
3
  end
@@ -20,10 +20,10 @@ HAML
20
20
 
21
21
  context 'default values' do
22
22
  it 'get set when the cell is empty' do
23
- row.cells[0].value.should == 100
23
+ expect(row.cells[0].value).to eq(100)
24
24
  end
25
25
  it 'does not get set when the cell is not empty' do
26
- row.cells[1].value.should == 50
26
+ expect(row.cells[1].value).to eq(50)
27
27
  end
28
28
  end
29
29
  end
data/spec/format_spec.rb CHANGED
@@ -6,7 +6,7 @@ describe ToSpreadsheet::Rule::Format do
6
6
  :ruby
7
7
  format_xls do
8
8
  format column: 0, width: 25
9
- format 'tr', fg_color: lambda { |row| 'cccccc' if row.index.odd? }
9
+ format 'tr', fg_color: lambda { |row| 'cccccc' if row.row_index.odd? }
10
10
  format 'table' do |ws|
11
11
  ws.name = 'Test'
12
12
  end
@@ -23,16 +23,16 @@ describe ToSpreadsheet::Rule::Format do
23
23
 
24
24
  context 'local styles' do
25
25
  it 'sets column width' do
26
- sheet.column_info[0].width.should == 25
26
+ expect(sheet.column_info[0].width).to eq(25)
27
27
  end
28
28
  it 'runs lambdas on properties' do
29
29
  cell = sheet.rows[1].cells[0]
30
30
  styles = sheet.workbook.styles
31
31
  font_id = styles.cellXfs[cell.style].fontId
32
- styles.fonts[font_id].color.rgb.should == Axlsx::Color.new(rgb: 'cccccc').rgb
32
+ expect(styles.fonts[font_id].color.rgb).to eq(Axlsx::Color.new(rgb: 'cccccc').rgb)
33
33
  end
34
34
  it 'runs blocks for selectors' do
35
- sheet.name.should == 'Test'
35
+ expect(sheet.name).to eq('Test')
36
36
  end
37
37
  end
38
38
  end
@@ -0,0 +1,10 @@
1
+ require 'to_spreadsheet/railtie'
2
+
3
+ describe ToSpreadsheet::Railtie do
4
+
5
+ it "registers a renderer" do
6
+ expect(ToSpreadsheet.renderer).to eq(:html2xlsx)
7
+ expect(ActionController::Renderers::RENDERERS).to include(ToSpreadsheet.renderer)
8
+ end
9
+
10
+ end
data/spec/types_spec.rb CHANGED
@@ -19,19 +19,19 @@ HAML
19
19
 
20
20
  context 'data types' do
21
21
  it 'num' do
22
- row.cells[0].value.should == 20
22
+ expect(row.cells[0].value).to eq(20)
23
23
  end
24
24
 
25
25
  it 'float' do
26
- row.cells[1].type.should be :float
26
+ expect(row.cells[1].type).to be :float
27
27
  end
28
28
 
29
29
  it 'date' do
30
- row.cells[2].type.should be :date
30
+ expect(row.cells[2].type).to be :date
31
31
  end
32
32
 
33
33
  it 'empty date' do
34
- row.cells[3].type.should_not be :date
34
+ expect(row.cells[3].type).not_to be :date
35
35
  end
36
36
  end
37
37
  end
@@ -10,7 +10,7 @@ describe ToSpreadsheet::Renderer do
10
10
 
11
11
  context 'worksheets' do
12
12
  it 'are created 1 per <table>' do
13
- spreadsheet.workbook.should have(2).worksheets
13
+ expect(spreadsheet.workbook.worksheets.length).to eq(2)
14
14
  end
15
15
  end
16
16
  end
metadata CHANGED
@@ -1,97 +1,125 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: to_spreadsheet
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gleb Mazovetskiy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-06-26 00:00:00.000000000 Z
11
+ date: 2021-08-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: nokogiri
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: axlsx
42
+ name: caxlsx
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '>='
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '>='
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: chronic
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - '>='
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
62
  type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - '>='
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: responders
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
67
81
  - !ruby/object:Gem::Version
68
82
  version: '0'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: haml-rails
71
85
  requirement: !ruby/object:Gem::Requirement
72
86
  requirements:
73
- - - '>='
87
+ - - ">="
74
88
  - !ruby/object:Gem::Version
75
89
  version: '0'
76
90
  type: :development
77
91
  prerelease: false
78
92
  version_requirements: !ruby/object:Gem::Requirement
79
93
  requirements:
80
- - - '>='
94
+ - - ">="
81
95
  - !ruby/object:Gem::Version
82
96
  version: '0'
83
97
  - !ruby/object:Gem::Dependency
84
98
  name: rspec-rails
85
99
  requirement: !ruby/object:Gem::Requirement
86
100
  requirements:
87
- - - '>='
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: combustion
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
88
116
  - !ruby/object:Gem::Version
89
117
  version: '0'
90
118
  type: :development
91
119
  prerelease: false
92
120
  version_requirements: !ruby/object:Gem::Requirement
93
121
  requirements:
94
- - - '>='
122
+ - - ">="
95
123
  - !ruby/object:Gem::Version
96
124
  version: '0'
97
125
  description: Render XLSX from Rails using existing views ( .*.html => .xlsx )
@@ -99,32 +127,33 @@ email: glex.spb@gmail.com
99
127
  executables: []
100
128
  extensions: []
101
129
  extra_rdoc_files:
102
- - README.textile
130
+ - README.md
103
131
  files:
104
- - README.textile
105
132
  - LICENSE
133
+ - README.md
106
134
  - Rakefile
107
- - lib/to_spreadsheet/context/pairing.rb
135
+ - lib/to_spreadsheet.rb
108
136
  - lib/to_spreadsheet/context.rb
137
+ - lib/to_spreadsheet/context/pairing.rb
109
138
  - lib/to_spreadsheet/rails/action_pack_renderers.rb
110
139
  - lib/to_spreadsheet/rails/mime_types.rb
111
140
  - lib/to_spreadsheet/rails/view_helpers.rb
112
141
  - lib/to_spreadsheet/railtie.rb
113
142
  - lib/to_spreadsheet/renderer.rb
143
+ - lib/to_spreadsheet/rule.rb
114
144
  - lib/to_spreadsheet/rule/base.rb
115
145
  - lib/to_spreadsheet/rule/container.rb
116
146
  - lib/to_spreadsheet/rule/default_value.rb
117
147
  - lib/to_spreadsheet/rule/format.rb
118
148
  - lib/to_spreadsheet/rule/sheet.rb
119
149
  - lib/to_spreadsheet/rule/workbook.rb
120
- - lib/to_spreadsheet/rule.rb
121
150
  - lib/to_spreadsheet/selectors.rb
122
151
  - lib/to_spreadsheet/themes/default.rb
123
152
  - lib/to_spreadsheet/type_from_value.rb
124
153
  - lib/to_spreadsheet/version.rb
125
- - lib/to_spreadsheet.rb
126
154
  - spec/defaults_spec.rb
127
155
  - spec/format_spec.rb
156
+ - spec/rails_integration_spec.rb
128
157
  - spec/types_spec.rb
129
158
  - spec/worksheets_spec.rb
130
159
  homepage: https://github.com/glebm/to_spreadsheet
@@ -136,22 +165,22 @@ require_paths:
136
165
  - lib
137
166
  required_ruby_version: !ruby/object:Gem::Requirement
138
167
  requirements:
139
- - - '>='
168
+ - - ">="
140
169
  - !ruby/object:Gem::Version
141
170
  version: '0'
142
171
  required_rubygems_version: !ruby/object:Gem::Requirement
143
172
  requirements:
144
- - - '>='
173
+ - - ">="
145
174
  - !ruby/object:Gem::Version
146
175
  version: '0'
147
176
  requirements: []
148
- rubyforge_project:
149
- rubygems_version: 2.0.3
177
+ rubygems_version: 3.1.2
150
178
  signing_key:
151
179
  specification_version: 4
152
180
  summary: Render existing views as Excel documents with style!
153
181
  test_files:
154
182
  - spec/defaults_spec.rb
183
+ - spec/rails_integration_spec.rb
184
+ - spec/worksheets_spec.rb
155
185
  - spec/format_spec.rb
156
186
  - spec/types_spec.rb
157
- - spec/worksheets_spec.rb
data/README.textile DELETED
@@ -1,116 +0,0 @@
1
- to_spreadsheet is a gem that lets you render xls from your existing slim/haml/erb/etc views from Rails (>= 3.0). !https://secure.travis-ci.org/glebm/to_spreadsheet.png?branch=master(Build Status)!:http://travis-ci.org/glebm/to_spreadsheet
2
-
3
- h2. Installation
4
-
5
- Add it to your Gemfile:
6
-
7
- bc. gem 'to_spreadsheet'
8
-
9
- h2. Usage
10
-
11
- In your controller:
12
-
13
- bc. # my_thingies_controller.rb
14
- class MyThingiesController < ApplicationController
15
- respond_to :xls, :html
16
- def index
17
- @my_items = MyItem.all
18
- respond_to do |format|
19
- format.html
20
- format.xlsx { render xlsx: :index, filename: "my_items_doc" }
21
- end
22
- end
23
- end
24
-
25
- In your view partial:
26
-
27
- bc. # _my_items.haml
28
- %table
29
- %caption My items
30
- %thead
31
- %tr
32
- %td ID
33
- %td Name
34
- %tbody
35
- - my_items.each do |my_item|
36
- %tr
37
- %td.number= my_item.id
38
- %td= my_item.name
39
- %tfoot
40
- %tr
41
- %td(colspan="2") #{my_items.length}
42
-
43
- In your index.xls.haml:
44
-
45
- bc. # index.xls.haml
46
- = render 'my_items', my_items: @my_items
47
-
48
- In your index.html.haml:
49
-
50
- bc. # index.html.haml
51
- = link_to 'Download spreadsheet', my_items_url(format: :xlsx)
52
- = render 'my_items', my_items: @my_items
53
-
54
- h3. Worksheets
55
-
56
- Every table in the view will be converted to a separate sheet.
57
- The sheet title will be assigned to the value of the table's caption element if it exists.
58
-
59
- h3. Formatting
60
-
61
- You can define formats in your view file (local to the view) or in the initializer
62
-
63
- bc. format_xls 'table.my-table' do
64
- workbook use_autowidth: true
65
- sheet orientation: landscape
66
- format 'th', b: true # bold
67
- format 'tbody tr', bg_color: lambda { |row| 'ddffdd' if row.index.odd? }
68
- format 'A3:B10', i: true # italic
69
- format column: 0, width: 35
70
- format 'td.custom', lambda { |cell| modify cell somehow.}
71
- # default value (fallback value when value is blank or 0 for integer / float)
72
- default 'td.price', 10
73
-
74
- For the full list of supported properties head here: http://rubydoc.info/github/randym/axlsx/Axlsx/Styles#add_style-instance_method
75
- In addition, for column formats, Axlsx columnInfo properties are also supported
76
-
77
- h3. Advanced formatting
78
-
79
- to_spreadsheet "associates":https://github.com/glebm/to_spreadsheet/blob/master/lib/to_spreadsheet/renderer.rb#L33 HTML nodes with Axlsx objects as follows:
80
-
81
- |_. HTML tag |_. Axlsx object |
82
- | table | worksheet |
83
- | tr | row |
84
- | td, th | cell |
85
-
86
- For example, to directly manipulate a worksheet:
87
-
88
- bc. format_xls do
89
- format 'table' do |worksheet|
90
- worksheet.add_chart ...
91
- # to get the associated Nokogiri node:
92
- el = context.to_xml_node(worksheet)
93
-
94
-
95
- h3. Themes
96
-
97
- You can define "themes" - blocks of formatting code:
98
-
99
- bc. ToSpreadsheet.theme :zebra do
100
- format 'tr', bg_color: lambda { |row| 'ddffdd' if row.index.odd? }
101
-
102
- And then use them:
103
-
104
- bc. format_xls 'table.zebra', ToSpreadsheet.theme(:zebra)
105
-
106
- h3. Types
107
-
108
- The default theme uses class names on td/th to cast values.
109
- Here is the list of class to type mapping:
110
-
111
- |_. ==CSS== class |_. Format |
112
- | decimal or float | Decimal |
113
- | num or int | Integer |
114
- | datetime | DateTime (Chronic.parse) |
115
- | date | Date (Date.parse) |
116
- | time | Time (Chronic.parse) |