neat-spreadsheet 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in neat-spreadsheet.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2013 De Marque inc.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,115 @@
1
+ Neat Spreadsheet
2
+ ================
3
+
4
+ Simplify the task of building a spreadsheet in Rails. This gem is based on the Spreadsheet gem. The syntax have been simplified a bit.
5
+
6
+ Install
7
+ -------
8
+
9
+ ```
10
+ gem install neat-spreadsheet
11
+ ```
12
+
13
+ ### Rails 3
14
+
15
+ In your Gemfile:
16
+
17
+ ```ruby
18
+ gem 'neat-spreadsheet'
19
+ ```
20
+
21
+ Example
22
+ -------
23
+
24
+ In your controller *(app/controllers/products_controller.rb)*
25
+
26
+ ```ruby
27
+ def index
28
+ respond_to do |format|
29
+ format.html
30
+ format.xls { render xls: "filename" }
31
+ end
32
+ end
33
+ ```
34
+
35
+ In your **RXLS** view *(app/views/products/index.rxls)*
36
+
37
+ ```ruby
38
+ neat_spreadsheet workbook, title: 'Product Report', columns_width: [8, 25, 15, 15] do |sheet|
39
+ sheet.row style: :h1 do |row|
40
+ row.cell 'Product Report', colspan: 4
41
+ end
42
+
43
+ sheet.spacer
44
+
45
+ sheet.row do |row|
46
+ row.cell '2013-01-01'
47
+ row.cell 'Apple'
48
+ row.cell 4, align: :right
49
+ row.cell 12.99, align: :right
50
+ end
51
+ end
52
+ ```
53
+
54
+
55
+ Usage
56
+ -----
57
+
58
+ ### neat_spreadsheet *(title, options={}, &block)*
59
+ In your **RXLS** view, you can call the method ```neat_spreadsheet```. You must pass the workbook object. You can also use those options :
60
+
61
+ | Options | Type | Description |
62
+ | ------------- |--------|-------------------------------------------------------------------|
63
+ | title | String | Title of the sheet |
64
+ | column_width | Array | Size of each column. This will set the number of columns per row. |
65
+
66
+
67
+ This method takes a block and return a sheet object. Here's a list of the methods available for the sheet object.
68
+
69
+ #### row *(options={}, &block)*
70
+
71
+ Create a new row in the sheet
72
+
73
+ | Name | Type | Description |
74
+ | ----------------|---------|----------------------------------------------------------------|
75
+ | **Options** |
76
+ | height | Integer | Height of the row |
77
+ | start_at_column | Integer | Position of the first column |
78
+ | style | Symbol | Style of the content (h1 to h4, th, normal, small, tiny) |
79
+
80
+ #### cell *(value, options={})*
81
+
82
+ Set the content of a cell at the current row in the sheet
83
+
84
+ | Name | Type | Description |
85
+ | -----------------|---------|---------------------------------------------------------------|
86
+ | value | Mixed | The content of the cell |
87
+ | **Options** |
88
+ | align | Symbol | left, right or center |
89
+ | color | Symbol | Color of the text (white, black, red, etc...) |
90
+ | colspan | Integer | The number of columns this cell take. |
91
+ | pattern_fg_color | Symbol | Background color of the cell. Must set pattern to 1 |
92
+ | size | Integer | Font size |
93
+ | vertical_align | Symbol | top, bottom, middle |
94
+ | weight | Symbol | :normal, :bold |
95
+
96
+ #### skip_cell
97
+
98
+ #### skip_cells *(nb)*
99
+
100
+ #### skip_row
101
+
102
+ #### spacer
103
+
104
+ Show a blank merged row.
105
+
106
+
107
+ Thanks
108
+ ------
109
+
110
+ Thanks to https://github.com/10to1/spreadsheet_on_rails for the hooks with Rails.
111
+
112
+ Copyright
113
+ ---------
114
+
115
+ Copyright (c) 2013 De Marque inc. See LICENSE for further details.
@@ -0,0 +1,11 @@
1
+ require "bundler"
2
+ require "rspec/core/rake_task"
3
+
4
+ Bundler::GemHelper.install_tasks
5
+
6
+ desc "Run all specs in spec directory"
7
+ RSpec::Core::RakeTask.new(:spec) do |spec|
8
+ spec.pattern = "spec/**/*_spec.rb"
9
+ end
10
+
11
+ task :default => :spec
@@ -0,0 +1,15 @@
1
+ module NeatSpreadsheet
2
+ # Thanks to https://github.com/10to1/spreadsheet_on_rails for this method.
3
+ def self.render_xls_string(spreadsheet)
4
+ <<RENDER
5
+ workbook = Spreadsheet::Workbook.new
6
+ #{spreadsheet}
7
+ blob = StringIO.new("")
8
+ workbook.write(blob)
9
+ blob.string
10
+ RENDER
11
+ end
12
+ end
13
+
14
+ require "neat-spreadsheet/engine"
15
+ require 'neat-spreadsheet/implants'
@@ -0,0 +1,5 @@
1
+ module NeatSpreadsheet
2
+ class Engine < Rails::Engine
3
+ #auto wire
4
+ end
5
+ end
@@ -0,0 +1,9 @@
1
+ module NeatSpreadsheet::Helpers
2
+ def neat_spreadsheet(workbook, options={}, &block)
3
+ Sheet.new(workbook, options, &block)
4
+ end
5
+
6
+ ::ActionView::Base.send :include, self
7
+ end
8
+
9
+ require 'neat-spreadsheet/helpers/sheet'
@@ -0,0 +1,131 @@
1
+ module NeatSpreadsheet::Helpers
2
+ class Sheet
3
+ def initialize(workbook, options={}, &block)
4
+ options.reverse_merge! title: nil, columns_width: [10, 10, 10, 10, 10]
5
+
6
+ init_sheet workbook, options[:title]
7
+ init_columns options[:columns_width]
8
+ init_styles
9
+
10
+ yield self
11
+ end
12
+
13
+ def cell(value, options={})
14
+ if options[:colspan]
15
+ options[:horizontal_align] = :merge
16
+
17
+ (@column_position..(@column_position+options[:colspan]-1)).each do |i|
18
+ current_row.set_format(i, current_format(options))
19
+ end
20
+ else
21
+ current_row.set_format(@column_position, current_format(options))
22
+ end
23
+
24
+ set_current_column value
25
+
26
+ @column_position += 1
27
+ end
28
+
29
+ def row(options={}, &block)
30
+ options.reverse_merge! height: nil, start_at_column: 0, style: :normal
31
+
32
+ @column_position = 0
33
+
34
+ set_current_style options[:style]
35
+ set_current_height options[:height]
36
+
37
+ (0..options[:start_at_column]-1).each { |i| cell(' ') } if options[:start_at_column] > 0
38
+
39
+ yield self
40
+
41
+ @row_position += 1
42
+ end
43
+
44
+ def skip_cell
45
+ skip_cells 1
46
+ end
47
+
48
+ def skip_cells(nb)
49
+ (@column_position..(@column_position+nb-1)).each { |i| cell(' ') }
50
+ end
51
+
52
+ def skip_row
53
+ @row_position += 1
54
+ end
55
+
56
+ def spacer(options={})
57
+ options.reverse_merge! height: 20
58
+
59
+ @column_position = 0
60
+
61
+ current_row.height = options[:height]
62
+ (0..@total_columns-1).each { |i| current_row.set_format(i, Spreadsheet::Format.new(horizontal_align: :merge)) }
63
+ set_current_column ' '
64
+
65
+ @row_position += 1
66
+ end
67
+
68
+ private
69
+
70
+ def current_format(options={})
71
+ format = current_style(options)
72
+
73
+ format.delete(:colspan)
74
+
75
+ return Spreadsheet::Format.new(format)
76
+ end
77
+
78
+ def current_row
79
+ @sheet.row(@row_position)
80
+ end
81
+
82
+ def current_style(options={})
83
+ @styles[@current_style].merge(options)
84
+ end
85
+
86
+ def init_columns(sizes)
87
+ sizes.each_with_index { |width, i| @sheet.column(i).width = width }
88
+
89
+ @total_columns = sizes.length
90
+ end
91
+
92
+ def init_sheet(workbook, title)
93
+ @sheet = workbook.create_worksheet
94
+ @sheet.name = title if title
95
+ @row_position = 0
96
+ end
97
+
98
+ def init_styles
99
+ @styles = {}
100
+
101
+ @styles[:h1] = { weight: :bold, size: 16, horizontal_align: :left, vertical_align: :middle }
102
+ @styles[:h2] = { weight: :normal, size: 12, horizontal_align: :left, vertical_align: :middle }
103
+ @styles[:h3] = { weight: :normal, size: 9, horizontal_align: :left, vertical_align: :middle }
104
+ @styles[:h4] = { weight: :bold, size: 8, horizontal_align: :left, vertical_align: :middle }
105
+ @styles[:th] = { weight: :bold, size: 8, horizontal_align: :center, vertical_align: :middle, pattern_fg_color: :xls_color_19, pattern: 1 }
106
+
107
+ @styles[:normal] = { size: 8, horizontal_align: :left , vertical_align: :middle }
108
+ @styles[:small] = { size: 7, horizontal_align: :left , vertical_align: :middle }
109
+ @styles[:tiny] = { size: 6, horizontal_align: :left , vertical_align: :middle }
110
+ @styles[:unknown] = { weight: :bold, size: 5, color: :red }
111
+ end
112
+
113
+ def set_column(position, value)
114
+ @sheet[@row_position, position] = value
115
+ end
116
+
117
+ def set_current_column(value)
118
+ set_column @column_position, value
119
+ end
120
+
121
+ def set_current_height(height)
122
+ height = current_style[:size] + 8 if not height
123
+
124
+ current_row.height = height
125
+ end
126
+
127
+ def set_current_style(name)
128
+ @current_style = (@styles.has_key?(name) ? name : :unknown)
129
+ end
130
+ end
131
+ end
@@ -0,0 +1,10 @@
1
+ module NeatSpreadsheet
2
+ module Implants
3
+ end
4
+ end
5
+
6
+ if defined? Rails::Railtie
7
+ require 'neat-spreadsheet/implants/railtie'
8
+ elsif defined? Rails::Initializer
9
+ raise "neat-spreadsheet is not compatible with Rails 2.3 or older"
10
+ end
@@ -0,0 +1,19 @@
1
+ module NeatSpreadsheet::Implants
2
+ class Railtie < Rails::Railtie
3
+ initializer "neat-spreadsheet" do |app|
4
+ ActiveSupport.on_load :action_view do
5
+ require 'neat-spreadsheet/helpers'
6
+ end
7
+
8
+ ActionView::Template.register_template_handler :rxls, lambda { |template|
9
+ NeatSpreadsheet.render_xls_string(template.source)
10
+ }
11
+
12
+ ActionController::Renderers.add :xls do |filename, options|
13
+ send_data(render_to_string(options), filename: "#{filename}.xls", type: "application/vnd.ms-excel", disposition: "attachment")
14
+ end
15
+
16
+ Mime::Type.register "application/vnd.ms-excel", :xls if not Mime::Type.lookup_by_extension :xls
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe NeatSpreadsheet do
4
+
5
+ context 'with this particular fixture' do
6
+
7
+ describe "#method" do
8
+
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,12 @@
1
+ require "rubygems"
2
+ require "bundler/setup"
3
+
4
+ require 'rspec'
5
+
6
+ require File.expand_path('../../lib/neat-spreadsheet', __FILE__)
7
+
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
9
+
10
+ RSpec.configure do |config|
11
+ config.mock_with :rspec
12
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: neat-spreadsheet
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Sebastien Rosa
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-04-09 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ requirement: &2177974420 !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ none: false
21
+ prerelease: false
22
+ type: :runtime
23
+ name: spreadsheet
24
+ version_requirements: *2177974420
25
+ - !ruby/object:Gem::Dependency
26
+ requirement: &2177998400 !ruby/object:Gem::Requirement
27
+ requirements:
28
+ - - ! '>='
29
+ - !ruby/object:Gem::Version
30
+ version: 0.8.7
31
+ none: false
32
+ prerelease: false
33
+ type: :development
34
+ name: rake
35
+ version_requirements: *2177998400
36
+ - !ruby/object:Gem::Dependency
37
+ requirement: &2177997880 !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '2.0'
42
+ none: false
43
+ prerelease: false
44
+ type: :development
45
+ name: rspec
46
+ version_requirements: *2177997880
47
+ description: Simplify the task of building a spreadsheet in Rails. This gem is based
48
+ on the Spreadsheet gem. The syntax have been simplified a bit.
49
+ email:
50
+ - sebastien@demarque.com
51
+ executables: []
52
+ extensions: []
53
+ extra_rdoc_files:
54
+ - LICENSE
55
+ - README.md
56
+ files:
57
+ - lib/neat-spreadsheet/engine.rb
58
+ - lib/neat-spreadsheet/helpers/sheet.rb
59
+ - lib/neat-spreadsheet/helpers.rb
60
+ - lib/neat-spreadsheet/implants/railtie.rb
61
+ - lib/neat-spreadsheet/implants.rb
62
+ - lib/neat-spreadsheet.rb
63
+ - spec/neat-spreadsheet_spec.rb
64
+ - spec/spec_helper.rb
65
+ - LICENSE
66
+ - README.md
67
+ - Rakefile
68
+ - Gemfile
69
+ homepage: https://github.com/demarque/neat-spreadsheet
70
+ licenses:
71
+ - MIT
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ! '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ none: false
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ none: false
88
+ requirements: []
89
+ rubyforge_project: neat-spreadsheet
90
+ rubygems_version: 1.8.10
91
+ signing_key:
92
+ specification_version: 3
93
+ summary: Simplify the task of building a spreadsheet in Rails. This gem is based on
94
+ the Spreadsheet gem. The syntax have been simplified a bit.
95
+ test_files: []
96
+ has_rdoc: