robust_excel_ole 1.1.3 → 1.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/README_detail.rdoc CHANGED
@@ -280,6 +280,12 @@ This method ensures keeping the save status of the workbook
280
280
  # some reading or modifying
281
281
  end
282
282
 
283
+ === Setting options
284
+
285
+ You can set options, e.g. with
286
+
287
+ book.for_this_workbook(:visible => true, :read_only => true)
288
+
283
289
  === Checking whether the workbook is alive.
284
290
 
285
291
  This method finds out whether the Excel workbook that is referenced by the Workbook object responds to methods.
@@ -536,17 +542,17 @@ You can turn off and off DisplayAlerts in a block.
536
542
 
537
543
  === Making all workbooks visible or invisible
538
544
 
539
- excel1.workbooks_visible true
545
+ excel.workbooks_visible true
540
546
 
541
- excel1.workbooks_visible false
547
+ excel.workbooks_visible false
542
548
 
543
549
  === Setting options
544
550
 
545
551
  You can set all options in a given Excel instance
546
552
 
547
- e1 = Excel.current
553
+ excel = Excel.current
548
554
 
549
- e1.set_options(:visible => true, :displayalerts => true)
555
+ excel.for_this_instance(:visible => true, :displayalerts => true)
550
556
 
551
557
  === Bringing an Excel instance to the foreground
552
558
 
@@ -0,0 +1,5 @@
1
+ = RobustExcelOle
2
+
3
+ === Development
4
+
5
+ The tests of RobustExcelOle are optimised with help of the rcov tool.
data/README_excel.rdoc ADDED
@@ -0,0 +1,145 @@
1
+ = RobustExcelOle
2
+
3
+ == Creating, using and closing Excel instances
4
+
5
+ RobustExcelOle enables simultanously running Excel instances. An Excel application is represented by an Excel object.
6
+
7
+ === Creating and reusing an Excel instance.
8
+
9
+ You can start a new Excel instance by
10
+
11
+ excel1 = Excel.create
12
+
13
+ or
14
+
15
+ excel1 = Excel.new(:reuse => false)
16
+
17
+ In case you want to reuse an already running Excel instance, write
18
+
19
+ excel2 = Excel.current
20
+
21
+ or
22
+
23
+ excel2 = Excel.new(:reuse => true)
24
+
25
+ Options of the methods +create+ and +new+ are +:reuse+ (+true+, +false+), +:visible+ (+true+, +false+), +:displayalerts+ (+true+, +false+, +:if_visible+), +:calculation+ (+:manual+, +:automatic+, +nil+) and +:screenupdating+ (+true+, +false+).
26
+
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
+
29
+ You can also promote an Excel instance represented as WIN32OLE object to an Excel object.
30
+
31
+ excel = Excel.new(win32ole_object, :visible => true)
32
+
33
+ === Setting options
34
+
35
+ You can set all options in a given Excel instance
36
+
37
+ excel = Excel.current
38
+
39
+ excel.for_this_instance(:visible => true, :displayalerts => true, :calculation => :manual)
40
+
41
+ === Making an Excel visible or invisible
42
+
43
+ You can create a new Excel instance and make it visible.
44
+
45
+ excel1 = Excel.create(:visible => true)
46
+
47
+ or
48
+
49
+ excel1 = Excel.new(:reuse => false, :visible => true)
50
+
51
+ or
52
+
53
+ excel1 = Excel.create
54
+ excel1.visible = true
55
+
56
+
57
+ === Enabling or disabling DisplayAlerts
58
+
59
+ You can enable DisplayAlerts with, e.g.
60
+
61
+ excel1 = Excel.new(:reuse => true, :displayalerts => true)
62
+
63
+ or
64
+
65
+ excel1 = Excel.current
66
+ excel1.displayalerts = true
67
+
68
+ and turn DisplayAlerts off with
69
+
70
+ excel1.displayalerts = false
71
+
72
+ You can turn off and off DisplayAlerts in a block.
73
+
74
+ excel = Excel.create
75
+ excel.with_displayalerts false do
76
+ # do something
77
+ end
78
+
79
+ === Setting Calculation mode.
80
+
81
+ You can set the calculation mode of an Excel instance to manual or automatic.
82
+
83
+ excel.calculation = :manual
84
+
85
+ You can do it in a block:
86
+
87
+ excel = Excel.create
88
+ book = Workbook.open('workbook.xls')
89
+ excel.with_calculation(:manual) do
90
+ # do something
91
+ end
92
+
93
+
94
+
95
+ === Setting options for all workbooks
96
+
97
+ You can set options for all workbooks of an Excel instance.
98
+
99
+ excel.for_all_workooks(:visible => true, :read_only => true)
100
+
101
+ === Bringing an Excel instance to the foreground
102
+
103
+ excel1.focus
104
+
105
+ === Closing an Excel
106
+
107
+ excel = Excel.current
108
+ excel.close
109
+
110
+ The method +close has the option +:if_unsaved+ with the values +:raise+, +:save+, +:forget+ and +:alert+.
111
+
112
+ For example, if you want to close an Excel instance and save unsaved workbooks, use
113
+
114
+ excel.close(:if_unsaved => :save)
115
+
116
+ === Recreating an Excel instance
117
+
118
+ Closed Excel instances can also be reopened. This includes reopening all workbooks that were open in that Excel instance.
119
+
120
+ excel.close
121
+ excel.recreate
122
+
123
+ The options are :reopen_workbooks, :visible and :displayalerts.
124
+
125
+ excel.recreate(:reopen_workbooks => true, :visible => true, :displayalerts => true)
126
+
127
+ === Providing Excel instances
128
+
129
+ Providing all Excel instances (opened via RobustExcelOle) as objects of the class Excel
130
+
131
+ Excel.excel_processes
132
+
133
+ === Closing all Excel instances
134
+
135
+ Excel.close_all
136
+
137
+ This method has the option +:if_unsaved+ as described above. For example, if you want to close all Excel instances containing saved workbooks and raise an error for Excel instances with unsaved workbooks, use
138
+
139
+ Excel.close_all(:if_unsaved => :raise)
140
+
141
+ === Terminating all Excel processes
142
+
143
+ Excel.kill_all
144
+
145
+ This method kills all Excel instances no matter whether they contain unsaved workbooks.
data/README_open.rdoc ADDED
@@ -0,0 +1,202 @@
1
+ = RobustExcelOle
2
+
3
+ === Opening and unobtrusively modifying a workbook.
4
+
5
+ If you want to open a workbook, use
6
+
7
+ book = Workbook.open('workbook.xls')
8
+
9
+ You can also open a workbook with a block.
10
+ The semantics is similar to, e.g., +File.open+.
11
+
12
+ Workbook.open('workbook.xls') do |book|
13
+ # do something
14
+ end
15
+
16
+ The options are the following:
17
+
18
+ +:default+:: if the workbook was already open, then use the properties of this workbook.otherwise use the properties stated in +:default+
19
+
20
+ +:force+:: no matter whether the workbook was open before, use the properties stated in +:force+
21
+
22
+ +:excel+ and +:visible+ are options stated in +:default+ or +:force+
23
+
24
+ +:excel+:: specifies the Excel instance.
25
+
26
+ +:visible+:: makes the workbook visible or invisible
27
+
28
+ +:if_unsaved+:: specify behaviour if the workbook was unsaved (default: +new_excel+)
29
+
30
+ +:if_obstructed+:: specify behaviour if the workbook is blocked by another book (default: +new_excel+)
31
+
32
+ +:read_only+:: open in read-only mode (default: +false+)
33
+
34
+ +:check_compatibility:: check compatibility when saving
35
+
36
+ +:calculation+:: forces the calculation mode to be manual (:manual) or automatic (:automatic)
37
+
38
+ +:if_absent+:: specify behaviour if the workbook with the given file name does not exist if the workbook does not exist (default: +create+)
39
+
40
+ You can use the following abbreviations:
41
+
42
+ +:f+:: +:force+
43
+ +:d+:: +:default+
44
+ +:e+:: +:excel+
45
+ +:v+:: +:visible+
46
+
47
+ The option +:excel+ :
48
+
49
+ Valid values are : +:current+ (or +:active+ or +:reuse+), +:new+, or a given Excel instance (default: +:current).
50
+
51
+ The option +:if_unsaved+ :
52
+
53
+ If a workbook contains unsaved changes and a new workbook with the same file name shall be opened, then
54
+
55
+ +:raise+:: Raise an exeption. Don't open the workbook.
56
+ +:accept+:: Let the unsaved workbook open.
57
+ +:forget+:: Discard any changes and reopen the workbook.
58
+ +:new_excel+:: Open the new workbook in a new Excel instance
59
+ +:alert+:: Give control to Excel.
60
+
61
+ The option +:if_obstructed+ :
62
+
63
+ If a workbook is open and a new workbook with same name and a different path is open, then
64
+
65
+ +:raise+:: Raise an exception. Don't open the workbook.
66
+ +:forget+:: Close the old workbook, open the new workbook.
67
+ +:save+:: Save the old workbook, close it, open the new workbook
68
+ +:close_if_saved+:: Close the old workbook and open the new workbook, if the old workbook is saved, otherwise raise an exception.
69
+ +:new_excel+:: Open the new workbook in a new Excel instance.
70
+
71
+ The option +:if_absent :
72
+
73
+ If the Excel file does not exists, then
74
+
75
+ +:create+:: Create a new Excel file
76
+ +:raise+:: Raise an exception.
77
+
78
+ Here are a few examples:
79
+
80
+ If you want to open a workbook that was not opened before, or reopen a workbook that was open in an Excel instance that is now closed, in the current (active) Excel instance, then use
81
+
82
+ Workbook.open('workbook.xls', :default => {:excel => :current})
83
+
84
+ or
85
+
86
+ Workbook.open('workbook.xls')
87
+
88
+ In case you want to open such a workbook in a new Excel instance, then use
89
+
90
+ Workbook.open('workbook.xls', :default => {:excel => :new})
91
+
92
+ If you want to open a workbook in a new Excel instance, no matter if it was opened before, you can write
93
+
94
+ Workbook.open('workbook.xls', :force => {:excel => :new})
95
+
96
+ For simplicity, you can also leave out the +:force+ option (but not the +:default+ option).
97
+
98
+ Workbook.open('workbook.xls', :excel => :new)
99
+
100
+ You can also specify an Excel instance
101
+
102
+ excel1 = Excel.create
103
+ # do something
104
+ Workbook.open('workbook.xls', :excel => excel1)
105
+
106
+ If you want to open the workbook and make its window visible, then use
107
+
108
+ book = Workbook.open('workbook.xls', :visible => true)
109
+
110
+ You can combine options, e.g.
111
+
112
+ Workbook.open('workbook.xls', :excel => :new, :visible => true, :default => {:excel => excel1})
113
+
114
+ You can use the abbreviations, e.g. in this case
115
+
116
+ Workbook.open('workbook.xls', :e => :new, :v => true, :d => {:e => excel1})
117
+
118
+ If a workbook contains unsaved changes and a workbook with the same filename shall be opened, then the option +:if_unsaved+ manages this conflict. For example, if the workbook with the unsaved changes shall remain open, you can use
119
+
120
+ book = Workbook.open('workbook.xls', :if_unsaved => :accept)
121
+
122
+ If a workbook is open and a workbook with the same name but in different path shall be opened, i.e. the first workbook blocks opening the other workbook, then the option +:if_obstructed+ handles this situation, e.g.
123
+
124
+ book = Workbook.open('path/workbook.xls', :if_obstructed => :forget)
125
+
126
+ Remarks:
127
+
128
+ Opening linked workbooks for EXCEL 2007 is supported
129
+
130
+ Doing updating links seems to be dependent on calculation mode: updates happen, if the calcultion mode is automatic, and does not happen, if calculation mode is manual.
131
+
132
+ === Reopening a workbook.
133
+
134
+ A special feature of RobustExcelOle is that it allows to reopen workbooks after closing them.
135
+
136
+ book = Workbook.open('workbook.xls')
137
+ book.close
138
+ book.reopen
139
+
140
+ The closed workbook is now alive again, i.e. is open and responds to Excel methods.
141
+
142
+ This feature is a result of providing identity transparence and storing the file name.
143
+
144
+ === The Workbook objects and transperence identity
145
+
146
+ An Excel file (or workbook) is represented by a Workbook object. A Workbook object is defined by the full name of the workbook and the Excel instance in which it is opened. RobustExcelOle aims to ensure identity transperency.
147
+ Identity transparence means that the same Workbook objects refer to the same Excel files, and vice versa.
148
+ In other words, a Workbook objects is a proxy of an Excel file.
149
+
150
+ === Promoting a workbook to a Workbook object
151
+
152
+ A Workbook object can be created when giving an Excel workbook.
153
+
154
+ book = Workbook.new(win32ole_workbook)
155
+
156
+
157
+ === Unobtrusively modifying a workbook
158
+
159
+ The method +unobtrusively+ enables the user to read or modify a workbook, no matter if it is open in some Excel instance, if it is saved or unsaved, and if it is writable or not. When opening a workbook unobtrusively, its status remains unchanged. This status includes, whether the workbook is opened or closed, saved or unsaved, readonly or writable, visible or invisible, calculation mode is automatic or manual, and checking compatibility is turned on or off.
160
+
161
+ One option chooses the Excel instance in which a closed workbook is opened. The option +:current+ (or +:active+, or +:reuse) (default) indicates that the closed workbook is opened in the Excel instance of the workbook, if it exists, or that another Excel instance is reused. Moreover, an Excel instance can be given directly where to open the closed workbook.
162
+
163
+ Options are the following:
164
+
165
+ +: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
166
+
167
+ +:read_only+:: Open the workbook unobtrusively for reading only (default: false)
168
+ +:readonly_excel+:: if the workbook is opened only as ReadOnly and shall be modified, then
169
+ true: close it and open it as writable in the excel instance where it was open so far
170
+ false (default) open it as writable in another running excel instance, if it exists, otherwise open in a new excel instance
171
+ +:keep_open+:: let the workbook open after unobtrusively opening (default: false)
172
+
173
+ Workbook.unobtrusively('workbook.xls') do |book|
174
+ # some modification
175
+ sheet = book[0]
176
+ sheet[1,1] = "c"
177
+ end
178
+
179
+ The methods +for_reading+ and +for_modifying+ indicate unobtrusively reading or modifying.
180
+
181
+ Workbook.for_modifying('workbook.xls') do |book|
182
+ # some modification
183
+ sheet = book[0]
184
+ sheet[1,1] = "c"
185
+ end
186
+
187
+ === Retaining the saved-status
188
+
189
+ This method ensures keeping the save status of the workbook
190
+
191
+ book = Workbook.open('workbook.xls')
192
+ book.retain_saved do
193
+ # some reading or modifying
194
+ end
195
+
196
+ === Checking whether the workbook is alive.
197
+
198
+ This method finds out whether the Excel workbook that is referenced by the Workbook object responds to methods.
199
+
200
+ book.alive?
201
+ # => true
202
+
@@ -0,0 +1,134 @@
1
+ = RobustExcelOle
2
+
3
+ == Reading and writing the contents of ranges and cells
4
+
5
+ RobustExcelOle enables to read and write the contents of ranges and cells in Excel files. This can be done from a Sheet, a Book or an Excel object representing a Worksheet, Workbook, or Application object in VBA.
6
+
7
+ === Reading and writing the contents of a named range in a workbook.
8
+
9
+ Assume you have opened a workbook:
10
+
11
+ book = Workbook.open('workbook.xls', :visible => true)
12
+
13
+ You can get the contents of a range with a defined name with help of the method [] or nameval:
14
+
15
+ book["name"]
16
+ => "value"
17
+
18
+ or
19
+
20
+ book.nameval("name")
21
+ => "value"
22
+
23
+ Using nameval, via the option :default you can provide a value that is returned when the name cannot be found or some other error would occur.
24
+
25
+ book.namval("name", :default => "default_value")
26
+
27
+ You can set the contents of a range with
28
+
29
+ book["name"] = "new_value"
30
+
31
+ or
32
+
33
+ book.set_nameval("name", "new_value")
34
+
35
+ The method []= colors the written cell. You can specify the color of a changed range via the method set_nameval:
36
+
37
+ book.set_nameval("name", "new_value", :color => 4)
38
+
39
+ Similarly, the contents of a named range can be read and modified in a worksheet
40
+
41
+ sheet = book.sheet(1)
42
+
43
+ sheet["name"]
44
+ => value
45
+
46
+ sheet["name"] = "new_value"
47
+
48
+ or an application object:
49
+
50
+ excel = book.excel
51
+
52
+ excel["name"]
53
+ => "value"
54
+
55
+ excel["name"] = "new_value"
56
+
57
+ When saving, the written cells get discolored when using the option :discoloring
58
+
59
+ book.save(:discoloring => true)
60
+
61
+ or
62
+
63
+ book.save_as('workbook.xls', :discoloring => true)
64
+
65
+ === Reading and writing the contents of a range with a locally defined name
66
+
67
+ The contents of locally defined ranges can be read
68
+
69
+ sheet.rangeval("name")
70
+ => "value"
71
+
72
+ or
73
+
74
+ excel.rangeval("name")
75
+ => "value"
76
+
77
+ and be modified:
78
+
79
+ sheet.set_rangeval("name", "value")
80
+
81
+ or
82
+
83
+ excel.set_rangeval("name", "value")
84
+
85
+ Similarly to nameval, you can provide a default value that is returned when ocurring an error.
86
+
87
+ sheet.rangeval("name", :default => "default_value")
88
+
89
+ === Accessing a cell
90
+
91
+ You can read and write a cell from a sheet object by providing the row and the column. For example, the following lines provide and set the value of the first cell (first row, first column):
92
+
93
+ sheet[1,1].value
94
+ => "foo
95
+
96
+ sheet[1,1] = "new_value"
97
+
98
+ === Accessing rows and columns
99
+
100
+ The methods Sheet#each, Sheet#each_row and Sheet#each_column enable to access each cell, row and column, respectively.
101
+
102
+ sheet.each do |cell|
103
+ # do something with cell
104
+ # read every row, every column
105
+ end
106
+
107
+ sheet.each_row do |row|
108
+ # do something with row
109
+ end
110
+
111
+ sheet.each_column do |column|
112
+ # do something with column
113
+ end
114
+
115
+ You access a range of a row by giving the number of the row, and optionally, the range of the cell numbers.
116
+
117
+ sheet.row_range(1) # => first row
118
+ sheet.row_range(1, 1..3 ) # => first three cells of the first row
119
+
120
+ Simarly you can access a range of a column.
121
+
122
+ sheet.col_range(3) # => third column
123
+ sheet.col_range(3, 1..2) # => first two cells of the third column
124
+
125
+ Within a row or column range you can access a certain cell.
126
+
127
+ row_range[1] # => first cell in row_range
128
+ column_range[2] # => second cell in column_range
129
+
130
+ === Naming a cell
131
+
132
+ You can (re-) name a cell range.
133
+
134
+ sheet.set_name(1,1,"name")