robust_excel_ole 1.35 → 1.36

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.
@@ -68,22 +68,23 @@ module RobustExcelOle
68
68
  # :visible true, false, or nil (default)
69
69
  # alternatives: :default_excel, :force_excel, :visible, :d, :f, :e, :v
70
70
  # :if_unsaved if an unsaved workbook with the same name is open, then
71
- # :raise -> raises an exception
72
- # :forget -> close the unsaved workbook, open the new workbook
73
- # :accept -> lets the unsaved workbook open
74
- # :alert or :excel -> gives control to Excel
75
- # :new_excel -> opens the new workbook in a new Excel instance
71
+ # :raise -> raise an exception
72
+ # :forget -> close the unsaved workbook, re-open the workbook
73
+ # :accept -> let the unsaved workbook open
74
+ # :alert or :excel -> give control to Excel
75
+ # :new_excel -> open the workbook in a new Excel instance
76
76
  # :if_obstructed if a workbook with the same name in a different path is open, then
77
- # or :raise -> raises an exception
78
- # :if_blocked :forget -> closes the old workbook, open the new workbook
79
- # :save -> saves the old workbook, close it, open the new workbook
80
- # :close_if_saved -> closes the old workbook and open the new workbook, if the old workbook is saved,
81
- # otherwise raises an exception.
82
- # :new_excel -> opens the new workbook in a new Excel instance
83
- # :if_absent :raise -> raises an exception , if the file does not exists
84
- # :create -> creates a new Excel file, if it does not exists
85
- # :read_only true -> opens in read-only mode
86
- # :visible true -> makes the workbook visible
77
+ # or :raise -> raise an exception
78
+ # :if_blocked :forget -> close the workbook, re-open the workbook
79
+ # :accept -> let the blocked workbook open
80
+ # :save -> save the blocked workbook, close it, re-open the workbook
81
+ # :close_if_saved -> close the blocked workbook and re-open the workbook, if the blocked workbook is saved,
82
+ # otherwise raise an exception.
83
+ # :new_excel -> open the workbook in a new Excel instance
84
+ # :if_absent :raise -> raise an exception , if the file does not exists
85
+ # :create -> create a new Excel file, if it does not exists
86
+ # :read_only true -> open in read-only mode
87
+ # :visible true -> make the workbook visible
87
88
  # :check_compatibility true -> check compatibility when saving
88
89
  # :update_links true -> user is being asked how to update links, false -> links are never updated
89
90
  # @return [Workbook] a representation of a workbook
@@ -259,8 +260,9 @@ module RobustExcelOle
259
260
  @ole_workbook = workbooks.Item(File.basename(filename)) rescue nil if @ole_workbook.nil?
260
261
  if @ole_workbook && alive?
261
262
  set_was_open options, true
263
+ #open_or_create_workbook(filename,options) if (!options[:read_only].nil?) && options[:read_only]
264
+ manage_changing_readonly_mode(options) if (!options[:read_only].nil?) && options[:read_only] != @ole_workbook.ReadOnly
262
265
  manage_blocking_or_unsaved_workbook(filename,options)
263
- open_or_create_workbook(filename,options) if (!options[:read_only].nil?) && options[:read_only] != @ole_workbook.ReadOnly
264
266
  else
265
267
  if (excel_option.nil? || excel_option == :current) &&
266
268
  !(::CONNECT_JRUBY_BUG && filename[0] == '/')
@@ -277,8 +279,8 @@ module RobustExcelOle
277
279
  def apply_options(filename, options)
278
280
  # changing read-only mode
279
281
  if (!options[:read_only].nil?) && options[:read_only] != @ole_workbook.ReadOnly
280
- ensure_workbook(filename, options)
281
- raise WorkbookReadOnly, "could not change read-only mode" if options[:read_only] != @ole_workbook.ReadOnly
282
+ # ensure_workbook(filename, options)
283
+ manage_changing_readonly_mode(options)
282
284
  end
283
285
  retain_saved do
284
286
  self.visible = options[:force][:visible].nil? ? @excel.Visible : options[:force][:visible]
@@ -312,6 +314,45 @@ module RobustExcelOle
312
314
  @excel = excel_class.new(ole_excel)
313
315
  end
314
316
 
317
+ def manage_changing_readonly_mode(options)
318
+ if !ole_workbook.Saved && options[:read_only]
319
+ manage_unsaved_workbook_when_changing_readonly_mode(options)
320
+ end
321
+ change_readonly_mode(options)
322
+ end
323
+
324
+ def change_readonly_mode(options)
325
+ read_write_value = options[:read_only] ? RobustExcelOle::XlReadOnly : RobustExcelOle::XlReadWrite
326
+ give_control_to_excel = !ole_workbook.Saved && options[:read_only] &&
327
+ (options[:if_unsaved] == :excel || options[:if_unsaved] == :alert)
328
+ displayalerts = give_control_to_excel ? true : @excel.Displayalerts
329
+ @excel.with_displayalerts(displayalerts) {
330
+ @ole_workbook.ChangeFileAccess('Mode' => read_write_value)
331
+ }
332
+ end
333
+
334
+ def manage_unsaved_workbook_when_changing_readonly_mode(options)
335
+ case options[:if_unsaved]
336
+ when :raise
337
+ if options[:read_only]
338
+ raise WorkbookNotSaved, "workbook is not saved" +
339
+ "\nHint: Use the option :if_unsaved with values :forget or :save,
340
+ to allow changing to ReadOnly mode (with discarding or saving the workbook before, respectively),
341
+ or option :excel to give control to Excel."
342
+ end
343
+ when :save
344
+ save
345
+ when :forget
346
+ @ole_workbook.Saved = true
347
+ when :alert, :excel
348
+ # displayalerts = true
349
+ # nothing
350
+ else
351
+ raise OptionInvalid, ":if_unsaved: invalid option: #{options[:if_unsaved].inspect}" +
352
+ "\nHint: Valid values are :raise, :forget, :save, :excel"
353
+ end
354
+ end
355
+
315
356
  def manage_nonexisting_file(filename, options)
316
357
  return if File.exist?(filename)
317
358
  abs_filename = General.absolute_path(filename)
@@ -354,10 +395,12 @@ module RobustExcelOle
354
395
  raise WorkbookBlocked, "can't open workbook #{filename},
355
396
  because it is being blocked by #{blocked_filename.call} with the same name in a different path." +
356
397
  "\nHint: Use the option :if_blocked with values :forget or :save,
357
- to allow automatic closing of the old workbook (without or with saving before, respectively),
358
- before the new workbook is being opened."
398
+ to allow automatic closing of the blocking workbook (without or with saving before, respectively),
399
+ before reopening the workbook."
359
400
  when :forget
360
401
  manage_forgetting_workbook(filename, options)
402
+ when :accept
403
+ # do nothing
361
404
  when :save
362
405
  manage_saving_workbook(filename, options)
363
406
  when :close_if_saved
@@ -378,12 +421,7 @@ module RobustExcelOle
378
421
  def manage_unsaved_workbook(filename, options)
379
422
  case options[:if_unsaved]
380
423
  when :raise
381
- msg = if !options[:read_only].nil? && @ole_workbook.ReadOnly != options[:read_only]
382
- "cannot change read-only mode of the workbook #{File.basename(filename).inspect}, because it contains unsaved changes"
383
- else
384
- "workbook is already open but not saved: #{File.basename(filename).inspect}"
385
- end
386
- raise WorkbookNotSaved, msg +
424
+ raise WorkbookNotSaved, "workbook is already open but not saved: #{File.basename(filename).inspect}" +
387
425
  "\nHint: Use the option :if_unsaved with values :forget to close the unsaved workbook,
388
426
  :accept to let it open, or :save to save it, respectivly"
389
427
  when :forget
@@ -410,7 +448,7 @@ module RobustExcelOle
410
448
 
411
449
  def manage_saving_workbook(filename, options)
412
450
  save unless @ole_workbook.Saved
413
- manage_forgetting_workbook(filename, options)
451
+ manage_forgetting_workbook(filename, options) if options[:if_obstructed] == :save
414
452
  end
415
453
 
416
454
  def manage_new_excel(filename, options)
@@ -507,11 +545,11 @@ module RobustExcelOle
507
545
  # @option opts [Symbol] :if_unsaved :raise (default), :save, :forget, :keep_open, or :alert
508
546
  # options:
509
547
  # :if_unsaved if the workbook is unsaved
510
- # :raise -> raises an exception
511
- # :save -> saves the workbook before it is closed
512
- # :forget -> closes the workbook
548
+ # :raise -> raise an exception
549
+ # :save -> save the workbook before it is closed
550
+ # :forget -> close the workbook
513
551
  # :keep_open -> keep the workbook open
514
- # :alert or :excel -> gives control to excel
552
+ # :alert or :excel -> give control to excel
515
553
  # @raise WorkbookNotSaved if the option :if_unsaved is :raise and the workbook is unsaved
516
554
  # @raise OptionInvalid if the options is invalid
517
555
  def close(opts = {if_unsaved: :raise})
@@ -622,7 +660,10 @@ module RobustExcelOle
622
660
  was_saved = book.saved
623
661
  was_check_compatibility = book.check_compatibility
624
662
  was_calculation = book.excel.properties[:calculation]
625
- opts[:read_only] = !opts[:writable] unless (!opts[:read_only].nil? || opts[:writable].nil? || open_opts[:was_open])
663
+ #opts[:read_only] = !opts[:writable] unless (!opts[:read_only].nil? || opts[:writable].nil? || open_opts[:was_open])
664
+ if (opts[:read_only].nil? && !opts[:writable].nil? && !open_opts[:was_open] && (was_saved || opts[:if_unsaved]==:save))
665
+ opts[:read_only] = !opts[:writable]
666
+ end
626
667
  book.send :apply_options, file, opts
627
668
  yield book
628
669
  ensure
@@ -649,12 +690,14 @@ module RobustExcelOle
649
690
 
650
691
  # reopens a closed workbook
651
692
  # @options options
652
- def reopen(options = { })
693
+ def open(options = { })
653
694
  book = self.class.open(@stored_filename, options)
654
695
  raise WorkbookREOError("cannot reopen workbook\n#{$!.message}") unless book && book.alive?
655
696
  book
656
697
  end
657
698
 
699
+ alias reopen open # :deprecated: #
700
+
658
701
  # simple save of a workbook.
659
702
  # @return [Boolean] true, if successfully saved, nil otherwise
660
703
  def save(opts = { }) # option opts is deprecated #
@@ -683,10 +726,10 @@ module RobustExcelOle
683
726
  # :overwrite -> writes the file, delete the old file
684
727
  # :alert or :excel -> gives control to Excel
685
728
  # :if_obstructed if a workbook with the same name and different path is already open and blocks the saving, then
686
- # or :raise -> raises an exception
687
- # :if_blocked :forget -> closes the blocking workbook
688
- # :save -> saves the blocking workbook and closes it
689
- # :close_if_saved -> closes the blocking workbook, if it is saved,
729
+ # or :raise -> raise an exception
730
+ # :if_blocked :forget -> close the blocking workbook
731
+ # :save -> save the blocking workbook and close it
732
+ # :close_if_saved -> close the blocking workbook, if it is saved,
690
733
  # otherwise raises an exception
691
734
  # @return [Workbook], the book itself, if successfully saved, raises an exception otherwise
692
735
  def save_as(file, options = { })
@@ -900,12 +943,12 @@ module RobustExcelOle
900
943
  public
901
944
 
902
945
  # for compatibility to older versions
903
- def add_sheet(sheet = nil, opts = { })
946
+ def add_sheet(sheet = nil, opts = { }) # :deprecated: #
904
947
  add_or_copy_sheet(sheet, opts)
905
948
  end
906
949
 
907
- # for compatibility to older versions
908
- def copy_sheet(sheet, opts = { })
950
+ # for compatibility to older versions
951
+ def copy_sheet(sheet, opts = { }) # :deprecated: #
909
952
  add_or_copy_sheet(sheet, opts)
910
953
  end
911
954
 
@@ -975,14 +1018,36 @@ module RobustExcelOle
975
1018
 
976
1019
  # returns the full file name of the workbook
977
1020
  def filename
978
- General.canonize(@ole_workbook.Fullname.tr('\\','/')) rescue nil
1021
+ @filename ||= General.canonize(@ole_workbook.Fullname.tr('\\','/')) rescue nil
979
1022
  end
980
1023
 
981
- # @private
1024
+ # @returns true, if the workbook is not in read-only mode
982
1025
  def writable
983
1026
  !@ole_workbook.ReadOnly if @ole_workbook
984
1027
  end
985
1028
 
1029
+ # sets the writable mode
1030
+ # @param [Bool] writable mode (true: read-write-mode, false: read-only mode)
1031
+ # @options [Symbol] :if_unsaved if the workbook is unsaved, then
1032
+ # :raise -> raise an exception (default)
1033
+ # :forget -> close the unsaved workbook, re-open the workbook
1034
+ # :accept -> let the unsaved workbook open
1035
+ # :alert or :excel -> give control to Excel
1036
+ def writable=(value_and_opts)
1037
+ writable_value, unsaved_opts = *value_and_opts
1038
+ if @ole_workbook && !value_and_opts.nil?
1039
+ options = {:if_unsaved => :raise}
1040
+ options = options.merge(unsaved_opts) if unsaved_opts
1041
+ options = {:read_only => !writable_value}.merge(options)
1042
+ if options[:read_only] != @ole_workbook.ReadOnly
1043
+ manage_changing_readonly_mode(options)
1044
+ manage_unsaved_workbook(filename,options) if !@ole_workbook.Saved && unsaved_opts
1045
+ end
1046
+ end
1047
+ writable_value
1048
+ end
1049
+
1050
+
986
1051
  # @private
987
1052
  def saved
988
1053
  @ole_workbook.Saved if @ole_workbook
@@ -119,7 +119,7 @@ module RobustExcelOle
119
119
  end
120
120
  rescue
121
121
  address2_string = (address2.nil? || address2 == :__not_provided) ? "" : ", #{address2.inspect}"
122
- raise RangeNotCreated, "cannot find name or address #{name_or_address.inspect}#{address2_string})"
122
+ raise RangeNotCreated, "cannot find name or address #{name_or_address.inspect}#{address2_string}"
123
123
  end
124
124
  end
125
125
  end
@@ -33,7 +33,6 @@ Gem::Specification.new do |s|
33
33
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
34
34
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
35
35
  s.require_paths = ["lib"]
36
- s.add_runtime_dependency 'win32api', '~> 0.1'
37
36
  s.add_runtime_dependency 'pry', '~> 0.12', '>= 0.12.1'
38
37
  s.add_runtime_dependency 'pry-bond', '~> 0.0', '>= 0.0.1'
39
38
  s.add_development_dependency 'rspec', '~> 2.6', '>= 2.6.0'
@@ -60,8 +60,7 @@ describe Bookstore do
60
60
  @network_path = "N:/data/workbook.xls"
61
61
  network = WIN32OLE.new('WScript.Network')
62
62
  computer_name = network.ComputerName
63
- #@hostname_share_path = "DESKTOP-A3C5CJ6/spec/data/workbook.xls"
64
- @hostname_share_path = "//#{computer_name}/#{absolute_path('spec/data/workbook.xls').tr('\\','/').gsub('C:','c$')}"
63
+ @hostname_share_path = "//#{computer_name}/spec/data/workbook.xls"
65
64
  @network_path_not_existing = "M:/data/workbook.xls"
66
65
  @hostname_not_existing_share_path = "//DESKTOP_not_existing/spec/data/workbook.xls"
67
66
  @hostname_share_not_existing_path = "//#{computer_name}/c$/gim/ats/aSrc/gems/robust_excel_ole/spec_not_existing/data/workbook.xls"
Binary file
Binary file
Binary file
Binary file
data/spec/excel_spec.rb CHANGED
@@ -457,7 +457,7 @@ module RobustExcelOle
457
457
  @excel1.Visible.should be false
458
458
  @excel1.DisplayAlerts.should be false
459
459
  @book1.should_not be_alive
460
- @book1.reopen
460
+ @book1.open
461
461
  @book1.should be_alive
462
462
  @excel1.close
463
463
  @excel1.should_not be_alive
@@ -473,7 +473,7 @@ module RobustExcelOle
473
473
  @excel1.should be_alive
474
474
  @excel1.Visible.should be true
475
475
  @excel1.DisplayAlerts.should be true
476
- @book1.reopen
476
+ @book1.open
477
477
  @book1.should be_alive
478
478
  @excel1.close
479
479
  @excel1.should_not be_alive
@@ -487,7 +487,7 @@ module RobustExcelOle
487
487
  @excel1.should be_alive
488
488
  @excel1.Visible.should be true
489
489
  @excel1.DisplayAlerts.should be true
490
- @book1.reopen
490
+ @book1.open
491
491
  @book1.should be_alive
492
492
  @excel1.close
493
493
  @excel1.should_not be_alive
@@ -539,7 +539,7 @@ module RobustExcelOle
539
539
  @excel3.should be_a Excel
540
540
  @excel3.Visible.should be true
541
541
  @excel3.DisplayAlerts.should be true
542
- @book3.reopen
542
+ @book3.open
543
543
  @book3.should be_alive
544
544
  @book3.excel.should == @excel3
545
545
  @excel1.close(:if_unsaved => :forget)
data/spec/general_spec.rb CHANGED
@@ -45,8 +45,7 @@ module RobustExcelOle
45
45
  @simple_file_xlsx = @dir + '/workbook.xlsx'
46
46
  @network_path = "N:/data/workbook.xls"
47
47
  computer_name = NETWORK.ComputerName
48
- #@hostname_share_path = "//#{computer_name}/spec/data/workbook.xls"
49
- @hostname_share_path = "//#{computer_name}/#{absolute_path('spec/data/workbook.xls').tr('\\','/').gsub('C:','c$')}"
48
+ @hostname_share_path = "//#{computer_name}/spec/data/workbook.xls"
50
49
  @network_path_downcase = @network_path.downcase
51
50
  @hostname_share_path_downcase = @hostname_share_path.downcase
52
51
  @simple_file_extern = "D:/data/workbook.xls"
@@ -128,6 +128,15 @@ describe ListObject do
128
128
 
129
129
  end
130
130
 
131
+ describe "workbook" do
132
+
133
+ it "should access the workbook" do
134
+ table = @sheet.table(1)
135
+ table.workbook.should == @book
136
+ end
137
+
138
+ end
139
+
131
140
  describe "each" do
132
141
 
133
142
  before do
@@ -137,7 +146,7 @@ describe ListObject do
137
146
  it "should traverse through list rows" do
138
147
  @table1.each.with_index do |listrow, i|
139
148
  listrow.values.should == [3.0, "John", 50.0, 0.5, 30] if i == 0
140
- listrow.values.should == [2.0, "Fred", nil, 0.5416666666666666, 40] if i == 1
149
+ listrow.values.should == [2.0, "Fred", 0.0, 0.5416666666666666, 40] if i == 1
141
150
  listrow.values.should == [nil, nil, nil, nil, nil] if i == 2
142
151
  end
143
152
  end
@@ -173,7 +182,7 @@ describe ListObject do
173
182
  it "should access a listrow given its number" do
174
183
  listrow = @table1[2]
175
184
  listrow.should be_a ListRow
176
- listrow.values.should == [2.0, "Fred", nil, 0.5416666666666666, 40]
185
+ listrow.values.should == [2.0, "Fred", 0.0, 0.5416666666666666, 40]
177
186
  end
178
187
 
179
188
  it "should access a listrow via a multiple-column key" do
@@ -189,6 +198,14 @@ describe ListObject do
189
198
  @table1[{:person => "Angel"}].values.should == [3.0, "Angel", 100, 0.6666666666666666, 60]
190
199
  end
191
200
 
201
+ it "should access a listrow with key value which constitutes a number, type-independent" do
202
+ @table1[{"Amount" => 100}].values.should == [3.0, "Angel", 100, 0.6666666666666666, 60]
203
+ @table1[{"Amount" => "100"}].values.should == [3.0, "Angel", 100, 0.6666666666666666, 60]
204
+ @table1[{"Amount" => 0}].values.should == [2.0, "Fred", 0, 0.5416666666666666, 40.0]
205
+ @table1[{"Amount" => "0"}].values.should == [2.0, "Fred", 0, 0.5416666666666666, 40.0]
206
+ @table1[{"Amount" => "hello"}].should == nil
207
+ end
208
+
192
209
  it "should yield nil if there is no match" do
193
210
  @table1[{"Number" => 5, "Person" => "Angel"}].should == nil
194
211
  @table1[{"Number" => 5, "Person" => "Angel"}, limit: :first].should == nil
@@ -252,7 +269,7 @@ describe ListObject do
252
269
  table_value = [
253
270
  ["Number", "Person", "Amount", "Time", "Price"],
254
271
  [3.0, "John", 50.0, 0.5, 30.0],
255
- [2.0, "Fred", nil, 0.5416666666666666, 40.0],
272
+ [2.0, "Fred", 0.0, 0.5416666666666666, 40.0],
256
273
  [nil, nil, nil, nil, nil],
257
274
  [3.0, "Angel", 100.0, 0.6666666666666666, 60.0],
258
275
  [nil, nil, nil, nil, nil],
@@ -387,7 +404,7 @@ describe ListObject do
387
404
  listrows = @table.ListRows
388
405
  listrows.Item(1).Range.Value.first.should == [3.0, "John", 50.0, 0.5, 30]
389
406
  listrows.Item(2).Range.Value.first.should == [nil,nil,nil,nil,nil]
390
- listrows.Item(3).Range.Value.first.should == [2.0, "Fred", nil, 0.5416666666666666, 40]
407
+ listrows.Item(3).Range.Value.first.should == [2.0, "Fred", 0.0, 0.5416666666666666, 40]
391
408
  expect{
392
409
  @table.add_row(16)
393
410
  }.to raise_error(TableError)
@@ -398,7 +415,7 @@ describe ListObject do
398
415
  listrows = @table.ListRows
399
416
  listrows.Item(1).Range.Value.first.should == [3.0, "John", 50.0, 0.5, 30]
400
417
  listrows.Item(2).Range.Value.first.should == [2.0, "Herbert", 30.0, 0.25, 40]
401
- listrows.Item(3).Range.Value.first.should == [2.0, "Fred", nil, 0.5416666666666666, 40]
418
+ listrows.Item(3).Range.Value.first.should == [2.0, "Fred", 0.0, 0.5416666666666666, 40]
402
419
  end
403
420
 
404
421
  it "should add a row with contents with umlauts" do
@@ -417,7 +434,7 @@ describe ListObject do
417
434
  end
418
435
 
419
436
  it "should delete the contents of a column" do
420
- @table.ListColumns.Item(3).Range.Value.should == [["Amount"],[50],[nil],[nil],[100],[nil],[40],[50],[nil],[nil],[nil],[nil],[40],[20]]
437
+ @table.ListColumns.Item(3).Range.Value.should == [["Amount"],[50],[0],[nil],[100],[nil],[40],[50],[nil],[nil],[nil],[nil],[40],[20]]
421
438
  @table.delete_column_values(3)
422
439
  @table.HeaderRowRange.Value.first.should == ["Number","Person", "Amount", "Time","Price"]
423
440
  @table.ListColumns.Item(3).Range.Value.should == [["Amount"],[nil],[nil],[nil],[nil],[nil],[nil],[nil],[nil],[nil],[nil],[nil],[nil],[nil]]
@@ -430,7 +447,7 @@ describe ListObject do
430
447
  end
431
448
 
432
449
  it "should delete the contents of a row" do
433
- @table.ListRows.Item(2).Range.Value.first.should == [2.0, "Fred", nil, 0.5416666666666666, 40]
450
+ @table.ListRows.Item(2).Range.Value.first.should == [2.0, "Fred", 0.0, 0.5416666666666666, 40]
434
451
  @table.delete_row_values(2)
435
452
  @table.ListRows.Item(2).Range.Value.first.should == [nil,nil,nil,nil,nil]
436
453
  @table.ListRows.Item(1).Range.Value.first.should == [3.0, "John", 50.0, 0.5, 30]
@@ -445,7 +462,7 @@ describe ListObject do
445
462
  @table.delete_empty_rows
446
463
  @table.ListRows.Count.should == 9
447
464
  @table.ListRows.Item(1).Range.Value.first.should == [3.0, "John", 50.0, 0.5, 30]
448
- @table.ListRows.Item(2).Range.Value.first.should == [2.0, "Fred", nil, 0.5416666666666666, 40]
465
+ @table.ListRows.Item(2).Range.Value.first.should == [2.0, "Fred", 0.0, 0.5416666666666666, 40]
449
466
  @table.ListRows.Item(3).Range.Value.first.should == [3, "Angel", 100, 0.6666666666666666, 60]
450
467
  @table.ListRows.Item(4).Range.Value.first.should == [1,"Werner",40,0.5, 80]
451
468
  end
@@ -525,7 +542,7 @@ describe ListObject do
525
542
  @table.sort("Number")
526
543
  @table.ListRows.Item(1).Range.Value.first.should == [1, "Werner",40,0.5, 80]
527
544
  @table.ListRows.Item(2).Range.Value.first.should == [1, "Napoli", 20.0, 0.4166666666666667, 70.0]
528
- @table.ListRows.Item(3).Range.Value.first.should == [2, "Fred", nil, 0.5416666666666666, 40]
545
+ @table.ListRows.Item(3).Range.Value.first.should == [2, "Fred", 0.0, 0.5416666666666666, 40]
529
546
  @table.ListRows.Item(4).Range.Value.first.should == [3, "John", 50.0, 0.5, 30]
530
547
  @table.ListRows.Item(5).Range.Value.first.should == [3, "Angel", 100, 0.6666666666666666, 60]
531
548
  @table.ListRows.Item(6).Range.Value.first.should == [3, "Eiffel", 50.0, 0.5, 30]
@@ -29,6 +29,34 @@ describe ListRow do
29
29
  rm_tmp(@dir)
30
30
  end
31
31
 
32
+ describe "accessing several tables" do
33
+
34
+ it "should preserve ole_table" do
35
+ table1 = @sheet.table(1)
36
+ table1[1].values.should == [3.0, "John", 50.0, 0.5, 30.0]
37
+ table2 = Table.new(@sheet, "table_name", [1,1], 3, ["Person","Amount"])
38
+ table2[1].values.should == [nil, nil]
39
+ table1[1].values.should == [3.0, "John", 50.0, 0.5, 30.0]
40
+ end
41
+
42
+ end
43
+
44
+ describe "#methods, #respond_to?" do
45
+
46
+ before do
47
+ @table1 = @sheet.table(1)
48
+ @tablerow1 = @table1[2]
49
+ end
50
+
51
+ it "should contain column name as methods" do
52
+ column_names_both_cases = @table1.column_names + @table1.column_names.map{|c| c.downcase}
53
+ column_names_both_cases.map{|c| c.to_sym}.each do |column_name_method|
54
+ @tablerow1.methods.include?(column_name_method).should be true
55
+ @tablerow1.respond_to?(column_name_method)
56
+ end
57
+ end
58
+ end
59
+
32
60
  describe "==" do
33
61
 
34
62
  before do
@@ -45,27 +73,14 @@ describe ListRow do
45
73
 
46
74
  end
47
75
 
48
- describe "accessing several tables" do
49
-
50
- it "should preserve the table when accessing table rows in several tables" do
51
- table1 = @sheet.table(1)
52
- values1 = table1[1].values
53
- table2 = Table.new(@sheet, "table_name", [1,1], 3, ["Person","Amount"])
54
- values2 = table2[1].values
55
- table1[1].values.should == values1
56
- table1[1].values.should_not == values2
57
- end
58
-
59
- end
60
-
61
76
  describe "promote" do
62
77
 
63
78
  it "should promote a win32ole tablerow" do
64
79
  table1 = @sheet.table(1)
65
80
  tablerow1 = table1[2]
66
81
  ole_tablerow1 = tablerow1.ole_tablerow
67
- ListRow.new(ole_tablerow1).values.should == [2.0, "Fred", nil, 0.5416666666666666, 40]
68
- ListRow.new(tablerow1).values.should == [2.0, "Fred", nil, 0.5416666666666666, 40]
82
+ ListRow.new(ole_tablerow1).values.should == [2.0, "Fred", 0, 0.5416666666666666, 40]
83
+ ListRow.new(tablerow1).values.should == [2.0, "Fred", 0, 0.5416666666666666, 40]
69
84
  end
70
85
  end
71
86
 
@@ -76,13 +91,13 @@ describe ListRow do
76
91
  end
77
92
 
78
93
  it "should yield values of a row" do
79
- @table1[2].to_a.should == [2.0, "Fred", nil, 0.5416666666666666, 40]
80
- @table1[2].values.should == [2.0, "Fred", nil, 0.5416666666666666, 40]
94
+ @table1[2].to_a.should == [2.0, "Fred", 0, 0.5416666666666666, 40]
95
+ @table1[2].values.should == [2.0, "Fred", 0, 0.5416666666666666, 40]
81
96
  end
82
97
 
83
98
  it "should yield key-value pairs of a row" do
84
- @table1[2].to_h.should == {"Number" => 2.0, "Person" => "Fred", "Amount" => nil, "Time" => 0.5416666666666666, "Price" => 40}
85
- @table1[2].keys_values.should == {"Number" => 2.0, "Person" => "Fred", "Amount" => nil, "Time" => 0.5416666666666666, "Price" => 40}
99
+ @table1[2].to_h.should == {"Number" => 2.0, "Person" => "Fred", "Amount" => 0, "Time" => 0.5416666666666666, "Price" => 40}
100
+ @table1[2].keys_values.should == {"Number" => 2.0, "Person" => "Fred", "Amount" => 0, "Time" => 0.5416666666666666, "Price" => 40}
86
101
  end
87
102
 
88
103
  it "should yield values and key-value pairs of a row with umlauts" do
@@ -187,7 +202,6 @@ describe ListRow do
187
202
  @table_row1.verkaeufer.should be nil
188
203
  @table_row1.verkaeufer = "John"
189
204
  @table_row1.verkaeufer.should == "John"
190
- @table_row1.verkäufer.should == "John"
191
205
  @sheet[2,1].should == "John"
192
206
  @table_row1.Verkaeufer = "Herbert"
193
207
  @table_row1.Verkaeufer.should == "Herbert"
data/spec/range_spec.rb CHANGED
@@ -47,6 +47,15 @@ describe RobustExcelOle::Range do
47
47
  end
48
48
  end
49
49
 
50
+ describe "#workbook, worksheet" do
51
+
52
+ it "should access workbook" do
53
+ @range2.worksheet.should == @sheet
54
+ @range2.workbook.should == @book
55
+ end
56
+
57
+ end
58
+
50
59
  describe "#each" do
51
60
 
52
61
  it "items is RobustExcelOle::Cell" do
@@ -675,7 +675,7 @@ describe Workbook do
675
675
  book1 = @book
676
676
  @book.close
677
677
  @book.should_not be_alive
678
- @book.reopen
678
+ @book.open
679
679
  @book.should be_a Workbook
680
680
  @book.should be_alive
681
681
  @book.should === book1
@@ -826,7 +826,7 @@ describe Workbook do
826
826
  book2 = Workbook.open(@different_file, :force => {:excel => :new})
827
827
  @book.close
828
828
  book2.close
829
- @book.reopen
829
+ @book.open
830
830
  Workbook.unobtrusively(@simple_file1) do |book|
831
831
  book.should be_a Workbook
832
832
  book.should be_alive