xplosion 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +1 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +14 -0
- data/README.md +81 -0
- data/Rakefile +2 -0
- data/lib/xplosion.rb +1 -0
- data/lib/xplosion/railtie.rb +8 -0
- data/lib/xplosion/version.rb +3 -0
- data/lib/xplosion/xplosion_helper.rb +61 -0
- data/xplosion.gemspec +21 -0
- metadata +56 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: fb688bd25b1a73b1dd1b320be40baaa78227b516
|
4
|
+
data.tar.gz: 9fe45a12a53a31918821a7d9f4cac284d711b567
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f1cc25f8146d39de1b3cabec86cd8fbf1828b0988a911effaf9875da2475d484368c015d1ef9279db255f8362ac4253818e3d88804b51dda0c465c814ae7fef2
|
7
|
+
data.tar.gz: 65b47d4ddecc2d8d8495d8cc331c228addad6e4c04ea2c75dfa7d46d987878793a17d1ed4a5a97c4ffdda730abd29beb0ad8fd12ee90054cf267a036ee43c557
|
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
pkg/*
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
data/README.md
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
XpLoSion
|
2
|
+
========
|
3
|
+
|
4
|
+
Description
|
5
|
+
---------------
|
6
|
+
XpLoSion is a gem that provides an easy way to create and define Excel'97 XLS files for Ruby. It works in a different way as other XLS exporters solutions because XpLoSion allows you to control exactly the structure and content of your XLS files independently of your views. That is entirely up to you.
|
7
|
+
|
8
|
+
The greatest part of Xplosion is that allows you to create a workbook with several worksheets with different types of data, as well as a single worksheet or whatever you are interested in.
|
9
|
+
|
10
|
+
Installation
|
11
|
+
---------------
|
12
|
+
This gem requires the [builder](https://github.com/jimweirich/builder) gem from our beloved and lost [Jim Weirich](https://github.com/jimweirich), so if you are using Rails or Sinatra you don't have to do nothing, as both frameworks include it *per se*. On other frameworks please refer to your inner documentation to find if **builder** is already included.
|
13
|
+
|
14
|
+
On your Gemfile please include:
|
15
|
+
|
16
|
+
gem 'xplosion'
|
17
|
+
|
18
|
+
and then run:
|
19
|
+
|
20
|
+
bundle install
|
21
|
+
|
22
|
+
Usage
|
23
|
+
---------
|
24
|
+
The XLS'97 format is in fact a XML with some particular structure and that's why we are using **builder** to build the XML file. Xplosion is in fact a helper set that abstracts the tedious work of implementing the XML/XLS by hand.
|
25
|
+
|
26
|
+
Xplosion has 4 methods:
|
27
|
+
|
28
|
+
* `xls_workbook`, which initializes the main workbook container in which worksheets and other elements will live.
|
29
|
+
* `xls_worhsheet(name)`, which declares the worksheet with the name passed by parameter. You can declare as many worksheets as you want.
|
30
|
+
* `xls_row`, which declares a new row.
|
31
|
+
* `xls_cell(value)`, which declares a cell with a particular value.
|
32
|
+
|
33
|
+
Example
|
34
|
+
---------
|
35
|
+
Imagine you want to create a workbook with a single worksheet with your contacts information. First of all you have to decide which of all contacts attributes you want to display and once you've decided it create a colum_names string array. That's the hardest decision you will have to make. Let the magic happens.
|
36
|
+
|
37
|
+
First, go to the controller and add the next to your respond_to section of your desired action (e.g. index):
|
38
|
+
|
39
|
+
class ContactsController < ApplicationController
|
40
|
+
def index
|
41
|
+
@contacts = Contact.all
|
42
|
+
respond_to do |format|
|
43
|
+
format.html
|
44
|
+
format.xls
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
OK! Now is when the magic starts!
|
50
|
+
|
51
|
+
Just create the corresponding view in `app/views/contacts`, with the name `index.xls.builder` and this content:
|
52
|
+
|
53
|
+
xls_workbook(xml) do # xml is the default xml builder object
|
54
|
+
xls_worksheet 'Contacts' do
|
55
|
+
xls_row do
|
56
|
+
xls_cell 'Name'
|
57
|
+
xls_cell 'CIF'
|
58
|
+
xls_cell 'Phone'
|
59
|
+
xls_cell 'E-mail'
|
60
|
+
xls_cell 'Address'
|
61
|
+
xls_cell 'City'
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
@contacts.each do |contact|
|
66
|
+
xls_row do
|
67
|
+
xls_cell contact.name
|
68
|
+
xls_cell contact.cif
|
69
|
+
xls_cell contact.phone
|
70
|
+
xls_cell contact.email
|
71
|
+
xls_cell contact.address
|
72
|
+
xls_cell contact.city
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
Finally, the corresponding link to download the XLS file would be something like:
|
78
|
+
|
79
|
+
<%= link_to "Export to Excel'97", contacts_path(format: :xls) %>
|
80
|
+
|
81
|
+
And that's it! Have fun!
|
data/Rakefile
ADDED
data/lib/xplosion.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'xplosion/railtie' if defined?(Rails)
|
@@ -0,0 +1,61 @@
|
|
1
|
+
module Xplosion
|
2
|
+
module XplosionHelpers
|
3
|
+
WORKBOOK_NAMESPACES = {
|
4
|
+
"xmlns" => "urn:schemas-microsoft-com:office:spreadsheet",
|
5
|
+
"xmlns:o" => "urn:schemas-microsoft-com:office:office",
|
6
|
+
"xmlns:x" => "urn:schemas-microsoft-com:office:excel",
|
7
|
+
"xmlns:ss" => "urn:schemas-microsoft-com:office:spreadsheet",
|
8
|
+
"xmlns:html" => "http://www.w3.org/TR/REC-html40"
|
9
|
+
}
|
10
|
+
|
11
|
+
def xls_workbook(xml, &block)
|
12
|
+
xml.instruct!
|
13
|
+
xml.Workbook WORKBOOK_NAMESPACES do |workbook|
|
14
|
+
go_down(workbook, block)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def xls_worksheet(name, &block)
|
19
|
+
xls_parent.Worksheet "ss:Name" => name do |worksheet|
|
20
|
+
worksheet.Table do |table|
|
21
|
+
go_down(table, block)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def xls_row(&block)
|
27
|
+
xls_parent.Row do |row|
|
28
|
+
go_down(row, block)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def xls_cell(value)
|
33
|
+
xls_parent.Cell do |cell|
|
34
|
+
type,value = xls_cell_type_from(value)
|
35
|
+
cell.Data value, "ss:Type" => type
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def xls_context
|
42
|
+
@xls_context ||= []
|
43
|
+
end
|
44
|
+
|
45
|
+
def go_down(parent, block)
|
46
|
+
xls_context.push(parent)
|
47
|
+
block.call
|
48
|
+
xls_context.pop
|
49
|
+
end
|
50
|
+
|
51
|
+
def xls_parent
|
52
|
+
xls_context.first
|
53
|
+
end
|
54
|
+
|
55
|
+
def xls_cell_type_from(value)
|
56
|
+
return ['Number', value] if value.is_a?(Numeric)
|
57
|
+
return ['String', value.to_date.to_s] if value.is_a?(DateTime) || value.is_a?(Date) || value.is_a?(Time)
|
58
|
+
['String', value]
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
data/xplosion.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "xplosion/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "xplosion"
|
7
|
+
s.version = Xplosion::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Albert Bellonch", "Marc Pursals"]
|
10
|
+
s.email = ["albert@getquipu.com", "marc@getquipu.com"]
|
11
|
+
s.homepage = "http://getquipu.com"
|
12
|
+
s.summary = %q{An XLS exporter for RAILS}
|
13
|
+
s.description = %q{-}
|
14
|
+
s.license = 'MIT'
|
15
|
+
|
16
|
+
s.rubyforge_project = "xplosion"
|
17
|
+
|
18
|
+
s.files = `git ls-files`.split("\n")
|
19
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: xplosion
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Albert Bellonch
|
8
|
+
- Marc Pursals
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-04-23 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: '-'
|
15
|
+
email:
|
16
|
+
- albert@getquipu.com
|
17
|
+
- marc@getquipu.com
|
18
|
+
executables: []
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- .gitignore
|
23
|
+
- Gemfile
|
24
|
+
- Gemfile.lock
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- lib/xplosion.rb
|
28
|
+
- lib/xplosion/railtie.rb
|
29
|
+
- lib/xplosion/version.rb
|
30
|
+
- lib/xplosion/xplosion_helper.rb
|
31
|
+
- xplosion.gemspec
|
32
|
+
homepage: http://getquipu.com
|
33
|
+
licenses:
|
34
|
+
- MIT
|
35
|
+
metadata: {}
|
36
|
+
post_install_message:
|
37
|
+
rdoc_options: []
|
38
|
+
require_paths:
|
39
|
+
- lib
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - '>='
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
requirements: []
|
51
|
+
rubyforge_project: xplosion
|
52
|
+
rubygems_version: 2.2.2
|
53
|
+
signing_key:
|
54
|
+
specification_version: 4
|
55
|
+
summary: An XLS exporter for RAILS
|
56
|
+
test_files: []
|