robust_excel_ole 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,355 @@
1
+ # -*- coding: utf-8 -*-
2
+ require File.join(File.dirname(__FILE__), './spec_helper')
3
+
4
+ describe RobustExcelOle::Sheet do
5
+ before do
6
+ @dir = create_tmpdir
7
+ @book = RobustExcelOle::Book.open(@dir + '/simple.xls')
8
+ @sheet = @book[0]
9
+ end
10
+
11
+ after do
12
+ @book.close
13
+ rm_tmp(@dir)
14
+ end
15
+
16
+ describe ".initialize" do
17
+ context "when open sheet protected(with password is 'protect')" do
18
+ before do
19
+ @book_protect = RobustExcelOle::Book.open(@dir + '/protected_sheet.xls', :visible => true)
20
+ @protected_sheet = @book_protect['protect']
21
+ end
22
+
23
+ after do
24
+ @book_protect.close
25
+ end
26
+
27
+ it { @protected_sheet.ProtectContents.should be_true }
28
+
29
+ it "protected sheet can't be write" do
30
+ expect { @protected_sheet[0,0] = 'write' }.to raise_error
31
+ end
32
+ end
33
+
34
+ end
35
+
36
+ shared_context "sheet 'open book with blank'" do
37
+ before do
38
+ @book_with_blank = RobustExcelOle::Book.open(@dir + '/book_with_blank.xls')
39
+ @sheet_with_blank = @book_with_blank[0]
40
+ end
41
+
42
+ after do
43
+ @book_with_blank.close
44
+ end
45
+ end
46
+
47
+ describe "access sheet name" do
48
+ describe "#name" do
49
+ it 'get sheet1 name' do
50
+ @sheet.name.should eq 'Sheet1'
51
+ end
52
+ end
53
+
54
+ describe "#name=" do
55
+ it 'change sheet1 name to foo' do
56
+ @sheet.name = 'foo'
57
+ @sheet.name.should eq 'foo'
58
+ end
59
+ end
60
+ end
61
+
62
+ describe 'access cell' do
63
+ describe "#[]" do
64
+ context "access [0,0]" do
65
+ it { @sheet[0, 0].should be_kind_of RobustExcelOle::Cell }
66
+ it { @sheet[0, 0].value.should eq 'simple' }
67
+ end
68
+
69
+ context "access [0, 0], [0, 1], [2, 0]" do
70
+ it "should get every values" do
71
+ @sheet[0, 0].value.should eq 'simple'
72
+ @sheet[0, 1].value.should eq 'workbook'
73
+ @sheet[2, 0].value.should eq 'matz'
74
+ end
75
+ end
76
+ end
77
+
78
+ it "change a cell to 'foo'" do
79
+ @sheet[0, 0] = 'foo'
80
+ @sheet[0, 0].value.should eq 'foo'
81
+ end
82
+
83
+ describe '#each' do
84
+ it "should sort line in order of column" do
85
+ @sheet.each_with_index do |cell, i|
86
+ case i
87
+ when 0
88
+ cell.value.should eq 'simple'
89
+ when 1
90
+ cell.value.should eq 'workbook'
91
+ when 2
92
+ cell.value.should eq 'sheet1'
93
+ when 3
94
+ cell.value.should eq 'foo'
95
+ when 4
96
+ cell.value.should be_nil
97
+ when 5
98
+ cell.value.should eq 'foobaaa'
99
+ end
100
+ end
101
+ end
102
+
103
+ context "read sheet with blank" do
104
+ include_context "sheet 'open book with blank'"
105
+
106
+ it 'should get from ["A1"]' do
107
+ @sheet_with_blank.each_with_index do |cell, i|
108
+ case i
109
+ when 5
110
+ cell.value.should be_nil
111
+ when 6
112
+ cell.value.should eq 'simple'
113
+ when 7
114
+ cell.value.should be_nil
115
+ when 8
116
+ cell.value.should eq 'workbook'
117
+ when 9
118
+ cell.value.should eq 'sheet1'
119
+ end
120
+ end
121
+ end
122
+ end
123
+
124
+ end
125
+
126
+ describe "#each_row" do
127
+ it "items should RobustExcelOle::Range" do
128
+ @sheet.each_row do |rows|
129
+ rows.should be_kind_of RobustExcelOle::Range
130
+ end
131
+ end
132
+
133
+ context "with argument 1" do
134
+ it 'should read from second row' do
135
+ @sheet.each_row(1) do |rows|
136
+ case rows.row
137
+ when 2
138
+ rows.values.should eq ['foo', nil, 'foobaaa']
139
+ when 3
140
+ rows.values.should eq ['matz', 'is', 'nice']
141
+ end
142
+ end
143
+ end
144
+ end
145
+
146
+ context "read sheet with blank" do
147
+ include_context "sheet 'open book with blank'"
148
+
149
+ it 'should get from ["A1"]' do
150
+ @sheet_with_blank.each_row do |rows|
151
+ case rows.row - 1
152
+ when 0
153
+ rows.values.should eq [nil, nil, nil, nil, nil]
154
+ when 1
155
+ rows.values.should eq [nil, 'simple', nil, 'workbook', 'sheet1']
156
+ when 2
157
+ rows.values.should eq [nil, 'foo', nil, nil, 'foobaaa']
158
+ when 3
159
+ rows.values.should eq [nil, nil, nil, nil, nil]
160
+ when 4
161
+ rows.values.should eq [nil, 'matz', nil, 'is', 'nice']
162
+ end
163
+ end
164
+ end
165
+ end
166
+
167
+ end
168
+
169
+ describe "#each_row_with_index" do
170
+ it "should read with index" do
171
+ @sheet.each_row_with_index do |rows, idx|
172
+ case idx
173
+ when 0
174
+ rows.values.should eq ['simple', 'workbook', 'sheet1']
175
+ when 1
176
+ rows.values.should eq ['foo', nil, 'foobaaa']
177
+ when 2
178
+ rows.values.should eq ['matz', 'is', 'nice']
179
+ end
180
+ end
181
+ end
182
+
183
+ context "with argument 1" do
184
+ it "should read from second row, index is started 0" do
185
+ @sheet.each_row_with_index(1) do |rows, idx|
186
+ case idx
187
+ when 0
188
+ rows.values.should eq ['foo', nil, 'foobaaa']
189
+ when 1
190
+ rows.values.should eq ['matz', 'is', 'nice']
191
+ end
192
+ end
193
+ end
194
+ end
195
+
196
+ end
197
+
198
+ describe "#each_column" do
199
+ it "items should RobustExcelOle::Range" do
200
+ @sheet.each_column do |columns|
201
+ columns.should be_kind_of RobustExcelOle::Range
202
+ end
203
+ end
204
+
205
+ context "with argument 1" do
206
+ it "should read from second column" do
207
+ @sheet.each_column(1) do |columns|
208
+ case columns.column
209
+ when 2
210
+ columns.values.should eq ['workbook', nil, 'is']
211
+ when 3
212
+ columns.values.should eq ['sheet1', 'foobaaa', 'nice']
213
+ end
214
+ end
215
+ end
216
+ end
217
+
218
+ context "read sheet with blank" do
219
+ include_context "sheet 'open book with blank'"
220
+
221
+ it 'should get from ["A1"]' do
222
+ @sheet_with_blank.each_column do |columns|
223
+ case columns.column- 1
224
+ when 0
225
+ columns.values.should eq [nil, nil, nil, nil, nil]
226
+ when 1
227
+ columns.values.should eq [nil, 'simple', 'foo', nil, 'matz']
228
+ when 2
229
+ columns.values.should eq [nil, nil, nil, nil, nil]
230
+ when 3
231
+ columns.values.should eq [nil, 'workbook', nil, nil, 'is']
232
+ when 4
233
+ columns.values.should eq [nil, 'sheet1', 'foobaaa', nil, 'nice']
234
+ end
235
+ end
236
+ end
237
+ end
238
+
239
+ context "read sheet which last cell is merged" do
240
+ before do
241
+ @book_merge_cells = RobustExcelOle::Book.open(@dir + '/merge_cells.xls')
242
+ @sheet_merge_cell = @book_merge_cells[0]
243
+ end
244
+
245
+ after do
246
+ @book_merge_cells.close
247
+ end
248
+
249
+ it "should get from ['A1'] to ['C2']" do
250
+ columns_values = []
251
+ @sheet_merge_cell.each_column do |columns|
252
+ columns_values << columns.values
253
+ end
254
+ columns_values.should eq [
255
+ [nil, 'first merged', nil, 'merged'],
256
+ [nil, 'first merged', 'first', 'merged'],
257
+ [nil, 'first merged', 'second', 'merged'],
258
+ [nil, nil, 'third', 'merged']
259
+ ]
260
+ end
261
+
262
+ end
263
+
264
+ end
265
+
266
+ describe "#each_column_with_index" do
267
+ it "should read with index" do
268
+ @sheet.each_column_with_index do |columns, idx|
269
+ case idx
270
+ when 0
271
+ columns.values.should eq ['simple', 'foo', 'matz']
272
+ when 1
273
+ columns.values.should eq ['workbook', nil, 'is']
274
+ when 2
275
+ columns.values.should eq ['sheet1', 'foobaaa', 'nice']
276
+ end
277
+ end
278
+ end
279
+
280
+ context "with argument 1" do
281
+ it "should read from second column, index is started 0" do
282
+ @sheet.each_column_with_index(1) do |column_range, idx|
283
+ case idx
284
+ when 0
285
+ column_range.values.should eq ['workbook', nil, 'is']
286
+ when 1
287
+ column_range.values.should eq ['sheet1', 'foobaaa', 'nice']
288
+ end
289
+ end
290
+ end
291
+ end
292
+ end
293
+
294
+ describe "#row_range" do
295
+ context "with second argument" do
296
+ before do
297
+ @row_range = @sheet.row_range(0, 1..2)
298
+ end
299
+
300
+ it { @row_range.should be_kind_of RobustExcelOle::Range }
301
+
302
+ it "should get range cells of second argument" do
303
+ @row_range.values.should eq ['workbook', 'sheet1']
304
+ end
305
+ end
306
+
307
+ context "without second argument" do
308
+ before do
309
+ @row_range = @sheet.row_range(2)
310
+ end
311
+
312
+ it "should get all cells" do
313
+ @row_range.values.should eq ['matz', 'is', 'nice']
314
+ end
315
+ end
316
+
317
+ end
318
+
319
+ describe "#col_range" do
320
+ context "with second argument" do
321
+ before do
322
+ @col_range = @sheet.col_range(0, 1..2)
323
+ end
324
+
325
+ it { @col_range.should be_kind_of RobustExcelOle::Range }
326
+
327
+ it "should get range cells of second argument" do
328
+ @col_range.values.should eq ['foo', 'matz']
329
+ end
330
+ end
331
+
332
+ context "without second argument" do
333
+ before do
334
+ @col_range = @sheet.col_range(1)
335
+ end
336
+
337
+ it "should get all cells" do
338
+ @col_range.values.should eq ['workbook', nil, 'is']
339
+ end
340
+ end
341
+
342
+ end
343
+
344
+ describe "#method_missing" do
345
+ it "can access COM method" do
346
+ @sheet.Cells(1,1).Value.should eq 'simple'
347
+ end
348
+
349
+ context "unknown method" do
350
+ it { expect { @sheet.hogehogefoo }.to raise_error }
351
+ end
352
+ end
353
+
354
+ end
355
+ end
@@ -0,0 +1,35 @@
1
+ # -*- coding: utf-8 -*-
2
+ require "rspec"
3
+ require 'tmpdir'
4
+ require "fileutils"
5
+ require File.join(File.dirname(__FILE__), '../lib/robust_excel_ole')
6
+
7
+ module RobustExcelOle::SpecHelpers
8
+ def create_tmpdir
9
+ tmpdir = Dir.mktmpdir
10
+ FileUtils.cp_r(File.join(File.dirname(__FILE__), 'data'), tmpdir)
11
+ tmpdir + '/data'
12
+ end
13
+
14
+ def rm_tmp(tmpdir)
15
+ FileUtils.remove_entry_secure(File.dirname(tmpdir))
16
+ end
17
+
18
+ # This method is almost copy of wycats's implementation.
19
+ # http://pochi.hatenablog.jp/entries/2010/03/24
20
+ def capture(stream)
21
+ begin
22
+ stream = stream.to_s
23
+ eval "$#{stream} = StringIO.new"
24
+ yield
25
+ result = eval("$#{stream}").string
26
+ ensure
27
+ eval("$#{stream} = #{stream.upcase}")
28
+ end
29
+ result
30
+ end
31
+ end
32
+
33
+ RSpec.configure do |config|
34
+ config.include RobustExcelOle::SpecHelpers
35
+ end
data/version.rb ADDED
@@ -0,0 +1,3 @@
1
+ module RobustExcelOle
2
+ VERSION = "0.2.0"
3
+ end
metadata ADDED
@@ -0,0 +1,203 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: robust_excel_ole
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
11
+ platform: ruby
12
+ authors:
13
+ - traths
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2014-09-19 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rake
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 63
30
+ segments:
31
+ - 0
32
+ - 9
33
+ - 2
34
+ version: 0.9.2
35
+ type: :development
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: rspec
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 23
46
+ segments:
47
+ - 2
48
+ - 6
49
+ - 0
50
+ version: 2.6.0
51
+ type: :development
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: rb-fchange
55
+ prerelease: false
56
+ requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ hash: 21
62
+ segments:
63
+ - 0
64
+ - 0
65
+ - 5
66
+ version: 0.0.5
67
+ type: :development
68
+ version_requirements: *id003
69
+ - !ruby/object:Gem::Dependency
70
+ name: wdm
71
+ prerelease: false
72
+ requirement: &id004 !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ hash: 25
78
+ segments:
79
+ - 0
80
+ - 0
81
+ - 3
82
+ version: 0.0.3
83
+ type: :development
84
+ version_requirements: *id004
85
+ - !ruby/object:Gem::Dependency
86
+ name: win32console
87
+ prerelease: false
88
+ requirement: &id005 !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ hash: 31
94
+ segments:
95
+ - 1
96
+ - 3
97
+ - 2
98
+ version: 1.3.2
99
+ type: :development
100
+ version_requirements: *id005
101
+ - !ruby/object:Gem::Dependency
102
+ name: guard-rspec
103
+ prerelease: false
104
+ requirement: &id006 !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ hash: 9
110
+ segments:
111
+ - 2
112
+ - 1
113
+ - 1
114
+ version: 2.1.1
115
+ type: :development
116
+ version_requirements: *id006
117
+ description: RobustExcelOle is to wrap the win32ole, and easy to use Excel operations with ruby. Detailed description please see the README.
118
+ email:
119
+ - traths@cs.uni-potsdam.de
120
+ executables: []
121
+
122
+ extensions: []
123
+
124
+ extra_rdoc_files:
125
+ - README.rdoc
126
+ - LICENSE
127
+ files:
128
+ - .gitignore
129
+ - Gemfile
130
+ - Guardfile
131
+ - LICENSE
132
+ - README.rdoc
133
+ - Rakefile
134
+ - lib/robust_excel_ole.rb
135
+ - lib/robust_excel_ole/book.rb
136
+ - lib/robust_excel_ole/cell.rb
137
+ - lib/robust_excel_ole/cygwin.rb
138
+ - lib/robust_excel_ole/excel_app.rb
139
+ - lib/robust_excel_ole/range.rb
140
+ - lib/robust_excel_ole/robustexcelole.sublime-project
141
+ - lib/robust_excel_ole/robustexcelole.sublime-workspace
142
+ - lib/robust_excel_ole/sheet.rb
143
+ - lib/robust_excel_ole/sp
144
+ - lib/robust_excel_ole/version.rb
145
+ - lib/spec_helper.rb
146
+ - robust_excel_ole.gemspec
147
+ - robust_excel_ole_example.rb
148
+ - spec/book_spec.rb
149
+ - spec/cell_spec.rb
150
+ - spec/cygwin_spec.rb
151
+ - spec/data/book_with_blank.xls
152
+ - spec/data/different_simple.xls
153
+ - spec/data/merge_cells.xls
154
+ - spec/data/more_data/simple.xls
155
+ - spec/data/protected_sheet.xls
156
+ - spec/data/simple.xls
157
+ - spec/data/simple.xlsm
158
+ - spec/data/simple.xlsx
159
+ - spec/excel_app_spec.rb
160
+ - spec/helpers/key_sender.rb
161
+ - spec/range_spec.rb
162
+ - spec/sheet_spec.rb
163
+ - spec/spec_helper.rb
164
+ - version.rb
165
+ has_rdoc: true
166
+ homepage: https://github.com/Thomas008/robust_excel_ole
167
+ licenses: []
168
+
169
+ post_install_message:
170
+ rdoc_options:
171
+ - --main
172
+ - README.rdoc
173
+ - --charset
174
+ - utf-8
175
+ require_paths:
176
+ - lib
177
+ required_ruby_version: !ruby/object:Gem::Requirement
178
+ none: false
179
+ requirements:
180
+ - - ">="
181
+ - !ruby/object:Gem::Version
182
+ hash: 3
183
+ segments:
184
+ - 0
185
+ version: "0"
186
+ required_rubygems_version: !ruby/object:Gem::Requirement
187
+ none: false
188
+ requirements:
189
+ - - ">="
190
+ - !ruby/object:Gem::Version
191
+ hash: 3
192
+ segments:
193
+ - 0
194
+ version: "0"
195
+ requirements: []
196
+
197
+ rubyforge_project: robust_excel_ole
198
+ rubygems_version: 1.3.7
199
+ signing_key:
200
+ specification_version: 3
201
+ summary: RobustExcelOle is a wrapper library that specializes in the operation of Excel win32ole.
202
+ test_files: []
203
+