robust_excel_ole 1.19.13 → 1.22.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.
@@ -8,14 +8,26 @@ module RobustExcelOle
8
8
  # See https://docs.microsoft.com/en-us/office/vba/api/excel.worksheet#methods
9
9
 
10
10
  class Range < VbaObjects
11
+
11
12
  include Enumerable
13
+
12
14
  attr_reader :ole_range
13
15
  attr_reader :worksheet
14
16
 
17
+ alias ole_object ole_range
18
+
19
+
15
20
  def initialize(win32_range, worksheet = nil)
16
21
  @ole_range = win32_range
17
- @worksheet = worksheet ? worksheet : worksheet_class.new(self.Parent)
18
- #@worksheet = worksheet_class.new(self.Parent)
22
+ @worksheet = worksheet ? worksheet.to_reo : worksheet_class.new(self.Parent)
23
+ end
24
+
25
+ def rows
26
+ @rows ||= (1..@ole_range.Rows.Count)
27
+ end
28
+
29
+ def columns
30
+ @columns ||= (1..@ole_range.Columns.Count)
19
31
  end
20
32
 
21
33
  def each
@@ -43,7 +55,6 @@ module RobustExcelOle
43
55
  # @params [Range] a range
44
56
  # @returns [Array] the values
45
57
  def values(range = nil)
46
- #result = map { |x| x.Value }.flatten
47
58
  result_unflatten = if !::RANGES_JRUBY_BUG
48
59
  map { |x| x.v }
49
60
  else
@@ -64,12 +75,10 @@ module RobustExcelOle
64
75
  if !::RANGES_JRUBY_BUG
65
76
  self.Value
66
77
  else
67
- address_r1c1 = self.AddressLocal(true,true,XlR1C1)
68
- row, col = address_tool.as_integer_ranges(address_r1c1)
69
78
  values = []
70
- row.each do |r|
79
+ rows.each do |r|
71
80
  values_col = []
72
- col.each{ |c| values_col << worksheet.Cells(r,c).Value}
81
+ columns.each{ |c| values_col << worksheet.Cells(r,c).Value}
73
82
  values << values_col
74
83
  end
75
84
  values
@@ -85,10 +94,8 @@ module RobustExcelOle
85
94
  if !::RANGES_JRUBY_BUG
86
95
  ole_range.Value = value
87
96
  else
88
- address_r1c1 = ole_range.AddressLocal(true,true,XlR1C1)
89
- row, col = address_tool.as_integer_ranges(address_r1c1)
90
- row.each_with_index do |r,i|
91
- col.each_with_index do |c,j|
97
+ rows.each_with_index do |r,i|
98
+ columns.each_with_index do |c,j|
92
99
  ole_range.Cells(i+1,j+1).Value = (value.respond_to?(:first) ? value[i][j] : value)
93
100
  end
94
101
  end
@@ -106,128 +113,58 @@ module RobustExcelOle
106
113
  # @params [Address or Address-Array] address or upper left position of the destination range
107
114
  # @options [Worksheet] the destination worksheet
108
115
  # @options [Hash] options: :transpose, :values_only
109
- def copy(dest_address1, sheet_or_dest_address2 = :__not_provided, options_or_sheet = :__not_provided, not_provided_or_options = :__not_provided)
110
- dest_address = if sheet_or_dest_address2.is_a?(Object::Range) or sheet_or_dest_address2.is_a?(Integer)
111
- [dest_address1,sheet_or_dest_address2]
112
- else
113
- dest_address1
114
- end
115
- dest_sheet = if sheet_or_dest_address2.is_a?(Worksheet)
116
- sheet_or_dest_address2
117
- else
118
- if options_or_sheet.is_a?(Worksheet)
119
- options_or_sheet
120
- else
121
- @worksheet
122
- end
123
- end
124
- options = if options_or_sheet.is_a?(Hash)
125
- options_or_sheet
126
- else
127
- if not_provided_or_options.is_a?(Hash)
128
- not_provided_or_options
129
- else
130
- { }
116
+ def copy(dest_address, *remaining_args)
117
+ dest_sheet = @worksheet
118
+ options = { }
119
+ remaining_args.each do |arg|
120
+ case arg
121
+ when Object::Range, Integer then dest_address = [dest_address,arg]
122
+ when Worksheet, WIN32OLE then dest_sheet = arg.to_reo
123
+ when Hash then options = arg
124
+ else raise RangeNotCopied, "cannot copy range: argument error: #{remaining_args.inspect}"
131
125
  end
132
126
  end
133
- rows, columns = address_tool.as_integer_ranges(dest_address)
134
- dest_address_is_position = (rows.min == rows.max && columns.min == columns.max)
135
- dest_range_address = if (not dest_address_is_position)
127
+ begin
128
+ rows, columns = address_tool.as_integer_ranges(dest_address)
129
+ dest_address_is_position = (rows.min == rows.max && columns.min == columns.max)
130
+ dest_range_address = if (not dest_address_is_position)
136
131
  [rows.min..rows.max,columns.min..columns.max]
137
132
  else
138
133
  if (not options[:transpose])
139
- [rows.min..rows.min+self.Rows.Count-1,
140
- columns.min..columns.min+self.Columns.Count-1]
134
+ [rows.min..rows.min+self.Rows.Count-1, columns.min..columns.min+self.Columns.Count-1]
141
135
  else
142
- [rows.min..rows.min+self.Columns.Count-1,
143
- columns.min..columns.min+self.Rows.Count-1]
136
+ [rows.min..rows.min+self.Columns.Count-1, columns.min..columns.min+self.Rows.Count-1]
144
137
  end
145
138
  end
146
- dest_range = dest_sheet.range(dest_range_address)
147
- begin
139
+ dest_range = dest_sheet.range(dest_range_address)
148
140
  if options[:values_only]
149
- # dest_range.Value = options[:transpose] ? self.Value.transpose : self.Value
150
141
  dest_range.v = options[:transpose] ? self.v.transpose : self.v
151
142
  else
152
143
  if dest_range.worksheet.workbook.excel == @worksheet.workbook.excel
153
144
  if options[:transpose]
154
145
  self.Copy
155
- #dest_range.PasteSpecial('transpose' => true)
156
146
  dest_range.PasteSpecial(XlPasteAll,XlPasteSpecialOperationNone,false,true)
157
147
  else
158
- #self.Copy('destination' => dest_range.ole_range)
159
148
  self.Copy(dest_range.ole_range)
160
149
  end
161
150
  else
162
151
  if options[:transpose]
163
152
  added_sheet = @worksheet.workbook.add_sheet
164
- self.copy_special(dest_address, added_sheet, :transpose => true)
165
- added_sheet.range(dest_range_address).copy_special(dest_address,dest_sheet)
153
+ self.copy(dest_address, added_sheet, :transpose => true)
154
+ added_sheet.range(dest_range_address).copy(dest_address,dest_sheet)
166
155
  @worksheet.workbook.excel.with_displayalerts(false) {added_sheet.Delete}
167
156
  else
168
157
  self.Copy
169
- #dest_sheet.Paste('destination' => dest_range.ole_range)
170
158
  dest_sheet.Paste(dest_range.ole_range)
171
159
  end
172
160
  end
173
161
  end
162
+ dest_range
174
163
  rescue WIN32OLERuntimeError, Java::OrgRacobCom::ComFailException => msg
175
164
  raise RangeNotCopied, 'cannot copy range'
176
165
  end
177
166
  end
178
167
 
179
- # becomes copy
180
- # copies a range
181
- # @params [Address or Address-Array] address or upper left position of the destination range
182
- # @options [Worksheet] the destination worksheet
183
- # @options [Hash] options: :transpose, :values_only
184
- def copy_special(dest_address, dest_sheet = :__not_provided, options = { })
185
- rows, columns = address_tool.as_integer_ranges(dest_address)
186
- dest_sheet = @worksheet if dest_sheet == :__not_provided
187
- dest_address_is_position = (rows.min == rows.max && columns.min == columns.max)
188
- dest_range_address = if (not dest_address_is_position)
189
- [rows.min..rows.max,columns.min..columns.max]
190
- else
191
- if (not options[:transpose])
192
- [rows.min..rows.min+self.Rows.Count-1,
193
- columns.min..columns.min+self.Columns.Count-1]
194
- else
195
- [rows.min..rows.min+self.Columns.Count-1,
196
- columns.min..columns.min+self.Rows.Count-1]
197
- end
198
- end
199
- dest_range = dest_sheet.range(dest_range_address)
200
- begin
201
- if options[:values_only]
202
- dest_range.Value = options[:transpose] ? self.Value.transpose : self.Value
203
- else
204
- if dest_range.worksheet.workbook.excel == @worksheet.workbook.excel
205
- if options[:transpose]
206
- self.Copy
207
- #dest_range.PasteSpecial('transpose' => true)
208
- dest_range.PasteSpecial(XlPasteAll,XlPasteSpecialOperationNone,false,true)
209
- else
210
- #self.Copy('destination' => dest_range.ole_range)
211
- self.Copy(dest_range.ole_range)
212
- end
213
- else
214
- if options[:transpose]
215
- added_sheet = @worksheet.workbook.add_sheet
216
- self.copy_special(dest_address, added_sheet, :transpose => true)
217
- added_sheet.range(dest_range_address).copy_special(dest_address,dest_sheet)
218
- @worksheet.workbook.excel.with_displayalerts(false) {added_sheet.Delete}
219
- else
220
- self.Copy
221
- #dest_sheet.Paste('destination' => dest_range.ole_range)
222
- dest_sheet.Paste(dest_range.ole_range)
223
- end
224
- end
225
- end
226
- rescue WIN32OLERuntimeError, Java::OrgRacobCom::ComFailException => msg
227
- raise RangeNotCopied, 'cannot copy range'
228
- end
229
- end
230
-
231
168
  def == other_range
232
169
  other_range.is_a?(Range) &&
233
170
  self.worksheet == other_range.worksheet
@@ -239,6 +176,27 @@ module RobustExcelOle
239
176
  @worksheet.workbook.excel
240
177
  end
241
178
 
179
+ # @private
180
+ # returns true, if the Range object responds to VBA methods, false otherwise
181
+ def alive?
182
+ @ole_range.Row
183
+ true
184
+ rescue
185
+ # trace $!.message
186
+ false
187
+ end
188
+
189
+ # @private
190
+ def to_s
191
+ "#<REO::Range: " + "#{@ole_range.Address('External' => true).gsub(/\$/,'')} " + ">"
192
+ # "#<REO::Range: " + "#{@ole_range.Address.gsub(/\$/,'')} " + "#{worksheet.Name} " + ">"
193
+ end
194
+
195
+ # @private
196
+ def inspect
197
+ to_s # [0..-2] + "#{worksheet.workbook.Name} " + ">"
198
+ end
199
+
242
200
  # @private
243
201
  def self.worksheet_class
244
202
  @worksheet_class ||= begin
@@ -254,6 +212,8 @@ module RobustExcelOle
254
212
  self.class.worksheet_class
255
213
  end
256
214
 
215
+ include MethodHelpers
216
+
257
217
  private
258
218
 
259
219
  def method_missing(name, *args)
@@ -1,3 +1,3 @@
1
1
  module RobustExcelOle
2
- VERSION = "1.19.13"
2
+ VERSION = "1.22.2"
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
@@ -820,11 +823,12 @@ module RobustExcelOle
820
823
  opts = sheet
821
824
  sheet = nil
822
825
  end
823
- new_sheet_name = opts.delete(:as)
824
- last_sheet_local = last_sheet
825
- after_or_before, base_sheet = opts.to_a.first || [:after, last_sheet_local]
826
- base_sheet_ole = base_sheet.ole_worksheet
827
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
828
832
  if !::COPYSHEETS_JRUBY_BUG
829
833
  add_or_copy_sheet_simple(sheet, { after_or_before.to_s => base_sheet_ole })
830
834
  else
@@ -840,7 +844,7 @@ module RobustExcelOle
840
844
  end
841
845
  end
842
846
  end
843
- rescue WIN32OLERuntimeError, NameNotFound, Java::OrgRacobCom::ComFailException
847
+ rescue # WIN32OLERuntimeError, NameNotFound, Java::OrgRacobCom::ComFailException
844
848
  raise WorksheetREOError, "could not add given worksheet #{sheet.inspect}"
845
849
  end
846
850
  new_sheet = worksheet_class.new(ole_workbook.Activesheet)
@@ -889,7 +893,7 @@ module RobustExcelOle
889
893
  # @param [String] name the name of the range
890
894
  # @param [Variant] value the contents of the range
891
895
  def []= (name, value)
892
- set_namevalue_glob(name, value, :color => 42)
896
+ set_namevalue_glob(name, value)
893
897
  end
894
898
 
895
899
  # sets options
@@ -918,7 +922,7 @@ module RobustExcelOle
918
922
 
919
923
  # returns the full file name of the workbook
920
924
  def filename
921
- @ole_workbook.Fullname.tr('\\','/') rescue nil
925
+ General.canonize(@ole_workbook.Fullname.tr('\\','/')) rescue nil
922
926
  end
923
927
 
924
928
  # @private
@@ -1001,7 +1005,7 @@ module RobustExcelOle
1001
1005
 
1002
1006
  # @private
1003
1007
  def inspect
1004
- '#<Workbook: ' + ('not alive ' unless alive?).to_s + (File.basename(self.filename) if alive?).to_s + " #{@ole_workbook} #{@excel}" + '>'
1008
+ '#<Workbook: ' + ('not alive ' unless alive?).to_s + (File.basename(self.filename) if alive?).to_s + " #{@excel}" + '>'
1005
1009
  end
1006
1010
 
1007
1011
  # @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
@@ -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)
@@ -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
@@ -246,9 +258,11 @@ module RobustExcelOle
246
258
 
247
259
  # @private
248
260
  def inspect
249
- self.to_s
261
+ self.to_s[0..-2] + "#{workbook.Name} " + ">"
250
262
  end
251
263
 
264
+ include MethodHelpers
265
+
252
266
  private
253
267
 
254
268
  def method_missing(name, *args)
data/reo.bat ADDED
@@ -0,0 +1,3 @@
1
+ @echo off
2
+
3
+ ruby lib/reo_console.rb
@@ -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
@@ -48,7 +52,7 @@ module RobustExcelOle
48
52
  ole_table = worksheet.ListObjects.Item(1)
49
53
  table = Table.new(ole_table)
50
54
  table.Name.should == "table3"
51
- table.HeaderRowRange.Value.first.should == ["Number","Person","Amount","Time","Date"]
55
+ table.HeaderRowRange.Value.first.should == ["Number","Person","Amount","Time","Price"]
52
56
  table.ListRows.Count.should == 6
53
57
  worksheet[3,4].Value.should == "Number"
54
58
  end
@@ -147,13 +151,11 @@ module RobustExcelOle
147
151
  end
148
152
 
149
153
  it "should do methods for sheet" do
150
- # ((@ole_sheet_methods + @sheet_methods) - @book1.sheet(1).methods).should be_empty
151
- (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
152
155
  end
153
156
 
154
157
  it "should do own_methods with popular ole_excel and excel methods" do
155
- # ((@ole_sheet_methods + @sheet_methods) - @book1.sheet(1).own_methods).should == [] #be_empty
156
- (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
157
159
  end
158
160
 
159
161
  it "should respond to popular sheet methods" do
@@ -186,7 +188,7 @@ module RobustExcelOle
186
188
  filename = 'C:/Dokumente und Einstellungen/Zauberthomas/Eigene Dateien/robust_excel_ole/spec/book_spec.rb'
187
189
  absolute_path(filename).gsub("\\","/").should == filename
188
190
  end
189
- end
191
+ end
190
192
  end
191
193
 
192
194
  describe "canonize" do
@@ -219,10 +221,12 @@ module RobustExcelOle
219
221
  canonize("this../.i.s/.../..the/..../pa.th/").should == "this../.i.s/.../..the/..../pa.th"
220
222
  end
221
223
 
224
+ =begin
222
225
  it "should downcase" do
223
226
  canonize("/This/IS/tHe/path").should == "/this/is/the/path"
224
227
  canonize("///THIS/.///./////iS//the/../PatH/////").should == "/this/is/path"
225
228
  end
229
+ =end
226
230
 
227
231
  it "should raise an error for no strings" do
228
232
  expect{
@@ -230,6 +234,13 @@ 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 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
+ end
243
+
233
244
  end
234
245
  end
235
246
 
@@ -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