robust_excel_ole 1.19.10 → 1.21

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,3 @@
1
1
  module RobustExcelOle
2
- VERSION = "1.19.10"
2
+ VERSION = "1.21"
3
3
  end
@@ -97,6 +97,9 @@ module RobustExcelOle
97
97
  when String
98
98
  file = file_or_workbook
99
99
  raise FileNotFound, "file #{General.absolute_path(file).inspect} is a directory" if File.directory?(file)
100
+ when ->(n){ n.respond_to? :to_path }
101
+ file = file_or_workbook.to_path
102
+ raise FileNotFound, "file #{General.absolute_path(file).inspect} is a directory" if File.directory?(file)
100
103
  else
101
104
  raise TypeREOError, 'given object is neither a filename, a Win32ole, nor a Workbook object'
102
105
  end
@@ -1001,7 +1004,7 @@ module RobustExcelOle
1001
1004
 
1002
1005
  # @private
1003
1006
  def inspect
1004
- '#<Workbook: ' + ('not alive ' unless alive?).to_s + (File.basename(self.filename) if alive?).to_s + " #{@ole_workbook} #{@excel}" + '>'
1007
+ '#<Workbook: ' + ('not alive ' unless alive?).to_s + (File.basename(self.filename) if alive?).to_s + " #{@excel}" + '>'
1005
1008
  end
1006
1009
 
1007
1010
  # @private
@@ -14,6 +14,8 @@ module RobustExcelOle
14
14
  attr_reader :ole_worksheet
15
15
  attr_reader :workbook
16
16
 
17
+ alias ole_object ole_worksheet
18
+
17
19
  def initialize(win32_worksheet)
18
20
  @ole_worksheet = win32_worksheet
19
21
  if @ole_worksheet.ProtectContents
@@ -224,6 +226,16 @@ module RobustExcelOle
224
226
  self.Name == other_worksheet.Name
225
227
  end
226
228
 
229
+ # @private
230
+ # returns true, if the worksheet object responds to VBA methods, false otherwise
231
+ def alive?
232
+ @ole_worksheet.UsedRange
233
+ true
234
+ rescue
235
+ # trace $!.message
236
+ false
237
+ end
238
+
227
239
  # @private
228
240
  def self.workbook_class
229
241
  @workbook_class ||= begin
@@ -249,6 +261,8 @@ module RobustExcelOle
249
261
  self.to_s
250
262
  end
251
263
 
264
+ include MethodHelpers
265
+
252
266
  private
253
267
 
254
268
  def method_missing(name, *args)
@@ -59,7 +59,7 @@ describe Bookstore do
59
59
  @file_path = "spec/data/workbook.xls"
60
60
  @absolute_file_path = "C:/gim/ats/aSrc/gems/robust_excel_ole/spec/data/workbook.xls"
61
61
  @network_path = "N:/data/workbook.xls"
62
- @hostname_share_path = "DESKTOP-A3C5CJ6/spec/workbook.xls"
62
+ @hostname_share_path = "DESKTOP-A3C5CJ6/spec/data/workbook.xls"
63
63
  end
64
64
 
65
65
  after do
@@ -29,6 +29,10 @@ module RobustExcelOle
29
29
  @linked_file = @dir + '/workbook_linked.xlsm'
30
30
  @simple_file_xlsm = @dir + '/workbook.xls'
31
31
  @simple_file_xlsx = @dir + '/workbook.xlsx'
32
+ @network_path = "N:/data/workbook.xls"
33
+ @hostname_share_path = "//DESKTOP-A3C5CJ6/spec/data/workbook.xls"
34
+ @simple_file_extern = "D:/data/workbook.xls"
35
+ @hostname_share_path = "//DESKTOP-A3C5CJ6/spec/data/workbook.xls"
32
36
  end
33
37
 
34
38
  after do
@@ -186,7 +190,7 @@ module RobustExcelOle
186
190
  filename = 'C:/Dokumente und Einstellungen/Zauberthomas/Eigene Dateien/robust_excel_ole/spec/book_spec.rb'
187
191
  absolute_path(filename).gsub("\\","/").should == filename
188
192
  end
189
- end
193
+ end
190
194
  end
191
195
 
192
196
  describe "canonize" do
@@ -230,6 +234,14 @@ module RobustExcelOle
230
234
  }.to raise_error(TypeREOError, "No string given to canonize, but 1")
231
235
  end
232
236
 
237
+ it "should yield the hostname share path" do
238
+ General.canonize(@network_path).should == normalize(@hostname_share_path).downcase
239
+ General.canonize(@hostname_share_path).should == normalize(@hostname_share_path).downcase
240
+ General.canonize(@simple_file).should == normalize(@simple_file).downcase
241
+ General.canonize(@simple_file_extern).should == normalize(@simple_file_extern).downcase
242
+ end
243
+
244
+
233
245
  end
234
246
  end
235
247
 
@@ -45,7 +45,7 @@ describe ListObject do
45
45
  ole_table = @sheet.ListObjects.Item(1)
46
46
  table = Table.new(ole_table)
47
47
  table.Name.should == "table3"
48
- table.HeaderRowRange.Value.first.should == ["Number","Person","Amount","Time","Date"]
48
+ table.HeaderRowRange.Value.first.should == ["Number","Person","Amount","Time","Price"]
49
49
  table.ListRows.Count.should == 6
50
50
  @sheet[3,4].Value.should == "Number"
51
51
  end
@@ -54,7 +54,7 @@ describe ListObject do
54
54
  ole_table = @sheet.ListObjects.Item(1)
55
55
  table = Table.new(@sheet, "table3")
56
56
  table.Name.should == "table3"
57
- table.HeaderRowRange.Value.first.should == ["Number","Person","Amount","Time","Date"]
57
+ table.HeaderRowRange.Value.first.should == ["Number","Person","Amount","Time","Price"]
58
58
  table.ListRows.Count.should == 6
59
59
  @sheet[3,4].Value.should == "Number"
60
60
  end
@@ -63,7 +63,7 @@ describe ListObject do
63
63
  ole_table = @sheet.ListObjects.Item(1)
64
64
  table = Table.new(@sheet, 1)
65
65
  table.Name.should == "table3"
66
- table.HeaderRowRange.Value.first.should == ["Number","Person","Amount","Time","Date"]
66
+ table.HeaderRowRange.Value.first.should == ["Number","Person","Amount","Time","Price"]
67
67
  table.ListRows.Count.should == 6
68
68
  @sheet[3,4].Value.should == "Number"
69
69
  end
@@ -80,7 +80,7 @@ describe ListObject do
80
80
  ole_table = @sheet.ListObjects.Item(1)
81
81
  table = Table.new(@sheet.ole_worksheet, "table3")
82
82
  table.Name.should == "table3"
83
- table.HeaderRowRange.Value.first.should == ["Number","Person","Amount","Time","Date"]
83
+ table.HeaderRowRange.Value.first.should == ["Number","Person","Amount","Time","Price"]
84
84
  table.ListRows.Count.should == 6
85
85
  @sheet[3,4].Value.should == "Number"
86
86
  end
@@ -89,7 +89,7 @@ describe ListObject do
89
89
  ole_table = @sheet.ListObjects.Item(1)
90
90
  table = Table.new(@sheet.ole_worksheet, 1)
91
91
  table.Name.should == "table3"
92
- table.HeaderRowRange.Value.first.should == ["Number","Person","Amount","Time","Date"]
92
+ table.HeaderRowRange.Value.first.should == ["Number","Person","Amount","Time","Price"]
93
93
  table.ListRows.Count.should == 6
94
94
  @sheet[3,4].Value.should == "Number"
95
95
  end
@@ -103,20 +103,27 @@ describe ListObject do
103
103
  context "with new table" do
104
104
 
105
105
  before do
106
- @table = Table.new(@sheet, "table_name", [1,1], 3, ["Person","Amount"])
106
+ @table = Table.new(@sheet, "table_name", [1,1], 3, ["Person","Amo%untSales"])
107
107
  @table_row1 = @table[1]
108
108
  end
109
109
 
110
- it "should set and read values" do
110
+ it "should read and set values via alternative column names" do
111
111
  @table_row1.person.should be nil
112
112
  @table_row1.person = "John"
113
113
  @table_row1.person.should == "John"
114
114
  @sheet[2,1].Value.should == "John"
115
- @table_row1.amount.should be nil
116
- @table_row1.amount = 42
117
- @table_row1.amount.should == 42
115
+ @table_row1.amount_sales.should be nil
116
+ @table_row1.amount_sales = 42
117
+ @table_row1.amount_sales.should == 42
118
118
  @sheet[2,2].Value.should == 42
119
+ @table_row1.Person = "Herbert"
120
+ @table_row1.Person.should == "Herbert"
121
+ @sheet[2,1].Value.should == "Herbert"
122
+ @table_row1.AmountSales = 80
123
+ @table_row1.AmountSales.should == 80
124
+ @sheet[2,2].Value.should == 80
119
125
  end
126
+
120
127
  end
121
128
 
122
129
  context "with type-lifted ole list object" do
@@ -132,12 +139,218 @@ describe ListObject do
132
139
  @table_row1.number = 1
133
140
  @table_row1.number.should == 1
134
141
  @sheet[4,4].Value.should == 1
135
- @table_row1.person.should == "Herbert"
136
- @table_row1.person = "John"
137
142
  @table_row1.person.should == "John"
138
- @sheet[4,5].Value.should == "John"
143
+ @table_row1.person = "Herbert"
144
+ @table_row1.person.should == "Herbert"
145
+ @sheet[4,5].Value.should == "Herbert"
139
146
  end
140
147
  end
141
148
 
142
149
  end
150
+
151
+ describe "reading and setting contents of rows and columns" do
152
+
153
+ before do
154
+ ole_table = @sheet.ListObjects.Item(1)
155
+ @table = Table.new(ole_table)
156
+ end
157
+
158
+ it "should read contents of a column" do
159
+ @table.column_values("Person").should == ["John","Fred",nil,"Angel",nil,"Werner"]
160
+ expect{
161
+ @table.column_values("P")
162
+ }.to raise_error(TableError)
163
+ end
164
+
165
+ it "should set contents of a column" do
166
+ @table.set_column_values("Person",["H",nil,nil,nil,"G","W"])
167
+ @table.ListColumns.Item(2).Range.Value.should == [["Person"],["H"],[nil],[nil],[nil],["G"],["W"]]
168
+ @table.set_column_values("Person",["T","Z"])
169
+ @table.ListColumns.Item(2).Range.Value.should == [["Person"],["T"],["Z"],[nil],[nil],["G"],["W"]]
170
+ expect{
171
+ @table.set_column_values("P",["H",nil,nil,nil,"G","W"])
172
+ }.to raise_error(TableError)
173
+ end
174
+
175
+ it "should read contents of a row" do
176
+ @table.row_values(1).should == [3.0, "John", 50.0, 0.5, 30]
177
+ @table[1].values.should == [3.0, "John", 50.0, 0.5, 30]
178
+ expect{
179
+ @table.row_values(9)
180
+ }.to raise_error(TableError)
181
+ end
182
+
183
+ it "should set contents of a row" do
184
+ @table.set_row_values(1, [5, "George", 30.0, 0.2, 50])
185
+ @table.ListRows.Item(1).Range.Value.first.should == [5, "George", 30.0, 0.2, 50]
186
+ @table.set_row_values(1, [6, "Martin"])
187
+ @table.ListRows.Item(1).Range.Value.first.should == [6, "Martin", 30.0, 0.2, 50]
188
+ @table[1].set_values([2, "Merlin", 20.0, 0.1, 40])
189
+ @table[1].set_values([4, "John"])
190
+ @table.ListRows.Item(1).Range.Value.first.should == [4, "John", 20.0, 0.1, 40]
191
+ expect{
192
+ @table.set_row_values(9, [5, "George", 30.0, 0.2, 50])
193
+ }.to raise_error(TableError)
194
+ end
195
+
196
+ end
197
+
198
+ describe "renaming, adding and deleting columns and rows" do
199
+
200
+ before do
201
+ ole_table = @sheet.ListObjects.Item(1)
202
+ @table = Table.new(ole_table)
203
+ end
204
+
205
+ it "should list column names" do
206
+ @table.column_names.should == @table.HeaderRowRange.Value.first
207
+ end
208
+
209
+ it "should rename a column name" do
210
+ @table.rename_column("Person", "P")
211
+ @table.HeaderRowRange.Value.first.should == ["Number","P","Amount","Time","Price"]
212
+ end
213
+
214
+ it "should append a column" do
215
+ @table.add_column("column_name")
216
+ column_names = @table.HeaderRowRange.Value.first.should == ["Number","Person", "Amount","Time","Price", "column_name"]
217
+ end
218
+
219
+ it "should add a column" do
220
+ @table.add_column("column_name", 3)
221
+ column_names = @table.HeaderRowRange.Value.first.should == ["Number","Person","column_name","Amount","Time","Price"]
222
+ expect{
223
+ @table.add_column(8, "column_name")
224
+ }.to raise_error(TableError)
225
+ end
226
+
227
+ it "should add a column with contents" do
228
+ @table.add_column("column_name", 3, ["a","b","c","d","e","f"])
229
+ column_names = @table.HeaderRowRange.Value.first.should == ["Number","Person","column_name","Amount","Time","Price"]
230
+ @table.ListColumns.Item(3).Range.Value.should == [["column_name"],["a"],["b"],["c"],["d"],["e"],["f"]]
231
+ end
232
+
233
+ it "should delete a column" do
234
+ @table.delete_column(4)
235
+ @table.HeaderRowRange.Value.first.should == ["Number","Person", "Amount","Price"]
236
+ expect{
237
+ @table.delete_column(6)
238
+ }.to raise_error(TableError)
239
+ end
240
+
241
+ it "should append a row" do
242
+ @table.add_row
243
+ listrows = @table.ListRows
244
+ listrows.Item(listrows.Count).Range.Value.first.should == [nil,nil,nil,nil,nil]
245
+ end
246
+
247
+ it "should add a row" do
248
+ @table.add_row(2)
249
+ listrows = @table.ListRows
250
+ listrows.Item(1).Range.Value.first.should == [3.0, "John", 50.0, 0.5, 30]
251
+ listrows.Item(2).Range.Value.first.should == [nil,nil,nil,nil,nil]
252
+ listrows.Item(3).Range.Value.first.should == [2.0, "Fred", nil, 0.5416666666666666, 40]
253
+ expect{
254
+ @table.add_row(9)
255
+ }.to raise_error(TableError)
256
+ end
257
+
258
+ it "should add a row with contents" do
259
+ @table.add_row(2, [2.0, "Herbert", 30.0, 0.25, 40])
260
+ listrows = @table.ListRows
261
+ listrows.Item(1).Range.Value.first.should == [3.0, "John", 50.0, 0.5, 30]
262
+ listrows.Item(2).Range.Value.first.should == [2.0, "Herbert", 30.0, 0.25, 40]
263
+ listrows.Item(3).Range.Value.first.should == [2.0, "Fred", nil, 0.5416666666666666, 40]
264
+ end
265
+
266
+ it "should delete a row" do
267
+ @table.delete_row(4)
268
+ listrows = @table.ListRows
269
+ listrows.Item(5).Range.Value.first.should == [1,"Werner",40,0.5, 80]
270
+ listrows.Item(4).Range.Value.first.should == [nil,nil,nil,nil,nil]
271
+ expect{
272
+ @table.delete_row(8)
273
+ }.to raise_error(TableError)
274
+ end
275
+
276
+ it "should delete the contents of a column" do
277
+ @table.ListColumns.Item(3).Range.Value.should == [["Amount"],[50],[nil],[nil],[100],[nil],[40]]
278
+ @table.delete_column_values(3)
279
+ @table.HeaderRowRange.Value.first.should == ["Number","Person", "Amount", "Time","Price"]
280
+ @table.ListColumns.Item(3).Range.Value.should == [["Amount"],[nil],[nil],[nil],[nil],[nil],[nil]]
281
+ @table.ListColumns.Item(1).Range.Value.should == [["Number"],[3],[2],[nil],[3],[nil],[1]]
282
+ @table.delete_column_values("Number")
283
+ @table.ListColumns.Item(1).Range.Value.should == [["Number"],[nil],[nil],[nil],[nil],[nil],[nil]]
284
+ expect{
285
+ @table.delete_column_values("N")
286
+ }.to raise_error(TableError)
287
+ end
288
+
289
+ it "should delete the contents of a row" do
290
+ @table.ListRows.Item(2).Range.Value.first.should == [2.0, "Fred", nil, 0.5416666666666666, 40]
291
+ @table.delete_row_values(2)
292
+ @table.ListRows.Item(2).Range.Value.first.should == [nil,nil,nil,nil,nil]
293
+ @table.ListRows.Item(1).Range.Value.first.should == [3.0, "John", 50.0, 0.5, 30]
294
+ @table[1].delete_values
295
+ @table.ListRows.Item(1).Range.Value.first.should == [nil,nil,nil,nil,nil]
296
+ expect{
297
+ @table.delete_row_values(9)
298
+ }.to raise_error(TableError)
299
+ end
300
+
301
+ it "should delete empty rows" do
302
+ @table.delete_empty_rows
303
+ @table.ListRows.Count.should == 4
304
+ @table.ListRows.Item(1).Range.Value.first.should == [3.0, "John", 50.0, 0.5, 30]
305
+ @table.ListRows.Item(2).Range.Value.first.should == [2.0, "Fred", nil, 0.5416666666666666, 40]
306
+ @table.ListRows.Item(3).Range.Value.first.should == [3, "Angel", 100, 0.6666666666666666, 60]
307
+ @table.ListRows.Item(4).Range.Value.first.should == [1,"Werner",40,0.5, 80]
308
+ end
309
+
310
+ it "should delete empty columns" do
311
+ @table.delete_column_values(4)
312
+ @table.ListColumns.Count.should == 5
313
+ @table.HeaderRowRange.Value.first.should == ["Number","Person", "Amount", "Time","Price"]
314
+ @table.delete_empty_columns
315
+ @table.ListColumns.Count.should == 4
316
+ @table.HeaderRowRange.Value.first.should == ["Number","Person", "Amount","Price"]
317
+ end
318
+ end
319
+
320
+ describe "find all cells of a given value" do
321
+
322
+ before do
323
+ ole_table = @sheet.ListObjects.Item(1)
324
+ @table = Table.new(ole_table)
325
+ end
326
+
327
+ it "should find all cells" do
328
+ cells = @table.find_cells(40)
329
+ cells[0].Row.should == 5
330
+ cells[0].Column.should == 8
331
+ cells[1].Row.should == 9
332
+ cells[1].Column.should == 6
333
+ puts "cells[0]: #{[cells[0]]}"
334
+ p "cells[0]: #{[cells[0]]}"
335
+ end
336
+
337
+ end
338
+
339
+ describe "sort the table" do
340
+
341
+ before do
342
+ ole_table = @sheet.ListObjects.Item(1)
343
+ @table = Table.new(ole_table)
344
+ end
345
+
346
+ it "should sort the table according to first table" do
347
+ @table.sort("Number")
348
+ @table.ListRows.Item(1).Range.Value.first.should == [1,"Werner",40,0.5, 80]
349
+ @table.ListRows.Item(2).Range.Value.first.should == [2, "Fred", nil, 0.5416666666666666, 40]
350
+ @table.ListRows.Item(3).Range.Value.first.should == [3, "John", 50.0, 0.5, 30]
351
+ @table.ListRows.Item(4).Range.Value.first.should == [3, "Angel", 100, 0.6666666666666666, 60]
352
+ end
353
+
354
+ end
355
+
143
356
  end
@@ -1,6 +1,7 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
3
  require File.join(File.dirname(__FILE__), './spec_helper')
4
+ require 'pathname'
4
5
 
5
6
  $VERBOSE = nil
6
7
 
@@ -20,6 +21,7 @@ describe Workbook do
20
21
  before do
21
22
  @dir = create_tmpdir
22
23
  @simple_file = @dir + '/workbook.xls'
24
+ @pathname_file = Pathname(@dir) + 'workbook.xls'
23
25
  @simple_save_file = @dir + '/workbook_save.xls'
24
26
  @different_file = @dir + '/different_workbook.xls'
25
27
  @simple_file_other_path = @dir + '/more_data/workbook.xls'
@@ -47,6 +49,16 @@ describe Workbook do
47
49
  @book.close
48
50
  end
49
51
  end
52
+
53
+ context "with pathname" do
54
+ it "open an existing file" do
55
+ expect {
56
+ @book = Workbook.open(@pathname_file)
57
+ }.to_not raise_error
58
+ @book.should be_a Workbook
59
+ @book.close
60
+ end
61
+ end
50
62
  end
51
63
 
52
64
  describe "Book" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: robust_excel_ole
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.19.10
4
+ version: '1.21'
5
5
  platform: ruby
6
6
  authors:
7
7
  - traths
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-07-12 00:00:00.000000000 Z
11
+ date: 2020-08-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pry
@@ -49,7 +49,7 @@ email:
49
49
  - Thomas.Raths@gmx.net
50
50
  executables:
51
51
  - jreo
52
- - reo_old
52
+ - reo
53
53
  extensions: []
54
54
  extra_rdoc_files:
55
55
  - README.rdoc
@@ -79,7 +79,7 @@ files:
79
79
  - benchmarking/simple_xlsx_reader_example.rb
80
80
  - benchmarking/spreadsheet_example.rb
81
81
  - bin/jreo
82
- - bin/reo_old
82
+ - bin/reo
83
83
  - docs/README_excel.rdoc
84
84
  - docs/README_open.rdoc
85
85
  - docs/README_ranges.rdoc