excelerate 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +3 -0
- data/README.md +76 -0
- data/Rakefile +1 -0
- data/excelerate.gemspec +23 -0
- data/lib/excelerate.rb +18 -0
- data/lib/excelerate/parent_reader.rb +13 -0
- data/lib/excelerate/util.rb +19 -0
- data/lib/excelerate/version.rb +3 -0
- data/lib/excelerate/workbook.rb +17 -0
- data/lib/excelerate/worksheet.rb +48 -0
- data/spec/assets/example.xls +0 -0
- data/spec/lib/excelerate/util_spec.rb +10 -0
- data/spec/lib/excelerate/workbook_spec.rb +18 -0
- data/spec/lib/excelerate/worksheet_spec.rb +43 -0
- data/spec/spec_helper.rb +7 -0
- metadata +98 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
Excelerate
|
2
|
+
==========
|
3
|
+
|
4
|
+
Excelerate is a gem that adds a modern Ruby API to the classic
|
5
|
+
ParseExcel library. ParseExcel does a wonderful job of pulling the data
|
6
|
+
out of an Excel file, but its usability is lacking. Excelerate is
|
7
|
+
intended to address those woes.
|
8
|
+
|
9
|
+
Installation
|
10
|
+
------------
|
11
|
+
|
12
|
+
Install via standard gem installation:
|
13
|
+
|
14
|
+
gem install excelerate
|
15
|
+
|
16
|
+
Add to `Gemfile`:
|
17
|
+
|
18
|
+
gem 'excelerate'
|
19
|
+
|
20
|
+
Usage
|
21
|
+
-----
|
22
|
+
|
23
|
+
Open a file via URL:
|
24
|
+
|
25
|
+
Excelerate.open 'http://example.com/example.xls'
|
26
|
+
|
27
|
+
Open a local file:
|
28
|
+
|
29
|
+
Excelerate.open 'example.xls'
|
30
|
+
|
31
|
+
Select a worksheet:
|
32
|
+
|
33
|
+
workbook = Excelerate.open(file)
|
34
|
+
first_sheet = workbook.first
|
35
|
+
second_sheet = workbook[1]
|
36
|
+
|
37
|
+
Get column headers:
|
38
|
+
|
39
|
+
first_sheet.column_headers
|
40
|
+
|
41
|
+
Loop through all cells:
|
42
|
+
|
43
|
+
first_sheet.each_cell { |cell| puts cell }
|
44
|
+
|
45
|
+
Loop through rows or columns:
|
46
|
+
|
47
|
+
first_sheet.rows.each { |row| puts row }
|
48
|
+
first_sheet.columns.each { |row| puts columns }
|
49
|
+
|
50
|
+
Grab an individual row or column:
|
51
|
+
|
52
|
+
first_sheet.row(4)
|
53
|
+
first_sheet.column(4)
|
54
|
+
|
55
|
+
Other Features
|
56
|
+
--------
|
57
|
+
|
58
|
+
* `attr_reader` for instance variables
|
59
|
+
* Can open files, strings, or URLs
|
60
|
+
|
61
|
+
To do
|
62
|
+
-----
|
63
|
+
|
64
|
+
* Make it work on Heroku by default
|
65
|
+
|
66
|
+
Contributing
|
67
|
+
------------
|
68
|
+
|
69
|
+
Just issue a pull request!
|
70
|
+
|
71
|
+
License
|
72
|
+
-------
|
73
|
+
|
74
|
+
Released under the MIT license:
|
75
|
+
|
76
|
+
* http://www.opensource.org/licenses/MIT
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
data/excelerate.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "excelerate/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "excelerate"
|
7
|
+
s.version = Excelerate::VERSION
|
8
|
+
s.authors = ["Matthew Culpepper"]
|
9
|
+
s.email = ["culpepper.matthew@gmail.com"]
|
10
|
+
s.homepage = "http://github.com/mculp/excelerate"
|
11
|
+
s.summary = %q{An extension to ParseExcel}
|
12
|
+
s.description = %q{An extension to ParseExcel with an updated, simple-to-use API}
|
13
|
+
|
14
|
+
s.rubyforge_project = "excelerate"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_dependency 'parseexcel'
|
22
|
+
s.add_development_dependency 'rspec'
|
23
|
+
end
|
data/lib/excelerate.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require "excelerate/version"
|
2
|
+
require "excelerate/util"
|
3
|
+
require "excelerate/parent_reader"
|
4
|
+
|
5
|
+
require "delegate"
|
6
|
+
|
7
|
+
require "excelerate/workbook"
|
8
|
+
require "excelerate/worksheet"
|
9
|
+
require "parseexcel"
|
10
|
+
|
11
|
+
module Excelerate
|
12
|
+
def self.open(args)
|
13
|
+
spreadsheet_file = Util::parse_args(args)
|
14
|
+
workbook = Spreadsheet::ParseExcel.parse(spreadsheet_file)
|
15
|
+
|
16
|
+
Workbook.new(:workbook => workbook)
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Excelerate
|
2
|
+
module Util
|
3
|
+
def self.parse_args(args)
|
4
|
+
case args
|
5
|
+
when /^https?:\/\//
|
6
|
+
open_url(args)
|
7
|
+
when args.respond_to?(:seek)
|
8
|
+
args
|
9
|
+
when String
|
10
|
+
File.open(args, 'rb+')
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.open_url(args)
|
15
|
+
require 'open-uri'
|
16
|
+
open(args)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Excelerate
|
2
|
+
class Workbook < SimpleDelegator
|
3
|
+
def initialize(options = {})
|
4
|
+
__setobj__ options[:workbook]
|
5
|
+
end
|
6
|
+
|
7
|
+
def first
|
8
|
+
Worksheet.new(:worksheet => __getobj__.worksheet(0))
|
9
|
+
end
|
10
|
+
|
11
|
+
def worksheet(index)
|
12
|
+
Worksheet.new(:worksheet => __getobj__.worksheet(index))
|
13
|
+
end
|
14
|
+
alias_method :[], :worksheet
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module Excelerate
|
2
|
+
class Worksheet < SimpleDelegator
|
3
|
+
extend ParentReader
|
4
|
+
|
5
|
+
AS_CELL = proc { |cell| Cell.new(:cell => cell) }
|
6
|
+
|
7
|
+
parent_reader :cells, :min_row, :max_row, :min_col, :max_col
|
8
|
+
|
9
|
+
def initialize(options = {})
|
10
|
+
__setobj__ options[:worksheet]
|
11
|
+
end
|
12
|
+
|
13
|
+
def each_cell(&block)
|
14
|
+
cells.flatten.map(&AS_CELL).each &block
|
15
|
+
end
|
16
|
+
|
17
|
+
def columns
|
18
|
+
cells.transpose.map { |c| c.map(&AS_CELL) }
|
19
|
+
end
|
20
|
+
|
21
|
+
def column(index)
|
22
|
+
columns[index]
|
23
|
+
end
|
24
|
+
|
25
|
+
def rows
|
26
|
+
cells.map { |c| c.map(&AS_CELL) }
|
27
|
+
end
|
28
|
+
|
29
|
+
def row(index)
|
30
|
+
rows[index]
|
31
|
+
end
|
32
|
+
|
33
|
+
def column_headers
|
34
|
+
rows.first.map(&AS_CELL)
|
35
|
+
end
|
36
|
+
|
37
|
+
class Cell < SimpleDelegator
|
38
|
+
def initialize(options = {})
|
39
|
+
__setobj__ options[:cell]
|
40
|
+
end
|
41
|
+
|
42
|
+
def value
|
43
|
+
__getobj__.value.delete("\x00")
|
44
|
+
end
|
45
|
+
alias_method :to_s, :value
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
Binary file
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Excelerate::Util do
|
4
|
+
it "should open a local file" do
|
5
|
+
file_name = File.join(File.dirname(__FILE__), '..', '..', 'assets', 'example.xls')
|
6
|
+
file = Excelerate::Util.parse_args(file_name)
|
7
|
+
|
8
|
+
file.should respond_to(:seek)
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Excelerate::Workbook do
|
4
|
+
let(:workbook) {
|
5
|
+
file_name = File.join(File.dirname(__FILE__), '..', '..', 'assets', 'example.xls')
|
6
|
+
Excelerate.open(file_name)
|
7
|
+
}
|
8
|
+
|
9
|
+
it "should provide a method to get a workbook index" do
|
10
|
+
workbook.worksheet(0).should be_a_kind_of(Excelerate::Worksheet)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should provide a helper method to get first worksheet" do
|
14
|
+
workbook.first.should be_a_kind_of(Excelerate::Worksheet)
|
15
|
+
workbook.first.to_s.should == workbook.worksheet(0).to_s
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Excelerate::Worksheet do
|
4
|
+
let(:workbook) {
|
5
|
+
file_name = File.join(File.dirname(__FILE__), '..', '..', 'assets', 'example.xls')
|
6
|
+
Excelerate.open(file_name)
|
7
|
+
}
|
8
|
+
|
9
|
+
let(:worksheet) { workbook.first }
|
10
|
+
|
11
|
+
context "Cell" do
|
12
|
+
it "should provide a value without null bytes" do
|
13
|
+
worksheet.column_headers.first.should be_a_kind_of(Excelerate::Worksheet::Cell)
|
14
|
+
worksheet.column_headers.first.value.should == 'VOTER'
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should provide an array of column headers" do
|
19
|
+
worksheet.column_headers.first.value.should == 'VOTER'
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should provide an array of all columns" do
|
23
|
+
worksheet.columns.size.should == 7
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should provide a way to get a specific column" do
|
27
|
+
worksheet.column(0).to_s.should == worksheet.columns[0].to_s
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should provide an array of all rows" do
|
31
|
+
worksheet.rows.size.should == 50
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should provide a way to get a specific row" do
|
35
|
+
worksheet.row(0).to_s.should == worksheet.rows[0].to_s
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should provide an enumerator for each cell" do
|
39
|
+
worksheet.each_cell.to_a.size.should == 350
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: excelerate
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Matthew Culpepper
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-04-28 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: parseexcel
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
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
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: An extension to ParseExcel with an updated, simple-to-use API
|
47
|
+
email:
|
48
|
+
- culpepper.matthew@gmail.com
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- .gitignore
|
54
|
+
- Gemfile
|
55
|
+
- README.md
|
56
|
+
- Rakefile
|
57
|
+
- excelerate.gemspec
|
58
|
+
- lib/excelerate.rb
|
59
|
+
- lib/excelerate/parent_reader.rb
|
60
|
+
- lib/excelerate/util.rb
|
61
|
+
- lib/excelerate/version.rb
|
62
|
+
- lib/excelerate/workbook.rb
|
63
|
+
- lib/excelerate/worksheet.rb
|
64
|
+
- spec/assets/example.xls
|
65
|
+
- spec/lib/excelerate/util_spec.rb
|
66
|
+
- spec/lib/excelerate/workbook_spec.rb
|
67
|
+
- spec/lib/excelerate/worksheet_spec.rb
|
68
|
+
- spec/spec_helper.rb
|
69
|
+
homepage: http://github.com/mculp/excelerate
|
70
|
+
licenses: []
|
71
|
+
post_install_message:
|
72
|
+
rdoc_options: []
|
73
|
+
require_paths:
|
74
|
+
- lib
|
75
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
76
|
+
none: false
|
77
|
+
requirements:
|
78
|
+
- - ! '>='
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
|
+
none: false
|
83
|
+
requirements:
|
84
|
+
- - ! '>='
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
requirements: []
|
88
|
+
rubyforge_project: excelerate
|
89
|
+
rubygems_version: 1.8.22
|
90
|
+
signing_key:
|
91
|
+
specification_version: 3
|
92
|
+
summary: An extension to ParseExcel
|
93
|
+
test_files:
|
94
|
+
- spec/assets/example.xls
|
95
|
+
- spec/lib/excelerate/util_spec.rb
|
96
|
+
- spec/lib/excelerate/workbook_spec.rb
|
97
|
+
- spec/lib/excelerate/worksheet_spec.rb
|
98
|
+
- spec/spec_helper.rb
|