robust_excel_ole 1.20 → 1.23

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.
@@ -62,7 +62,7 @@ module RobustExcelOle
62
62
  # sets the contents of a range
63
63
  # @param [String] name the name of a range
64
64
  # @param [Variant] value the contents of the range
65
- # @option opts [Symbol] :color the color of the cell when set
65
+ # @option opts [Symbol] :color the color of the range when set
66
66
  def set_namevalue_glob(name, value, opts = { })
67
67
  begin
68
68
  name_obj = begin
@@ -177,36 +177,10 @@ module RobustExcelOle
177
177
  end
178
178
 
179
179
  # creates a range from a given defined name or address
180
- # range(address) does work for Worksheet objects only
181
- # @params [Variant] range name or address
180
+ # @params [Variant] defined name or address, and optional a worksheet
182
181
  # @return [Range] a range
183
- def range(name_or_address, address2 = :__not_provided)
184
- begin
185
- worksheet = self if self.is_a?(Worksheet)
186
- if address2 == :__not_provided
187
- range = if name_or_address.is_a?(String)
188
- begin
189
- RobustExcelOle::Range.new(name_object(name_or_address).RefersToRange, worksheet)
190
- rescue NameNotFound
191
- nil
192
- end
193
- end
194
- end
195
- if self.is_a?(Worksheet) && (range.nil? || (address2 != :__not_provided))
196
- address = name_or_address
197
- address = [name_or_address,address2] unless address2 == :__not_provided
198
- self.Names.Add('__dummy001',nil,true,nil,nil,nil,nil,nil,nil,'=' + address_tool.as_r1c1(address))
199
- range = RobustExcelOle::Range.new(name_object('__dummy001').RefersToRange, worksheet)
200
- self.Names.Item('__dummy001').Delete
201
- workbook = self.is_a?(Workbook) ? self : self.workbook
202
- workbook.save
203
- range
204
- end
205
- rescue WIN32OLERuntimeError, Java::OrgRacobCom::ComFailException
206
- address2_string = address2.nil? ? "" : ", #{address2.inspect}"
207
- raise RangeNotCreated, "cannot create range (#{name_or_address.inspect}#{address2_string})"
208
- end
209
- range
182
+ def range(*args)
183
+ raise RangeNotCreated, "not yet implemented"
210
184
  end
211
185
 
212
186
  def name2range(name) # :deprecated: #
@@ -0,0 +1,23 @@
1
+
2
+ # -*- coding: utf-8 -*-
3
+
4
+ module RobustExcelOle
5
+
6
+ class VbaObjects < Base
7
+
8
+ def to_reo -> VbaObjects
9
+
10
+ def address_tool -> AddressTool
11
+
12
+ end
13
+
14
+ class RangeNotEvaluatable < MiscREOError
15
+ end
16
+
17
+ class OptionInvalid < MiscREOError
18
+ end
19
+
20
+ class ObjectNotAlive < MiscREOError
21
+ end
22
+
23
+ end
@@ -1,3 +1,3 @@
1
1
  module RobustExcelOle
2
- VERSION = "1.20"
2
+ VERSION = "1.23"
3
3
  end
@@ -823,11 +823,12 @@ module RobustExcelOle
823
823
  opts = sheet
824
824
  sheet = nil
825
825
  end
826
- new_sheet_name = opts.delete(:as)
827
- last_sheet_local = last_sheet
828
- after_or_before, base_sheet = opts.to_a.first || [:after, last_sheet_local]
829
- base_sheet_ole = base_sheet.ole_worksheet
830
826
  begin
827
+ sheet = sheet.to_reo unless sheet.nil?
828
+ new_sheet_name = opts.delete(:as)
829
+ last_sheet_local = last_sheet
830
+ after_or_before, base_sheet = opts.to_a.first || [:after, last_sheet_local]
831
+ base_sheet_ole = base_sheet.to_reo.ole_worksheet
831
832
  if !::COPYSHEETS_JRUBY_BUG
832
833
  add_or_copy_sheet_simple(sheet, { after_or_before.to_s => base_sheet_ole })
833
834
  else
@@ -843,7 +844,7 @@ module RobustExcelOle
843
844
  end
844
845
  end
845
846
  end
846
- rescue WIN32OLERuntimeError, NameNotFound, Java::OrgRacobCom::ComFailException
847
+ rescue # WIN32OLERuntimeError, NameNotFound, Java::OrgRacobCom::ComFailException
847
848
  raise WorksheetREOError, "could not add given worksheet #{sheet.inspect}"
848
849
  end
849
850
  new_sheet = worksheet_class.new(ole_workbook.Activesheet)
@@ -881,6 +882,24 @@ module RobustExcelOle
881
882
  worksheet_class.new(@ole_workbook.Worksheets.Item(1))
882
883
  end
883
884
 
885
+ # creates a range from a given defined name or from a given worksheet and address
886
+ # @params [Variant] defined name or a worksheet
887
+ # @params [Address] address
888
+ # @return [Range] a range
889
+ def range(name_or_worksheet, name_or_address = :__not_provided, address2 = :__not_provided)
890
+ if name_or_worksheet.respond_to?(:gsub)
891
+ name = name_or_worksheet
892
+ RobustExcelOle::Range.new(name_object(name).RefersToRange)
893
+ else
894
+ begin
895
+ worksheet = name_or_worksheet.to_reo
896
+ worksheet.range(name_or_address, address2)
897
+ rescue
898
+ raise RangeNotCreated, "argument error: a defined name or a worksheet and an address must be provided"
899
+ end
900
+ end
901
+ end
902
+
884
903
  # returns the value of a range
885
904
  # @param [String] name the name of a range
886
905
  # @returns [Variant] the value of the range
@@ -892,7 +911,7 @@ module RobustExcelOle
892
911
  # @param [String] name the name of the range
893
912
  # @param [Variant] value the contents of the range
894
913
  def []= (name, value)
895
- set_namevalue_glob(name, value, :color => 42)
914
+ set_namevalue_glob(name, value)
896
915
  end
897
916
 
898
917
  # sets options
@@ -921,7 +940,7 @@ module RobustExcelOle
921
940
 
922
941
  # returns the full file name of the workbook
923
942
  def filename
924
- @ole_workbook.Fullname.tr('\\','/') rescue nil
943
+ General.canonize(@ole_workbook.Fullname.tr('\\','/')) rescue nil
925
944
  end
926
945
 
927
946
  # @private
@@ -1004,7 +1023,7 @@ module RobustExcelOle
1004
1023
 
1005
1024
  # @private
1006
1025
  def inspect
1007
- '#<Workbook: ' + ('not alive ' unless alive?).to_s + (File.basename(self.filename) if alive?).to_s + " #{@ole_workbook} #{@excel}" + '>'
1026
+ '#<Workbook: ' + ('not alive ' unless alive?).to_s + (File.basename(self.filename) if alive?).to_s + " #{@excel}" + '>'
1008
1027
  end
1009
1028
 
1010
1029
  # @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
@@ -62,7 +64,7 @@ module RobustExcelOle
62
64
  end
63
65
  end
64
66
 
65
- # a cell given the defined name or row and column
67
+ # returns a cell given the defined name or row and column
66
68
  # @params row, column, or name
67
69
  # @returns cell, if row and column are given
68
70
  def [] p1, p2 = :__not_provided
@@ -94,7 +96,7 @@ module RobustExcelOle
94
96
  else
95
97
  name, value = p1, p2
96
98
  begin
97
- set_namevalue_glob(name, value, :color => 42)
99
+ set_namevalue_glob(name, value)
98
100
  rescue REOError
99
101
  begin
100
102
  workbook.set_namevalue_glob(name, value)
@@ -116,19 +118,6 @@ module RobustExcelOle
116
118
  end
117
119
  end
118
120
 
119
- =begin
120
- def cellval(x,y)
121
- xy = "#{x}_#{y}"
122
- @cells = { }
123
- begin
124
- @cells[xy] ||= RobustExcelOle::Cell.new(@ole_worksheet.Cells.Item(x, y), @worksheet)
125
- @cells[xy].Value
126
- rescue
127
- raise RangeNotEvaluatable, "cannot read cell (#{p1.inspect},#{p2.inspect})"
128
- end
129
- end
130
- =end
131
-
132
121
  # sets the value of a cell, if row, column and color of the cell are given
133
122
  # @params [Integer] x,y row and column
134
123
  # @option opts [Symbol] :color the color of the cell when set
@@ -224,6 +213,41 @@ module RobustExcelOle
224
213
  self.Name == other_worksheet.Name
225
214
  end
226
215
 
216
+ # creates a range from a given defined name or address
217
+ # @params [Variant] defined name or address
218
+ # @return [Range] a range
219
+ def range(name_or_address, address2 = :__not_provided)
220
+ if name_or_address.respond_to?(:gsub) && address2 == :__not_provided
221
+ name = name_or_address
222
+ range = RobustExcelOle::Range.new(name_object(name).RefersToRange, self) rescue nil
223
+ end
224
+ unless range
225
+ address = name_or_address
226
+ address = [name_or_address,address2] unless address2 == :__not_provided
227
+ workbook.retain_saved do
228
+ begin
229
+ self.Names.Add('__dummy001',nil,true,nil,nil,nil,nil,nil,nil,'=' + address_tool.as_r1c1(address))
230
+ range = RobustExcelOle::Range.new(name_object('__dummy001').RefersToRange, self)
231
+ self.Names.Item('__dummy001').Delete
232
+ rescue
233
+ address2_string = address2.nil? ? "" : ", #{address2.inspect}"
234
+ raise RangeNotCreated, "cannot create range (#{name_or_address.inspect}#{address2_string})"
235
+ end
236
+ end
237
+ end
238
+ range
239
+ end
240
+
241
+ # @private
242
+ # returns true, if the worksheet object responds to VBA methods, false otherwise
243
+ def alive?
244
+ @ole_worksheet.UsedRange
245
+ true
246
+ rescue
247
+ # trace $!.message
248
+ false
249
+ end
250
+
227
251
  # @private
228
252
  def self.workbook_class
229
253
  @workbook_class ||= begin
@@ -246,9 +270,11 @@ module RobustExcelOle
246
270
 
247
271
  # @private
248
272
  def inspect
249
- self.to_s
273
+ to_s
250
274
  end
251
275
 
276
+ include MethodHelpers
277
+
252
278
  private
253
279
 
254
280
  def method_missing(name, *args)
data/reo.bat ADDED
@@ -0,0 +1,3 @@
1
+ @echo off
2
+
3
+ ruby lib/reo_console.rb
@@ -34,6 +34,7 @@ Gem::Specification.new do |s|
34
34
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
35
35
  s.require_paths = ["lib"]
36
36
  s.add_runtime_dependency "pry", '>= 0.12.1'
37
+ s.add_runtime_dependency "pry-bond", '>=0.01'
37
38
  s.add_development_dependency "rspec", '>= 2.6.0'
38
39
  s.required_ruby_version = '>= 2.1'
39
40
  end
@@ -52,7 +52,7 @@ module RobustExcelOle
52
52
  ole_table = worksheet.ListObjects.Item(1)
53
53
  table = Table.new(ole_table)
54
54
  table.Name.should == "table3"
55
- table.HeaderRowRange.Value.first.should == ["Number","Person","Amount","Time","Date"]
55
+ table.HeaderRowRange.Value.first.should == ["Number","Person","Amount","Time","Price"]
56
56
  table.ListRows.Count.should == 6
57
57
  worksheet[3,4].Value.should == "Number"
58
58
  end
@@ -151,13 +151,11 @@ module RobustExcelOle
151
151
  end
152
152
 
153
153
  it "should do methods for sheet" do
154
- # ((@ole_sheet_methods + @sheet_methods) - @book1.sheet(1).methods).should be_empty
155
- (Object.instance_methods.select{|m| m =~ /^(?!\_)/} - @book1.sheet(1).methods).sort.should be_empty
154
+ ((@ole_sheet_methods + @sheet_methods) - @book1.sheet(1).methods).should be_empty
156
155
  end
157
156
 
158
157
  it "should do own_methods with popular ole_excel and excel methods" do
159
- # ((@ole_sheet_methods + @sheet_methods) - @book1.sheet(1).own_methods).should == [] #be_empty
160
- (Object.instance_methods - @book1.sheet(1).own_methods).should == Object.instance_methods
158
+ ((@ole_sheet_methods + @sheet_methods) - @book1.sheet(1).own_methods).should == [] #be_empty
161
159
  end
162
160
 
163
161
  it "should respond to popular sheet methods" do
@@ -223,10 +221,12 @@ module RobustExcelOle
223
221
  canonize("this../.i.s/.../..the/..../pa.th/").should == "this../.i.s/.../..the/..../pa.th"
224
222
  end
225
223
 
224
+ =begin
226
225
  it "should downcase" do
227
226
  canonize("/This/IS/tHe/path").should == "/this/is/the/path"
228
227
  canonize("///THIS/.///./////iS//the/../PatH/////").should == "/this/is/path"
229
228
  end
229
+ =end
230
230
 
231
231
  it "should raise an error for no strings" do
232
232
  expect{
@@ -234,14 +234,13 @@ module RobustExcelOle
234
234
  }.to raise_error(TypeREOError, "No string given to canonize, but 1")
235
235
  end
236
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
237
+ it "should yield the network path" do
238
+ General.canonize(@hostname_share_path).should == @network_path
239
+ General.canonize(@network_path).should == @network_path
240
+ General.canonize(@simple_file).should == @simple_file
241
+ General.canonize(@simple_file_extern).should == @simple_file_extern
242
242
  end
243
243
 
244
-
245
244
  end
246
245
  end
247
246
 
@@ -41,6 +41,13 @@ describe ListObject do
41
41
  @sheet[1,1].Value.should == "Person"
42
42
  end
43
43
 
44
+ it "should do the idempotence" do
45
+ ole_table = @sheet.ListObjects.Item(1)
46
+ table = Table.new(ole_table)
47
+ table2 = Table.new(table)
48
+ table2.ole_table.should be_a WIN32OLE
49
+ end
50
+
44
51
  it "should type-lift a Win32ole list object into a RobustExcelOle list object" do
45
52
  ole_table = @sheet.ListObjects.Item(1)
46
53
  table = Table.new(ole_table)
@@ -103,19 +110,25 @@ describe ListObject do
103
110
  context "with new table" do
104
111
 
105
112
  before do
106
- @table = Table.new(@sheet, "table_name", [1,1], 3, ["Person","Amount"])
113
+ @table = Table.new(@sheet, "table_name", [1,1], 3, ["Person","Amo%untSales"])
107
114
  @table_row1 = @table[1]
108
115
  end
109
116
 
110
- it "should set and read values" do
117
+ it "should read and set values via alternative column names" do
111
118
  @table_row1.person.should be nil
112
119
  @table_row1.person = "John"
113
120
  @table_row1.person.should == "John"
114
121
  @sheet[2,1].Value.should == "John"
115
- @table_row1.amount.should be nil
116
- @table_row1.amount = 42
117
- @table_row1.amount.should == 42
122
+ @table_row1.amount_sales.should be nil
123
+ @table_row1.amount_sales = 42
124
+ @table_row1.amount_sales.should == 42
118
125
  @sheet[2,2].Value.should == 42
126
+ @table_row1.Person = "Herbert"
127
+ @table_row1.Person.should == "Herbert"
128
+ @sheet[2,1].Value.should == "Herbert"
129
+ @table_row1.AmountSales = 80
130
+ @table_row1.AmountSales.should == 80
131
+ @sheet[2,2].Value.should == 80
119
132
  end
120
133
 
121
134
  end
@@ -177,9 +177,9 @@ describe RobustExcelOle::Range do
177
177
  end
178
178
  end
179
179
 
180
- describe "#v" do
180
+ describe "#value" do
181
181
 
182
- context "v, v=" do
182
+ context "value, value=" do
183
183
 
184
184
  before do
185
185
  @sheet1 = @book.sheet(1)
@@ -207,6 +207,12 @@ describe RobustExcelOle::Range do
207
207
  @sheet1.range([1..2,3..5]).v = [[1,2,3],[4,5,6]]
208
208
  @sheet1.range([1..2,3..5]).v.should == [[1,2,3],[4,5,6]]
209
209
  end
210
+
211
+ it "should color the range" do
212
+ @sheet1.range([1..2,3..5]).set_value([[1,2,3],[4,5,6]],:color => 42)
213
+ @sheet1.range([1..2,3..5]).Interior.ColorIndex.should == 42
214
+ end
215
+
210
216
  end
211
217
  end
212
218
 
@@ -763,7 +763,7 @@ describe Workbook do
763
763
  @book1.Names.Item("new").RefersToRange.Interior.ColorIndex.should == 4
764
764
  @book1["new"].should == "bar"
765
765
  @book1["new"] = "bar"
766
- @book1.Names.Item("new").RefersToRange.Interior.ColorIndex.should == 42
766
+ @book1.Names.Item("new").RefersToRange.Interior.ColorIndex.should == 4
767
767
  @book1.save
768
768
  @book1.close
769
769
  #book2 = Workbook.open(@simple_file1, :visible => true)
@@ -777,7 +777,7 @@ describe Workbook do
777
777
  @book1.Names.Item("new").RefersToRange.Interior.ColorIndex.should == 4
778
778
  @book1["new"].should == "bar"
779
779
  @book1["new"] = "bar"
780
- @book1.Names.Item("new").RefersToRange.Interior.ColorIndex.should == 42
780
+ @book1.Names.Item("new").RefersToRange.Interior.ColorIndex.should == 4
781
781
  @book1.save
782
782
  @book1.close
783
783
  end
@@ -1096,6 +1096,16 @@ describe Workbook do
1096
1096
  range.Address.should == "$A$1:$D$3"
1097
1097
  end
1098
1098
 
1099
+ it "should raise error when given integer range" do
1100
+ expect{
1101
+ @book1.range([1..2,3..4])
1102
+ }.to raise_error(RangeNotCreated, /argument/)
1103
+ end
1104
+
1105
+ it "should accept an integer range when given a worksheet" do
1106
+ @book1.range(@book1.sheet(1),[4..5,1..2]).Address.should == "$A$4:$B$5"
1107
+ end
1108
+
1099
1109
  end
1100
1110
 
1101
1111
  context "adding and deleting the name of a range" do
@@ -19,6 +19,7 @@ describe Worksheet do
19
19
  before do
20
20
  @dir = create_tmpdir
21
21
  @simple_file = @dir + '/workbook.xls'
22
+ @another_workbook = @dir + '/another_workbook.xls'
22
23
  @protected_file = @dir + '/protected_sheet.xls'
23
24
  @blank_file = @dir + '/book_with_blank.xls'
24
25
  @merge_file = @dir + '/merge_cells.xls'
@@ -207,11 +208,13 @@ describe Worksheet do
207
208
  describe "range" do
208
209
 
209
210
  it "should a range with relative r1c1-reference" do
211
+ @sheet.range([1,1]).Select
210
212
  @sheet.range(["Z1S[3]:Z[2]S8"]).Address.should == "$D$1:$H$3"
211
213
  @sheet.range(["Z1S3:Z2S8"]).Address.should == "$C$1:$H$2"
212
214
  end
213
215
 
214
216
  it "should a range with relative integer-range-reference" do
217
+ @sheet.range([1,1]).Select
215
218
  @sheet.range([1..[2],[3]..8]).Address.should == "$D$1:$H$3"
216
219
  end
217
220