robust_excel_ole 0.2.2.1 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -128,13 +128,19 @@ Return the file name of the book with the absolute path that contains slash (/)
128
128
 
129
129
  === Access a sheet.
130
130
 
131
- A sheet object can be accessed with a Book#[] method.
131
+ A sheet object can be accessed with a Book#[] method via an integer number.
132
132
 
133
133
  sheet = book[0]
134
134
 
135
135
  Access a sheet object with the sheet name.
136
136
 
137
- book['Sheet1']
137
+ sheet = book['Sheet1']
138
+
139
+ Access sheet objects using the methods Book#each.
140
+
141
+ book.each do |sheet|
142
+ # do something with sheet
143
+ end
138
144
 
139
145
  === Access a row or a column.
140
146
 
@@ -146,11 +152,11 @@ A sheet object is enumerable. Use the methods Sheet#each_column, Sheet#each_row
146
152
  end
147
153
 
148
154
  sheet.each_row do |row|
149
- # do something with row_range
155
+ # do something with row
150
156
  end
151
157
 
152
- sheet.each_column do |column_range|
153
- # do something with column_range
158
+ sheet.each_column do |column|
159
+ # do something with column
154
160
  end
155
161
 
156
162
  === Access a cell.
@@ -164,7 +170,49 @@ Read a cell from a range object.
164
170
  row_range[0] => first cell in row_range
165
171
  column_range[1] => second cell in column_range
166
172
 
167
- = Examples
173
+ Read the value of a cell.
174
+
175
+ cell = sheet[0,0]
176
+ cell.value => value of the cell.
177
+
178
+ Write a cell
179
+
180
+ sheet[0,0] = "new_value"
181
+
182
+
183
+ === Access a range of a row or column.
184
+
185
+ Access a range of a row.
186
+
187
+ sheet.row_range(0) => first row
188
+ sheet.row_range(0, 0..2 ) => first three cells of the first row
189
+
190
+ Access a range of a column.
191
+
192
+ sheet.col_range(2) => third column
193
+ sheet.col_range(2, 0..1) => first two cells of the third column
194
+
195
+
196
+ === Add a sheet
197
+
198
+ Add a new sheet.
199
+
200
+ book.add_sheet
201
+
202
+ Add a new sheet with a name.
203
+
204
+ book.add_sheet(:as => 'new_sheet')
205
+
206
+ Add a new sheet with a name before another sheet.
207
+
208
+ book.add_sheet(:as => 'new_sheet2', :before => another_sheet)
209
+
210
+ Add a copy of a sheet with a name after another sheet.
211
+
212
+ book.add_sheet(sheet, :as => 'sheet_copy', :after => another_sheet)
213
+
214
+
215
+ === Examples
168
216
 
169
217
  Include robust_excel_ole.
170
218
 
@@ -176,11 +224,11 @@ Open a book.
176
224
 
177
225
  book = Book.open('simple.xls')
178
226
 
179
- Access a sheet.
227
+ Access a sheet via its name.
180
228
 
181
- sheet = book[0]
229
+ sheet = book['Sheet1']
182
230
 
183
- Change a cell.
231
+ Change the first cell.
184
232
 
185
233
  sheet[0,0] = "new"
186
234
 
@@ -188,7 +236,7 @@ Save the book.
188
236
 
189
237
  book.save
190
238
 
191
- Save it with a different name, and overwrite if a file with this name exists.
239
+ Save the book with a different name, and overwrite if a file with this name exists.
192
240
 
193
241
  book.save_as('different_simple.xls', :if_exists => :overwrite)
194
242
 
@@ -198,38 +246,38 @@ Close the book.
198
246
 
199
247
  === Example 2
200
248
 
201
- Open the book again in a new Excel application and make it visible.
249
+ Open the book in a new Excel application and make it visible.
202
250
 
203
- book1.open('simple.xls', :reuse => false, :visible => true)
251
+ book.open('simple.xls', :reuse => false, :visible => true)
204
252
 
205
- Add a sheet.
253
+ Add a copy of the first sheet after the second sheet.
206
254
 
207
- book1.add_sheet
255
+ book.add_sheet(book[0], :as => 'Sheet1_copy', :after => book[1])
208
256
 
209
- Open a new book with the same name in a new Excel. Leave the unsaved book in the old Excel.
257
+ Open a new book with the same name in a new Excel. Leave the book that contains unsaved changes in the old Excel.
210
258
 
211
- book2.open('simple.xls', :if_unsaved => :new_app)
259
+ new_book.open('simple.xls', :if_unsaved => :new_app)
212
260
 
213
- Access a sheet and Change a cell.
261
+ Access a sheet and change a cell.
214
262
 
215
- sheet = book2[0]
216
- sheet[0,0] = "another"
263
+ sheet = new_book[0]
264
+ sheet[1,1] = "another"
217
265
 
218
- Open another book with the same name in the running Excel. Close the unsaved book.
266
+ Open another book with the same name in the running Excel. The book that contains unsaved changes will be closed before.
219
267
 
220
- book3.open('simple.xls', :if_unsaved => :forget)
268
+ third_book.open('simple.xls', :if_unsaved => :forget)
221
269
 
222
270
  Add a sheet.
223
271
 
224
- book3.add_sheet
272
+ third_book.add_sheet
225
273
 
226
274
  Close the book without saving it.
227
275
 
228
- book3.close(:if_unsaved => :forget)
276
+ third_book.close(:if_unsaved => :forget)
229
277
 
230
278
  Close the first book and save it before.
231
279
 
232
- book1.close(:if_unsaved => :save)
280
+ book.close(:if_unsaved => :save)
233
281
 
234
282
  === Example 3
235
283
 
@@ -246,11 +294,11 @@ Change its cell.
246
294
  sheet = book2[0]
247
295
  sheet[0,0] = "new"
248
296
 
249
- Open a book with the same name in a different path. Save and close the unsaved book before.
297
+ Open a book with the same name in a different path. The old book that was modified will be saved and closed before.
250
298
 
251
299
  book3 = Book.open('simple.xls', :if_obstructed => :save)
252
300
 
253
- Open a book with the same name in a different path. The other book is closed, since it is saved.
301
+ Open a book with the same name in a different path. The other book will be closed, because it does not contain unsaved changes.
254
302
 
255
303
  book4 = Book.open('more/simple.xls', :if_obstructed => :close_if_unsaved)
256
304
 
@@ -0,0 +1,53 @@
1
+ # example_access_sheets_and_cells.rb:
2
+ # access sheets, print cells, rows, and columns of a sheet
3
+
4
+ require File.join(File.dirname(__FILE__), '../../lib/robust_excel_ole')
5
+ require File.join(File.dirname(__FILE__), '../../spec/helpers/create_temporary_dir')
6
+ require "fileutils"
7
+
8
+ include RobustExcelOle
9
+
10
+ Excel.close_all
11
+ begin
12
+ dir = create_tmpdir
13
+ simple_file = dir + 'simple.xls'
14
+ simple_save_file = dir + 'simple_save.xls'
15
+ File.delete simple_save_file rescue nil
16
+ book = Book.open(simple_file) # open a book
17
+ sheet = book[0] # access a sheet via integer
18
+ cell = sheet[0,0] # access the first cell
19
+ puts "1st cell: #{cell.value}" # put the value of the first cell
20
+ sheet[0,0] = "complex" # write a value into a cell
21
+ puts "new cell: #{sheet[0,0].value}"
22
+ puts "all cells:"
23
+ sheet.each do |cell| # access all cells
24
+ puts "#{cell.value}" # for each row: for every column: put the value of the cells
25
+ end
26
+
27
+ sheet_enum = proc do |enum_method| # put each cell, each row or each column
28
+ i = 0
29
+ sheet.send(enum_method) do |item|
30
+ i = i + 1
31
+ item_name =
32
+ case enum_method
33
+ when :each : "cell"
34
+ when :each_row : "row"
35
+ when :each_column : "column"
36
+ end
37
+ puts "#{item_name} #{i}: #{item.value}" # put values of the item of the sheet
38
+ end
39
+ end
40
+
41
+ sheet_enum[:each] # put cells
42
+ sheet_enum[:each_row] # put rows
43
+ sheet_enum[:each_column] # put columns
44
+
45
+ book.save # save the book
46
+ book.close # close the book
47
+
48
+ ensure
49
+ Excel.close_all
50
+ rm_tmp(dir)
51
+ end
52
+
53
+
@@ -0,0 +1,63 @@
1
+ # example_adding_sheets.rb:
2
+ # adding new and copied at various positions with various sheet names
3
+
4
+ require File.join(File.dirname(__FILE__), '../../lib/robust_excel_ole')
5
+ require File.join(File.dirname(__FILE__), '../../spec/helpers/create_temporary_dir')
6
+ require "fileutils"
7
+
8
+ include RobustExcelOle
9
+
10
+ Excel.close_all
11
+ begin
12
+ dir = create_tmpdir
13
+ simple_file = dir + 'simple.xls'
14
+ simple_save_file = dir + 'simple_save.xls'
15
+ File.delete simple_save_file rescue nil
16
+ @book = Book.open(simple_file) # open a book
17
+
18
+ def show_sheets
19
+ @book.each do |sheet| # access each sheet
20
+ puts "sheet name: #{sheet.name}" # put the sheet name
21
+ end
22
+ end
23
+
24
+ puts "sheets of the book:"
25
+ show_sheets
26
+
27
+ puts "adding a new sheet"
28
+ @book.add_sheet
29
+ show_sheets
30
+
31
+ puts "adding a new sheet with the name 'sheet_name'"
32
+ @book.add_sheet(:as => 'sheet_name')
33
+ show_sheets
34
+
35
+ puts "adding a copy of the 2nd sheet"
36
+ sheet = @book[1]
37
+ @book.add_sheet sheet
38
+ show_sheets
39
+
40
+ puts "adding a copy of the 2nd sheet and name it 'second_sheet_copy'"
41
+ @book.add_sheet(sheet, :as => 'second_sheet_copy')
42
+ show_sheets
43
+
44
+ puts "adding a new sheet after the 2nd sheet"
45
+ @book.add_sheet(:after => sheet)
46
+ show_sheets
47
+
48
+ puts "adding a copy of the 2nd sheet after the 2nd sheet"
49
+ @book.add_sheet(sheet, :after => sheet)
50
+ show_sheets
51
+
52
+ puts "adding a copy of the 4th sheet before the 7th sheet and name it 'sheet_copy'"
53
+ @book.add_sheet(@book[3], :as => 'sheet_copy', :after => @book[6])
54
+ show_sheets
55
+
56
+ @book.close(:if_unsaved => :forget) # close the book without saving it
57
+
58
+ ensure
59
+ Excel.close_all
60
+ rm_tmp(dir)
61
+ end
62
+
63
+
@@ -0,0 +1,38 @@
1
+ # example_ranges.rb:
2
+ # access row and column ranges of a sheet.
3
+
4
+ require File.join(File.dirname(__FILE__), '../../lib/robust_excel_ole')
5
+ require File.join(File.dirname(__FILE__), '../../spec/helpers/create_temporary_dir')
6
+ require "fileutils"
7
+
8
+ include RobustExcelOle
9
+
10
+ Excel.close_all
11
+ begin
12
+ dir = create_tmpdir
13
+ simple_file = dir + 'simple.xls'
14
+ simple_save_file = dir + 'simple_save.xls'
15
+ File.delete simple_save_file rescue nil
16
+ book = Book.open(simple_file) # open a book
17
+ sheet = book['Sheet1'] # access a sheet via the name
18
+ row_r = sheet.row_range(0) # access the whole range of the first row
19
+ col_r = sheet.col_range(0, 0..1) # access the first two cells of the range of the first column
20
+ cell = col_r[0] # access the first cell of these cells
21
+ puts "row range of 1st row: #{row_r.values}" # puts the values of the first row
22
+ puts "1st and 2nd cell of the 1st column : #{col_r.values}" # and the first two cells of the first column
23
+ puts "1st cell of these cells of the 1st columns: #{cell.value}" # and the first cell of the row range of the 1st row
24
+
25
+ i = 0
26
+ row_r.values.each do |value| # access the values of the first row
27
+ i = i + 1
28
+ puts "cell #{i} of the range of the 1st row: #{value}"
29
+ end
30
+
31
+ book.close
32
+
33
+ ensure
34
+ Excel.close_all
35
+ rm_tmp(dir)
36
+ end
37
+
38
+
@@ -1,3 +1,3 @@
1
1
  module RobustExcelOle
2
- VERSION = "0.2.2.1"
2
+ VERSION = "0.2.3"
3
3
  end
Binary file
data/spec/data/simple.xls CHANGED
Binary file
data/spec/sheet_spec.rb CHANGED
@@ -24,15 +24,20 @@ describe RobustExcelOle::Sheet do
24
24
  describe ".initialize" do
25
25
  context "when open sheet protected(with password is 'protect')" do
26
26
  before do
27
+ @key_sender = IO.popen 'ruby "' + File.join(File.dirname(__FILE__), '/helpers/key_sender.rb') + '" "Microsoft Office Excel" ' , "w"
28
+ @key_sender.puts "{p}{r}{o}{t}{e}{c}{t}{enter}"
27
29
  @book_protect = RobustExcelOle::Book.open(@dir + '/protected_sheet.xls', :visible => true, :read_only => true)
28
30
  @protected_sheet = @book_protect['protect']
29
31
  end
30
32
 
31
33
  after do
32
34
  @book_protect.close
35
+ @key_sender.close
33
36
  end
34
37
 
35
- it { @protected_sheet.ProtectContents.should be_true }
38
+ it "should be a protected sheet" do
39
+ @protected_sheet.ProtectContents.should be_true
40
+ end
36
41
 
37
42
  it "protected sheet can't be write" do
38
43
  expect { @protected_sheet[0,0] = 'write' }.to raise_error
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: robust_excel_ole
3
3
  version: !ruby/object:Gem::Version
4
- hash: 85
4
+ hash: 17
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 2
10
- - 1
11
- version: 0.2.2.1
9
+ - 3
10
+ version: 0.2.3
12
11
  platform: ruby
13
12
  authors:
14
13
  - traths
@@ -16,7 +15,7 @@ autorequire:
16
15
  bindir: bin
17
16
  cert_chain: []
18
17
 
19
- date: 2014-10-21 00:00:00 +02:00
18
+ date: 2014-10-22 00:00:00 +02:00
20
19
  default_executable:
21
20
  dependencies:
22
21
  - !ruby/object:Gem::Dependency
@@ -132,7 +131,9 @@ files:
132
131
  - LICENSE
133
132
  - README.rdoc
134
133
  - Rakefile
135
- - examples/edit_sheets/example_print_cells.rb
134
+ - examples/edit_sheets/example_access_sheets_and_cells.rb
135
+ - examples/edit_sheets/example_adding_sheets.rb
136
+ - examples/edit_sheets/example_ranges.rb
136
137
  - examples/open_save_close/example_control_to_excel.rb
137
138
  - examples/open_save_close/example_if_obstructed_closeifsaved.rb
138
139
  - examples/open_save_close/example_if_obstructed_forget.rb
@@ -1,43 +0,0 @@
1
- # example 1: open a book, print the cells, rows, and columns of a sheet
2
-
3
- require File.join(File.dirname(__FILE__), '../../lib/robust_excel_ole')
4
- require File.join(File.dirname(__FILE__), '../../spec/helpers/create_temporary_dir')
5
- require "fileutils"
6
-
7
- include RobustExcelOle
8
-
9
- Excel.close_all
10
- begin
11
- dir = create_tmpdir
12
- simple_file = dir + 'simple.xls'
13
- simple_save_file = dir + 'simple_save.xls'
14
- File.delete simple_save_file rescue nil
15
- book = Book.open(simple_file)
16
- sheet = book[0]
17
- cell = sheet[0,0]
18
-
19
- sheet_enum = proc do |enum_method|
20
- i = 0
21
- sheet.send(enum_method) do |cell|
22
- i = i + 1
23
- puts "sheet.#{enum_method} #{i}: #{cell.value}"
24
- end
25
- end
26
-
27
- sheet_enum[:each]
28
- sheet_enum[:each_row]
29
- sheet_enum[:each_column]
30
-
31
- col_r = sheet.col_range(0,1..2).values
32
- row_r = sheet.row_range(0,1..2).values
33
- puts "row range of 1st row, 1..2: #{row_r}"
34
- puts "column range of 1st column, 1..2: #{col_r}"
35
- sheet[0,0] = "complex"
36
- book.save
37
- book.close
38
- ensure
39
- Excel.close_all
40
- rm_tmp(dir)
41
- end
42
-
43
-