poi_spreadsheet 0.0.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/lib/poi_spreadsheet.rb +186 -0
- metadata +60 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 607ca4f5a407d66ce4c5711dff9cd5d8c111a88a
|
4
|
+
data.tar.gz: 3095b960c44ad39c9decf20675970c3d175b34fe
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: fa8f993e0a157ef915a8c8151b3fa3b312d9f532d9df41fa3895c601916ee6349c7f2774045e931775798355a92f541622c47fcc80516d9ad35c59cb4a450455
|
7
|
+
data.tar.gz: b13a87dd50b1dbcce23d4de4c623fbc18c02c82da36f635407775cbbcb694d85fefab3ffb0ba1db4c90ef553dada42c3cb4ffb7560cfe1e79e65ec6948863641
|
@@ -0,0 +1,186 @@
|
|
1
|
+
require 'rjb'
|
2
|
+
|
3
|
+
class PoiSpreadsheet
|
4
|
+
|
5
|
+
|
6
|
+
def self.init
|
7
|
+
apache_poi_path = File.dirname(__FILE__)+'/../apache/poi-3.10.1-20140818.jar'
|
8
|
+
Rjb::load(apache_poi_path, ['-Xmx512M'])
|
9
|
+
|
10
|
+
@cell_class = cell_class = Rjb::import('org.apache.poi.hssf.usermodel.HSSFCell')
|
11
|
+
|
12
|
+
|
13
|
+
Rjb::import('org.apache.poi.hssf.usermodel.HSSFCreationHelper')
|
14
|
+
Rjb::import('org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator')
|
15
|
+
|
16
|
+
@cell_reference_class = Rjb::import('org.apache.poi.hssf.util.CellReference')
|
17
|
+
# You can import all java classes that you need
|
18
|
+
@loaded = true
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.cell_class; @cell_class; end
|
22
|
+
|
23
|
+
|
24
|
+
def self.load file
|
25
|
+
puts 'Hello!'
|
26
|
+
unless @loaded
|
27
|
+
init
|
28
|
+
end
|
29
|
+
Workbook.load file
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
|
34
|
+
class Workbook
|
35
|
+
|
36
|
+
attr_accessor :j_book
|
37
|
+
|
38
|
+
def self.load file
|
39
|
+
@file_name = file
|
40
|
+
|
41
|
+
@workbook_class = Rjb::import('org.apache.poi.hssf.usermodel.HSSFWorkbook')
|
42
|
+
@poifs_class = Rjb::import('org.apache.poi.poifs.filesystem.POIFSFileSystem')
|
43
|
+
@file_input_class = Rjb::import('java.io.FileInputStream')
|
44
|
+
@file_input = @file_input_class.new(file)
|
45
|
+
|
46
|
+
book = new
|
47
|
+
|
48
|
+
fs = @poifs_class.new(@file_input)
|
49
|
+
book.j_book = @workbook_class.new(fs)
|
50
|
+
book
|
51
|
+
end
|
52
|
+
|
53
|
+
def initialize
|
54
|
+
@sheets = nil
|
55
|
+
end
|
56
|
+
|
57
|
+
# Get sheet names
|
58
|
+
def sheets
|
59
|
+
@sheets ||= begin
|
60
|
+
sheets = {}
|
61
|
+
self.j_book.getNumberOfSheets.times { |i|
|
62
|
+
j_sheet = j_book.getSheetAt(i)
|
63
|
+
sheet = Worksheet.from_sheet(j_sheet)
|
64
|
+
sheet.book = self
|
65
|
+
name = j_book.getSheetName(i)
|
66
|
+
sheets[name] = sheet
|
67
|
+
}
|
68
|
+
sheets
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
# Get sheet by name
|
73
|
+
def [](k)
|
74
|
+
sheets[k]
|
75
|
+
end
|
76
|
+
|
77
|
+
def save file_name = @file_name
|
78
|
+
@file_output_class ||= Rjb::import('java.io.FileOutputStream')
|
79
|
+
out = @file_output_class.new(file_name);
|
80
|
+
|
81
|
+
begin
|
82
|
+
j_book.write(out)
|
83
|
+
ensure
|
84
|
+
out.close();
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def _evaluator
|
89
|
+
@_evaluator ||= j_book.getCreationHelper.createFormulaEvaluator
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
class Worksheet
|
94
|
+
|
95
|
+
attr_accessor :j_sheet
|
96
|
+
attr_accessor :book
|
97
|
+
|
98
|
+
|
99
|
+
def initialize
|
100
|
+
@rows = {}
|
101
|
+
end
|
102
|
+
|
103
|
+
# get cell
|
104
|
+
def [](row)
|
105
|
+
@rows[row] ||= begin
|
106
|
+
j_row = j_sheet.getRow(row)
|
107
|
+
row = Row.from_row j_row
|
108
|
+
row.sheet = self
|
109
|
+
row
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
# set cell
|
114
|
+
def set(x, y)
|
115
|
+
|
116
|
+
end
|
117
|
+
|
118
|
+
def self.from_sheet j_sheet
|
119
|
+
sheet = new
|
120
|
+
sheet.j_sheet = j_sheet
|
121
|
+
sheet
|
122
|
+
end
|
123
|
+
|
124
|
+
class Row
|
125
|
+
|
126
|
+
attr_accessor :j_row
|
127
|
+
attr_accessor :sheet
|
128
|
+
|
129
|
+
def self.symbol_type(constant)
|
130
|
+
@types ||= begin
|
131
|
+
cell = ::PoiSpreadsheet.cell_class
|
132
|
+
{
|
133
|
+
cell.CELL_TYPE_BOOLEAN => :boolean,
|
134
|
+
cell.CELL_TYPE_NUMERIC => :numeric,
|
135
|
+
cell.CELL_TYPE_STRING => :string,
|
136
|
+
cell.CELL_TYPE_BLANK => :blank,
|
137
|
+
cell.CELL_TYPE_ERROR => :error,
|
138
|
+
cell.CELL_TYPE_FORMULA => :formula,
|
139
|
+
}
|
140
|
+
end
|
141
|
+
@types[constant]
|
142
|
+
end
|
143
|
+
|
144
|
+
def []= col, value
|
145
|
+
cell = j_row.getCell(col)
|
146
|
+
cell.setCellValue(value)
|
147
|
+
end
|
148
|
+
|
149
|
+
def [] col
|
150
|
+
unless cell = j_row.getCell(col)
|
151
|
+
return nil
|
152
|
+
end
|
153
|
+
|
154
|
+
#type = self.class.symbol_type(sheet.book._evaluator.evaluateFormulaCell(cell))
|
155
|
+
type = self.class.symbol_type(cell.getCellType())
|
156
|
+
|
157
|
+
case type
|
158
|
+
when :boolean
|
159
|
+
cell.getBooleanCellValue()
|
160
|
+
when :numeric
|
161
|
+
cell.getNumericCellValue()
|
162
|
+
when :string
|
163
|
+
cell.getStringCellValue()
|
164
|
+
when :blank
|
165
|
+
nil
|
166
|
+
when :error
|
167
|
+
cell.getErrorCellValue()
|
168
|
+
when :formula
|
169
|
+
cell.getNumericCellValue()
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
def self.from_row j_row
|
174
|
+
row = new
|
175
|
+
row.j_row = j_row
|
176
|
+
row
|
177
|
+
end
|
178
|
+
|
179
|
+
end
|
180
|
+
|
181
|
+
end
|
182
|
+
|
183
|
+
end
|
184
|
+
|
185
|
+
|
186
|
+
|
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: poi_spreadsheet
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Michal Hantl
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-09-08 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rjb
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.4'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.4'
|
27
|
+
description: There are quite a few pure ruby gems to deal with excel files with various
|
28
|
+
degree of success. This gem's approach is to use powerfull software like Apache
|
29
|
+
POI and simply provide ruby like interface.
|
30
|
+
email: michal@hantl.cz
|
31
|
+
executables: []
|
32
|
+
extensions: []
|
33
|
+
extra_rdoc_files: []
|
34
|
+
files:
|
35
|
+
- lib/poi_spreadsheet.rb
|
36
|
+
homepage: http://github.com/hakunin/poi_spreadsheet
|
37
|
+
licenses:
|
38
|
+
- MIT
|
39
|
+
metadata: {}
|
40
|
+
post_install_message:
|
41
|
+
rdoc_options: []
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
requirements: []
|
55
|
+
rubyforge_project:
|
56
|
+
rubygems_version: 2.4.1
|
57
|
+
signing_key:
|
58
|
+
specification_version: 4
|
59
|
+
summary: Modify excel sheets using the powerfull Apache POI
|
60
|
+
test_files: []
|