robust_excel_ole 1.14 → 1.15

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b63b8ec97e714c16c5b4e3f1bb60f7e160aba0469ad14d47b766fa1778919c1f
4
- data.tar.gz: 1e0af619c00875ae92782cfa25a091335de3e1235723be9c2ecef2924b4f3cf9
3
+ metadata.gz: 4c75264c3aa717667b2c38799b6fad4043f45a307d9d434b8e3d4a00ca0eff35
4
+ data.tar.gz: dbb9ba06291ab9f9efdb616d5b0f34d9f425166fc5a90accba4efed3b0afb99e
5
5
  SHA512:
6
- metadata.gz: 54d8101340e7a2c82af0a761998a466a931df0dc5eaf0bd5117a513f89ebf3e38fa6e27dd04af909f0d8cf8125117b126c8df7d642843c993862f6af2966afbd
7
- data.tar.gz: 9f92d1018a35b7d018b7b3c3f7f3bb71383b7cb3914429a7986fe097a3ea9e8ac7ae77c011adff4075d4b56946c08ae8d9fdb9c69a51abb65b6109ea4f66cd00
6
+ metadata.gz: e171843b4c4fa899a2db7ee69f21656dfc005396cd3f372977a9e520ff47b019642ddb188af41870bf79216508195609389743ffdb100c8c7975450d4334da1b
7
+ data.tar.gz: 5cb5bfe60497f110077d27746e90d15ad455e94a2a289762372407361f26cc7163e2ea211720782c99e4b00abb48be954a2507ae855faf0b0cc069408b1e73f6
data/Changelog CHANGED
@@ -1,11 +1,17 @@
1
1
  # Change Log
2
2
  All notable changes to this project will be documented in this file.
3
3
 
4
- ## [1.14]
4
+ ## [1.15]
5
+
6
+ ### Added
7
+ - Workbook#unobtrusively, Workbook#for_reading, Workbook#for_modifying
5
8
 
6
9
  ### Changed
7
10
  - Workbook.unobtrusively: removed parameter :rw_change_excel
8
11
 
12
+ ## [1.14]
13
+
14
+ - no interface change
9
15
 
10
16
  ## [1.13]
11
17
 
@@ -61,14 +61,11 @@ Let's open a workbook.
61
61
  Now we have a Workbook object that wraps a WIN32OLE object. That is, you can send any WIN32OLE (VBA) method to it. See
62
62
  https://docs.microsoft.com/en-us/office/vba/api/excel.workbook#methods.
63
63
 
64
- For example, you can determine the name and whether the workbook is visible.
64
+ For example, you can determine the name of the workbook.
65
65
 
66
66
  workbook.Name
67
67
  # => "workbook.xls"
68
68
 
69
- workbook.Visible
70
- # => false
71
-
72
69
  For some common tasks and for considering various complex cases of Excel and user behaviour, more convenient methods are implemented. For example, RobustExcelOle provides methods for reading and writing the contents of ranges, for opening, saving, closing, reopening and unobtrusively opening workbooks, and for setting options.
73
70
 
74
71
  First we want to make the workbook visible.
@@ -92,7 +89,7 @@ Then we'll save the workbook.
92
89
 
93
90
  === More features when opening, modifying, creating, saving and closing workbooks
94
91
 
95
- RobustExcelOle allows unobtrusively reading and modifying workbooks, i.e. accessing workbooks without changing their "status". The status comprises whether the workbook is open or closed, saved or unsaved, read-only or writable, visible or invisible, whether the calculation mode is manual or automatic, and checking compatibility is done or not done (the Workbook object remembers its properties).
92
+ RobustExcelOle allows unobtrusively reading and modifying workbooks, i.e. accessing workbooks without changing their "status". The status comprises whether the workbook is open or closed, saved or unsaved, read-only or writable, visible or invisible, whether the calculation mode is manual or automatic, and checking compatibility is done or not done.
96
93
 
97
94
  Workbook.unobtrusively('spec/data/workbook.xls') do |workbook|
98
95
  # do something
@@ -26,7 +26,7 @@ Options of the methods +create+ and +new+ are +:reuse+ (+true+, +false+), +:visi
26
26
 
27
27
  The option +:calculation+ specifies, whether the calculation mode is being forced to be manual (:manual), automatic (+:automatic+) or is not being forced (+nil+).
28
28
 
29
- You can also promote an Excel instance represented as WIN32OLE object to an Excel object.
29
+ You can also type-lift an Excel instance represented as WIN32OLE object to an Excel object.
30
30
 
31
31
  excel = Excel.new(win32ole_object, :visible => true)
32
32
 
@@ -145,18 +145,18 @@ The closed workbook is now alive again, i.e. is open and responds to Excel metho
145
145
  === Promoting WIN32OLE objects to RobustExcelOle objects
146
146
 
147
147
  Promoting means here: enriching a given object of a certain class by properties and methods of another class.
148
- The method +General.to_reo+ enables promoting WIN32OLE objects to RobustExcelOle objects such that the attributes and methods of RobustExcelOle can be applied to these objects. For example, assume we have a WIN32OLE workbook +win32ole_workbook+:
148
+ The method +General.to_reo+ enables type-lifting WIN32OLE objects to RobustExcelOle objects, in the sense that the attributes and methods of RobustExcelOle can be applied to these objects. For example, assume we have a WIN32OLE workbook +win32ole_workbook+:
149
149
 
150
150
  win32ole_workbook.to_class
151
151
  => WIN32OLE
152
152
 
153
- This object can be promoted to a RobustExcelOle workbook.
153
+ This object can be type-lifted to a RobustExcelOle workbook.
154
154
 
155
155
  workbook = win32ole_workbook.to_reo
156
156
  workbook.to_class
157
157
  => RobustExcelOle::Workbook
158
158
 
159
- Similarly, Excel, Worksheet, and Range WIN32OLE objects can be promoted to corresponding RobustExcelOle objects.
159
+ Similarly, Excel, Worksheet, and Range WIN32OLE objects can be type-lifted to corresponding RobustExcelOle objects.
160
160
 
161
161
  The method +to_reo+ uses the method +new+. You can apply the method +new+ directly.
162
162
 
@@ -169,7 +169,7 @@ You can supply options, e.g. +:visible+.
169
169
 
170
170
  === Identity transperence ===
171
171
 
172
- A RobustExcelOle Workbook object is a proxy of an Excel WIN32OLE workbook. A Workbook object is defined by the full workbook name and the Excel instance in which it is opened. RobustExcelOle ensures identity transparency which means that identical Workbook objects refer to identical Excel workbooks, and vice versa. Identity transperence ensures that, no matter how a Workbook object was created - by filename or by promoting an Excel workbook - two Workbook objects are identical, if and only if the Excel workbooks, they are referring to, are identical.
172
+ A RobustExcelOle Workbook object is a proxy of an Excel WIN32OLE workbook. A Workbook object is defined by the full workbook name and the Excel instance in which it is opened. RobustExcelOle ensures identity transparency which means that identical Workbook objects refer to identical Excel workbooks, and vice versa. Identity transperence ensures that, no matter how a Workbook object was created - by filename or by type-lifting an Excel workbook - two Workbook objects are identical, if and only if the Excel workbooks, they are referring to, are identical.
173
173
 
174
174
  Similarly, each Excel, Worksheet and a Range object in RobustExcelOle is a proxy of a corresponding Excel, Worksheet and a Range object in WIN32OLE. For these objects identity transperence holds as well.
175
175
 
@@ -179,32 +179,41 @@ The method +unobtrusively+ enables the user to read or modify a workbook, no mat
179
179
 
180
180
  Options are the following:
181
181
 
182
- +:if_closed+:: +:current+ (or +:active, or +:reuse+:): (default) : open a closed workbook in the Excel instance where it was opened most recently, if such an Excel instance exists, otherwise open it in the current (first opened) Excel instance
183
-
182
+ +:if_closed+:: the Excel instance in which to open the workbook, if it was closed (default: +:current+). (Note: this option works workbooks opened via RobustExcelOle only.)
183
+
184
184
  +:read_only+:: Whether the workbook shall be forced to be open in ReadOnly mode
185
185
  +:writable+:: Whether changes in the workbook shall be saved
186
186
 
187
- +:rw_change_excel+:: Excel instance in which the workbook with the
188
- changed read-write permissions shall be opened
189
- :current (default), :new or an Excel instance
190
-
191
- +:keep_open+:: Whether the workbook shall be open after unobtrusively opening (default: false)
187
+ +:keep_open+:: Whether the workbook shall be open after unobtrusively opening (default: false)
192
188
 
189
+ There are the class method and the instance method of +unobtrusively+. Here is an example of the class method:
193
190
 
194
191
  Workbook.unobtrusively('spec/data/workbook.xls') do |book|
195
192
  # some modification
196
- worksheet = book[0]
197
- worksheet[1,1] = "c"
193
+ book.sheet(1)[1,1] = "c"
198
194
  end
199
-
200
- The methods +for_reading+ and +for_modifying+ indicate unobtrusively reading or modifying.
195
+
196
+ Here is an example of the instance method:
197
+
198
+ book.unobtrusively do
199
+ # some modification
200
+ book.sheet(1)[1,1] = "c"
201
+ end
202
+
203
+ The methods +for_reading+ and +for_modifying+ indicate unobtrusively reading or modifying.
201
204
 
202
205
  Workbook.for_modifying('spec/data/workbook.xls') do |book|
203
206
  # some modification
204
- worksheet = book[0]
205
- worksheet[1,1] = "c"
207
+ book.sheet(1)[1,1] = "c"
206
208
  end
207
209
 
210
+ An example of the instance method would be
211
+
212
+ book.for_modifying do
213
+ # some modification
214
+ book.sheet(1)[1,1] = "c"
215
+ end
216
+
208
217
  Note, that the methods +unobtrusively+, +for_reading+ and +for_modifying+ work not only for workbooks opened via RobustExcelOle, but connect to workbooks opened outside RobustExcelOle as well.
209
218
 
210
219
  === Retaining the saved-status
@@ -218,7 +227,7 @@ This method ensures keeping the save status of the workbook
218
227
 
219
228
  === Checking whether the workbook is alive.
220
229
 
221
- This method finds out whether the Excel workbook that is referenced by the Workbook object responds to methods.
230
+ The method +alive?+ finds out whether the Excel workbook that is referenced by the Workbook object responds to methods. For example
222
231
 
223
232
  workbook.alive?
224
233
  # => true
@@ -19,7 +19,7 @@ module RobustExcelOle
19
19
  # @private
20
20
  def method_missing(name, *args)
21
21
  #if name.to_s[0,1] =~ /[A-Z]/
22
- if JRUBY_BUG_ERRORMESSAGE
22
+ if ::JRUBY_BUG_ERRORMESSAGE
23
23
  begin
24
24
  @cell.send(name, *args)
25
25
  rescue Java::OrgRacobCom::ComFailException
@@ -3,6 +3,8 @@
3
3
  require 'weakref'
4
4
  require 'Win32API'
5
5
 
6
+
7
+
6
8
  def ka
7
9
  Excel.kill_all
8
10
  end
@@ -16,19 +18,14 @@ module RobustExcelOle
16
18
 
17
19
  class Excel < RangeOwners
18
20
  attr_accessor :ole_excel
19
- #attr_accessor :created
20
- attr_accessor :workbook
21
-
22
- # setter methods are implemented below
23
- attr_reader :visible
24
- attr_reader :displayalerts
25
- attr_reader :calculation
26
- attr_reader :screenupdating
21
+ attr_reader :properties
27
22
 
28
23
  alias ole_object ole_excel
29
24
 
30
25
  @@hwnd2excel = {}
31
26
 
27
+ PROPERTIES = [:visible, :displayalerts, :calculation, :screenupdating]
28
+
32
29
  # creates a new Excel instance
33
30
  # @param [Hash] options the options
34
31
  # @option options [Variant] :displayalerts
@@ -96,10 +93,7 @@ module RobustExcelOle
96
93
  unless reused || connected
97
94
  options = { :displayalerts => :if_visible, :visible => false, :screenupdating => true }.merge(options)
98
95
  end
99
- result.visible = options[:visible] unless options[:visible].nil?
100
- result.displayalerts = options[:displayalerts] unless options[:displayalerts].nil?
101
- result.calculation = options[:calculation] unless options[:calculation].nil?
102
- result.screenupdating = options[:screenupdating] unless options[:screenupdating].nil?
96
+ result.set_options(options)
103
97
  end
104
98
  result
105
99
  end
@@ -118,13 +112,11 @@ module RobustExcelOle
118
112
  def recreate(opts = {})
119
113
  unless alive?
120
114
  opts = {
121
- :visible => @visible || false,
122
- :displayalerts => @displayalerts || :if_visible
115
+ :visible => @properties[:visible] || false,
116
+ :displayalerts => @properties[:displayalerts] || :if_visible
123
117
  }.merge(opts)
124
118
  @ole_excel = WIN32OLE.new('Excel.Application')
125
- self.visible = opts[:visible]
126
- self.displayalerts = opts[:displayalerts]
127
- self.calculation = opts[:calculation]
119
+ set_options(opts)
128
120
  if opts[:reopen_workbooks]
129
121
  books = workbook_class.books
130
122
  books.each do |book|
@@ -399,7 +391,7 @@ module RobustExcelOle
399
391
  # if this Excel instance is being closed, then Excel creates a new Excel instance
400
392
  # @private
401
393
  def self.current_ole_excel
402
- if JRUBY_BUG_CONNECT
394
+ if ::JRUBY_BUG_CONNECT
403
395
  result = known_excel_instance
404
396
  if result.nil?
405
397
  if excels_number > 0
@@ -545,7 +537,7 @@ module RobustExcelOle
545
537
 
546
538
  # sets DisplayAlerts in a block
547
539
  def with_displayalerts displayalerts_value
548
- old_displayalerts = displayalerts
540
+ old_displayalerts = @properties[:displayalerts]
549
541
  self.displayalerts = displayalerts_value
550
542
  begin
551
543
  yield self
@@ -556,26 +548,29 @@ module RobustExcelOle
556
548
 
557
549
  # makes the current Excel instance visible or invisible
558
550
  def visible= visible_value
559
- @ole_excel.Visible = @visible = visible_value
560
- @ole_excel.DisplayAlerts = @visible if @displayalerts == :if_visible
551
+ return if visible_value.nil?
552
+ @ole_excel.Visible = @properties[:visible] = visible_value
553
+ @ole_excel.DisplayAlerts = @properties[:visible] if @properties[:displayalerts] == :if_visible
561
554
  end
562
555
 
563
556
  # enables DisplayAlerts in the current Excel instance
564
557
  def displayalerts= displayalerts_value
565
- @displayalerts = displayalerts_value
566
- @ole_excel.DisplayAlerts = @displayalerts == :if_visible ? @ole_excel.Visible : displayalerts_value
558
+ return if displayalerts_value.nil?
559
+ @properties[:displayalerts] = displayalerts_value
560
+ @ole_excel.DisplayAlerts = @properties[:displayalerts] == :if_visible ? @ole_excel.Visible : displayalerts_value
567
561
  end
568
562
 
569
563
  # sets ScreenUpdating
570
564
  def screenupdating= screenupdating_value
571
- @ole_excel.ScreenUpdating = @screenupdating = screenupdating_value
565
+ return if screenupdating_value.nil?
566
+ @ole_excel.ScreenUpdating = @properties[:screenupdating] = screenupdating_value
572
567
  end
573
568
 
574
569
  # sets calculation mode
575
570
  # retains the saved-status of the workbooks when set to manual
576
571
  def calculation= calculation_mode
577
572
  return if calculation_mode.nil?
578
- @calculation = calculation_mode
573
+ @properties[:calculation] = calculation_mode
579
574
  calc_mode_changable = @ole_excel.Workbooks.Count > 0 && @ole_excel.Calculation.is_a?(Integer)
580
575
  if calc_mode_changable
581
576
  retain_saved_workbooks do
@@ -615,9 +610,9 @@ module RobustExcelOle
615
610
  def Calculation= calculation_vba_mode
616
611
  case calculation_vba_mode
617
612
  when XlCalculationManual
618
- @calculation = :manual
613
+ @properties[:calculation] = :manual
619
614
  when XlCalculationAutomatic
620
- @calculation = :automatic
615
+ @properties[:calculation] = :automatic
621
616
  end
622
617
  @ole_excel.Calculation = calculation_vba_mode
623
618
  end
@@ -637,13 +632,22 @@ module RobustExcelOle
637
632
 
638
633
  # set options in this Excel instance
639
634
  def for_this_instance(options)
640
- self.class.new(@ole_excel, options)
635
+ set_options(options)
636
+ #self.class.new(@ole_excel, options)
641
637
  end
642
638
 
643
- def set_options(options)
644
- for_this_instance(options)
645
- end
639
+ #def set_options(options)
640
+ # for_this_instance(options)
641
+ #end
646
642
 
643
+ def set_options(options)
644
+ @properties ||= { }
645
+ PROPERTIES.each do |property|
646
+ method = (property.to_s + '=').to_sym
647
+ self.send(method, options[property])
648
+ end
649
+ end
650
+
647
651
  # set options in all workbooks
648
652
  def for_all_workbooks(options)
649
653
  each_workbook(options)
@@ -696,6 +700,13 @@ module RobustExcelOle
696
700
  workbook.color_if_modified = old_color_if_modified
697
701
  end
698
702
 
703
+ # @private
704
+ # returns active workbook
705
+ def workbook
706
+ return @workbook unless @workbook.nil?
707
+ @workbook = workbook_class.new(@ole_excel.ActiveWorkbook)
708
+ end
709
+
699
710
  # @private
700
711
  def to_s
701
712
  '#<Excel: ' + hwnd.to_s + ('not alive' unless alive?).to_s + '>'
@@ -729,7 +740,7 @@ module RobustExcelOle
729
740
  def method_missing(name, *args)
730
741
  if name.to_s[0,1] =~ /[A-Z]/
731
742
  raise ObjectNotAlive, 'method missing: Excel not alive' unless alive?
732
- if JRUBY_BUG_ERRORMESSAGE
743
+ if ::JRUBY_BUG_ERRORMESSAGE
733
744
  begin
734
745
  @ole_excel.send(name, *args)
735
746
  rescue Java::OrgRacobCom::ComFailException => msg
@@ -3,11 +3,11 @@
3
3
  module General
4
4
 
5
5
  IS_JRUBY_PLATFORM = (RUBY_PLATFORM =~ /java/)
6
- JRUBY_BUG_CONNECT = IS_JRUBY_PLATFORM && true
7
- JRUBY_BUG_COPYSHEETS = IS_JRUBY_PLATFORM && true
8
- JRUBY_BUG_ERRORMESSAGE = IS_JRUBY_PLATFORM && true
9
- JRUBY_BUG_CONNECTEXCEL = IS_JRUBY_PLATFORM && true
10
- JRUBY_BUG_RANGES = IS_JRUBY_PLATFORM && true
6
+ ::JRUBY_BUG_CONNECT = IS_JRUBY_PLATFORM && true
7
+ ::JRUBY_BUG_COPYSHEETS = IS_JRUBY_PLATFORM && true
8
+ ::JRUBY_BUG_ERRORMESSAGE = IS_JRUBY_PLATFORM && true
9
+ ::JRUBY_BUG_CONNECTEXCEL = IS_JRUBY_PLATFORM && true
10
+ ::JRUBY_BUG_RANGES = IS_JRUBY_PLATFORM && true
11
11
 
12
12
 
13
13
  # @private
@@ -75,7 +75,7 @@ class WIN32OLE
75
75
 
76
76
  include RobustExcelOle
77
77
 
78
- # promoting WIN32OLE objects to RobustExcelOle objects
78
+ # type-lifting WIN32OLE objects to RobustExcelOle objects
79
79
  def to_reo
80
80
  class2method = [{Excel => :Hwnd}, {Workbook => :FullName}, {Worksheet => :Copy}, {Range => :Address}]
81
81
  class2method.each do |element|
@@ -27,7 +27,7 @@ module RobustExcelOle
27
27
  # @returns [Array] the values
28
28
  def values(range = nil)
29
29
  #result = map { |x| x.Value }.flatten
30
- result_unflatten = if !JRUBY_BUG_RANGES
30
+ result_unflatten = if !::JRUBY_BUG_RANGES
31
31
  map { |x| x.v }
32
32
  else
33
33
  self.v
@@ -44,7 +44,7 @@ module RobustExcelOle
44
44
 
45
45
  def v
46
46
  begin
47
- if !JRUBY_BUG_RANGES
47
+ if !::JRUBY_BUG_RANGES
48
48
  self.Value
49
49
  else
50
50
  address_r1c1 = self.AddressLocal(true,true,XlR1C1)
@@ -65,7 +65,7 @@ module RobustExcelOle
65
65
 
66
66
  def v=(value)
67
67
  begin
68
- if !JRUBY_BUG_RANGES
68
+ if !::JRUBY_BUG_RANGES
69
69
  ole_range.Value = value
70
70
  else
71
71
  address_r1c1 = ole_range.AddressLocal(true,true,XlR1C1)
@@ -240,7 +240,7 @@ module RobustExcelOle
240
240
  # @private
241
241
  def method_missing(name, *args)
242
242
  if name.to_s[0,1] =~ /[A-Z]/
243
- if JRUBY_BUG_ERRORMESSAGE
243
+ if ::JRUBY_BUG_ERRORMESSAGE
244
244
  begin
245
245
  @ole_range.send(name, *args)
246
246
  rescue Java::OrgRacobCom::ComFailException
@@ -24,7 +24,7 @@ module RobustExcelOle
24
24
  ole_range = name_obj.RefersToRange
25
25
  value = begin
26
26
  #name_obj.RefersToRange.Value
27
- if !JRUBY_BUG_RANGES
27
+ if !::JRUBY_BUG_RANGES
28
28
  ole_range.Value
29
29
  else
30
30
  values = RobustExcelOle::Range.new(ole_range).v
@@ -39,7 +39,7 @@ module RobustExcelOle
39
39
  #sheet.Evaluate(name_obj.Name).Value
40
40
  # does it result in a range?
41
41
  ole_range = sheet.Evaluate(name_obj.Name)
42
- if !JRUBY_BUG_RANGES
42
+ if !::JRUBY_BUG_RANGES
43
43
  ole_range.Value
44
44
  else
45
45
  values = RobustExcelOle::Range.new(ole_range).v
@@ -71,7 +71,7 @@ module RobustExcelOle
71
71
  ole_range = name_object(name).RefersToRange
72
72
  workbook.color_if_modified = opts[:color] unless opts[:color].nil?
73
73
  ole_range.Interior.ColorIndex = workbook.color_if_modified unless workbook.color_if_modified.nil?
74
- if !JRUBY_BUG_RANGES
74
+ if !::JRUBY_BUG_RANGES
75
75
  ole_range.Value = value
76
76
  else
77
77
  address_r1c1 = ole_range.AddressLocal(true,true,XlR1C1)
@@ -106,7 +106,7 @@ module RobustExcelOle
106
106
  end
107
107
  begin
108
108
  #value = ole_range.Value
109
- value = if !JRUBY_BUG_RANGES
109
+ value = if !::JRUBY_BUG_RANGES
110
110
  ole_range.Value
111
111
  else
112
112
  values = RobustExcelOle::Range.new(ole_range).v
@@ -137,7 +137,7 @@ module RobustExcelOle
137
137
  begin
138
138
  workbook.color_if_modified = opts[:color] unless opts[:color].nil?
139
139
  ole_range.Interior.ColorIndex = workbook.color_if_modified unless workbook.color_if_modified.nil?
140
- if !JRUBY_BUG_RANGES
140
+ if !::JRUBY_BUG_RANGES
141
141
  ole_range.Value = value
142
142
  else
143
143
  address_r1c1 = ole_range.AddressLocal(true,true,XlR1C1)
@@ -1,3 +1,3 @@
1
1
  module RobustExcelOle
2
- VERSION = "1.14"
2
+ VERSION = "1.15"
3
3
  end