robust_excel_ole 1.17 → 1.18.4

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.
@@ -0,0 +1,36 @@
1
+
2
+ require 'spreadsheet'
3
+
4
+ start_time = Time.now
5
+
6
+ # ============================================
7
+ # =========== Read Example ===============
8
+ # ============================================
9
+
10
+ # Note: spreadsheet only supports .xls files (not .xlsx)
11
+ workbook = Spreadsheet.open './sample_excel_files/xls_25000_rows.xls'
12
+
13
+ worksheets = workbook.worksheets
14
+ puts "Found #{worksheets.count} worksheets"
15
+
16
+
17
+ worksheets.each do |worksheet|
18
+ puts "Reading: #{worksheet.name}"
19
+ num_rows = 0
20
+
21
+ worksheet.rows.each do |row|
22
+ row_cells = row.to_a.map{ |v| v.methods.include?(:value) ? v.value : v }
23
+ num_rows += 1
24
+
25
+ # uncomment to print out row values
26
+ # puts row_cells.join " "
27
+ end
28
+ puts "Read #{num_rows} rows"
29
+ end
30
+
31
+ end_time = Time.now
32
+ running_time = end_time - start_time
33
+ puts "time: #{running_time} sec."
34
+
35
+
36
+ puts 'Done'
@@ -0,0 +1,3 @@
1
+ @echo off
2
+
3
+ jirb -f -r ../lib/robust_excel_ole -r ../lib/jreo_console.rb
data/bin/reo ADDED
@@ -0,0 +1,3 @@
1
+ @echo off
2
+
3
+ irb -f -r ../lib/robust_excel_ole -r ../lib/reo_console.rb
@@ -124,6 +124,12 @@ or, with a block,
124
124
 
125
125
  excel.each_workbook(:visible => true) {|w| puts w}
126
126
 
127
+ === Accessing the active workbook
128
+
129
+ You can operate on the active workbook with help of the method Workbook#active_workbook, e.g.
130
+
131
+ workbook = excel.active_workbook
132
+
127
133
  === Bringing an Excel instance to the foreground
128
134
 
129
135
  excel1.focus
@@ -377,37 +377,7 @@ or
377
377
 
378
378
  worksheet.set_cellval(1,1,"new_value")
379
379
 
380
- === Accessing rows and columns
381
380
 
382
- The methods Worksheet#each, Worksheet#each_row and Worksheet#each_column enable to access each cell, row and column, respectively.
383
-
384
- worksheet.each do |cell|
385
- # do something with cell
386
- # read every row, every column
387
- end
388
-
389
- worksheet.each_row do |row|
390
- # do something with row
391
- end
392
-
393
- worksheet.each_column do |column|
394
- # do something with column
395
- end
396
-
397
- You access a range of a row by giving the number of the row, and optionally, the range of the cell numbers.
398
-
399
- worksheet.row_range(1) # => first row
400
- worksheet.row_range(1, 1..3 ) # => first three cells of the first row
401
-
402
- Simarly you can access a range of a column.
403
-
404
- worksheet.col_range(3) # => third column
405
- worksheet.col_range(3, 1..2) # => first two cells of the third column
406
-
407
- Within a row or column range you can access a certain cell.
408
-
409
- row_range[1] # => first cell in row_range
410
- column_range[2] # => second cell in column_range
411
381
 
412
382
  == Code
413
383
 
@@ -75,6 +75,78 @@ If you want to copy a worksheet, if a worksheet +sheet+ is given, and add an emp
75
75
 
76
76
  Note, that running in jruby, due to some restrictions of jruby, there is a workaround when adding or copy a worksheet at the end (appending): the last worksheet is being copied and deleted afterwards, in order to serve as a dummy worksheet. This may cause a different behaviour.
77
77
 
78
+ === Accessing rows and columns
79
+
80
+ The methods Worksheet#each, Worksheet#each_row and Worksheet#each_column enable to access each cell, row and column, respectively.
81
+
82
+ worksheet.each do |cell|
83
+ # do something with cell
84
+ # read every row, every column
85
+ end
86
+
87
+ worksheet.each_row do |row|
88
+ # do something with row
89
+ end
90
+
91
+ worksheet.each_column do |column|
92
+ # do something with column
93
+ end
94
+
95
+ The method Worksheet#values yields all cell values of the used range of the worksheet into a 2-dimensional array. For example:
96
+
97
+ worksheet.values
98
+ => [["foo", "workbook", "sheet1"], ["foo", nil, "foobaaa"], ["matz", "is", "nice"]]
99
+
100
+ The method Worksheet#each_rowvalue provides enable to access the values of each row.
101
+
102
+ worksheet.each_rowvalue do |row_values|
103
+ # do something with the row_values
104
+ end
105
+
106
+ You access a range of a row by giving the number of the row, and optionally, the range of the cell numbers.
107
+
108
+ worksheet.row_range(1) # => first row
109
+ worksheet.row_range(1, 1..3 ) # => first three cells of the first row
110
+
111
+ Reading the values is enabled with help of #values:
112
+
113
+ worksheet.row_range(1).values
114
+
115
+ Simarly you can access a range of a column.
116
+
117
+ worksheet.col_range(3) # => third column
118
+ worksheet.col_range(3, 1..2) # => first two cells of the third column
119
+
120
+ Within a row or column range you can access a certain cell.
121
+
122
+ row_range[1] # => first cell in row_range
123
+ column_range[2] # => second cell in column_range
124
+
125
+ === Deleting and inserting rows and columns
126
+
127
+ As mentioned above, VBA methods can be applied to the RobustExcelOle objects, e.g. when deleting or inserting rows and columns.
128
+
129
+ row1 = worksheet.row_range(1)
130
+ row1.Delete
131
+
132
+ row1.Insert(XlShiftDown,XlFormatFromLeftOrAbove)
133
+
134
+ col1 = worksheet.col_range(1)
135
+ col1.Insert
136
+
137
+ === Getting and setting row height and column width
138
+
139
+ row_hight = row1.RowHight
140
+ row1.RowHeight = row_hight * 2
141
+
142
+ col_width = col1.ColumnWidth
143
+ col1.ColumnWidth = col_width * 2
144
+
145
+ === Vertical and horizontal alignment of contents of rows
146
+
147
+ row1.VerticalAlignment = XlVAlignCenter
148
+ row1.HorizontalAlignment = XlHAlignLeft
149
+
78
150
  == Code
79
151
 
80
152
  worksheet.rb[https://github.com/Thomas008/robust_excel_ole/blob/master/lib/robust_excel_ole/worksheet.rb]
@@ -0,0 +1,27 @@
1
+ #require 'robust_excel_ole'
2
+
3
+ #workbook = Workbook.open './sample_excel_files/xlsx_500_rows.xlsx'
4
+
5
+ require_relative '../lib/robust_excel_ole'
6
+
7
+ include RobustExcelOle
8
+
9
+ workbook = Workbook.open './../spec/data/workbook.xls'
10
+
11
+ puts "Found #{workbook.worksheets_count} worksheets"
12
+
13
+ workbook.each do |worksheet|
14
+ puts "Reading: #{worksheet.name}"
15
+ num_rows = 0
16
+
17
+ worksheet.each do |row|
18
+ row_cells = row.map{ |cell| cell.value }
19
+ num_rows += 1
20
+
21
+ # uncomment to print out row values
22
+ # puts row_cells.join " "
23
+ end
24
+ puts "Read #{num_rows} rows"
25
+ end
26
+
27
+ puts 'Done'
@@ -12,8 +12,8 @@ require File.join(File.dirname(__FILE__), 'robust_excel_ole/excel')
12
12
  require File.join(File.dirname(__FILE__), 'robust_excel_ole/bookstore')
13
13
  require File.join(File.dirname(__FILE__), 'robust_excel_ole/workbook')
14
14
  require File.join(File.dirname(__FILE__), 'robust_excel_ole/worksheet')
15
- require File.join(File.dirname(__FILE__), 'robust_excel_ole/range')
16
15
  require File.join(File.dirname(__FILE__), 'robust_excel_ole/cell')
16
+ require File.join(File.dirname(__FILE__), 'robust_excel_ole/range')
17
17
  require File.join(File.dirname(__FILE__), 'robust_excel_ole/cygwin') if RUBY_PLATFORM =~ /cygwin/
18
18
  require File.join(File.dirname(__FILE__), 'robust_excel_ole/version')
19
19
 
@@ -1,13 +1,15 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
+ require File.join(File.dirname(__FILE__), './range')
4
+
3
5
  module RobustExcelOle
4
6
 
5
7
  class Cell < Range
6
- attr_reader :ole_cell
8
+ #attr_reader :ole_cell
7
9
 
8
- def initialize(win32_cell)
9
- @ole_cell = win32_cell.MergeCells ? win32_cell.MergeArea.Item(1,1) : win32_cell
10
+ def initialize(win32_cell, worksheet)
10
11
  super
12
+ ole_cell
11
13
  end
12
14
 
13
15
  def v
@@ -18,6 +20,10 @@ module RobustExcelOle
18
20
  self.Value = value
19
21
  end
20
22
 
23
+ def ole_cell
24
+ @ole_range = @ole_range.MergeArea.Item(1,1) if @ole_range.MergeCells
25
+ end
26
+
21
27
  private
22
28
 
23
29
  # @private
@@ -25,13 +31,15 @@ module RobustExcelOle
25
31
  if name.to_s[0,1] =~ /[A-Z]/
26
32
  if ::ERRORMESSAGE_JRUBY_BUG
27
33
  begin
28
- @ole_cell.send(name, *args)
34
+ #@ole_cell.send(name, *args)
35
+ @ole_range.send(name, *args)
29
36
  rescue Java::OrgRacobCom::ComFailException
30
37
  raise VBAMethodMissingError, "unknown VBA property or method #{name.inspect}"
31
38
  end
32
39
  else
33
40
  begin
34
- @ole_cell.send(name, *args)
41
+ #@ole_cell.send(name, *args)
42
+ @ole_range.send(name, *args)
35
43
  rescue NoMethodError
36
44
  raise VBAMethodMissingError, "unknown VBA property or method #{name.inspect}"
37
45
  end
@@ -3,8 +3,6 @@
3
3
  require 'weakref'
4
4
  require 'Win32API'
5
5
 
6
-
7
-
8
6
  def ka
9
7
  Excel.kill_all
10
8
  end
@@ -709,8 +707,7 @@ module RobustExcelOle
709
707
  # @private
710
708
  # returns active workbook
711
709
  def workbook
712
- return @workbook unless @workbook.nil?
713
- @workbook = workbook_class.new(@ole_excel.ActiveWorkbook)
710
+ @workbook ||= workbook_class.new(@ole_excel.ActiveWorkbook) if @ole_excel.Workbooks.Count > 0
714
711
  end
715
712
 
716
713
  alias_method :active_workbook, :workbook
@@ -1,4 +1,5 @@
1
1
  # -*- coding: utf-8 -*-
2
+
2
3
  module RobustExcelOle
3
4
 
4
5
  # This class essentially wraps a Win32Ole Range object.
@@ -11,17 +12,33 @@ module RobustExcelOle
11
12
  attr_reader :ole_range
12
13
  attr_reader :worksheet
13
14
 
14
- def initialize(win32_range)
15
+ def initialize(win32_range, worksheet = nil)
15
16
  @ole_range = win32_range
16
- @worksheet = worksheet_class.new(self.Parent)
17
+ @worksheet = worksheet ? worksheet : worksheet_class.new(self.Parent)
18
+ #@worksheet = worksheet_class.new(self.Parent)
17
19
  end
18
20
 
19
21
  def each
20
- @ole_range.each do |row_or_column|
21
- yield RobustExcelOle::Cell.new(row_or_column)
22
+ @ole_range.each_with_index do |ole_cell, index|
23
+ yield cell(index){ole_cell}
22
24
  end
23
25
  end
24
26
 
27
+ def [] index
28
+ cell(index) {
29
+ @ole_range.Cells.Item(index + 1)
30
+ }
31
+ end
32
+
33
+ private
34
+
35
+ def cell(index)
36
+ @cells ||= []
37
+ @cells[index + 1] ||= RobustExcelOle::Cell.new(yield,@worksheet)
38
+ end
39
+
40
+ public
41
+
25
42
  # returns flat array of the values of a given range
26
43
  # @params [Range] a range
27
44
  # @returns [Array] the values
@@ -82,10 +99,8 @@ module RobustExcelOle
82
99
  end
83
100
  end
84
101
 
85
- def [] index
86
- @cells = []
87
- @cells[index + 1] = RobustExcelOle::Cell.new(@ole_range.Cells.Item(index + 1))
88
- end
102
+ alias_method :value, :v
103
+ alias_method :value=, :v=
89
104
 
90
105
  # copies a range
91
106
  # @params [Address or Address-Array] address or upper left position of the destination range
@@ -22,12 +22,13 @@ module RobustExcelOle
22
22
  raise
23
23
  end
24
24
  ole_range = name_obj.RefersToRange
25
+ worksheet = self if self.is_a?(Worksheet)
25
26
  value = begin
26
27
  #name_obj.RefersToRange.Value
27
28
  if !::RANGES_JRUBY_BUG
28
29
  ole_range.Value
29
30
  else
30
- values = RobustExcelOle::Range.new(ole_range).v
31
+ values = RobustExcelOle::Range.new(ole_range, worksheet).v
31
32
  (values.size==1 && values.first.size==1) ? values.first.first : values
32
33
  end
33
34
  rescue WIN32OLERuntimeError, Java::OrgRacobCom::ComFailException
@@ -42,7 +43,7 @@ module RobustExcelOle
42
43
  if !::RANGES_JRUBY_BUG
43
44
  ole_range.Value
44
45
  else
45
- values = RobustExcelOle::Range.new(ole_range).v
46
+ values = RobustExcelOle::Range.new(ole_range, worksheet).v
46
47
  (values.size==1 && values.first.size==1) ? values.first.first : values
47
48
  end
48
49
  rescue WIN32OLERuntimeError, Java::OrgRacobCom::ComFailException
@@ -105,11 +106,12 @@ module RobustExcelOle
105
106
  raise NameNotFound, "name #{name.inspect} not in #{self.inspect}"
106
107
  end
107
108
  begin
109
+ worksheet = self if self.is_a?(Worksheet)
108
110
  #value = ole_range.Value
109
111
  value = if !::RANGES_JRUBY_BUG
110
112
  ole_range.Value
111
113
  else
112
- values = RobustExcelOle::Range.new(ole_range).v
114
+ values = RobustExcelOle::Range.new(ole_range, worksheet).v
113
115
  (values.size==1 && values.first.size==1) ? values.first.first : values
114
116
  end
115
117
  rescue WIN32OLERuntimeError, Java::OrgRacobCom::ComFailException
@@ -180,10 +182,11 @@ module RobustExcelOle
180
182
  # @return [Range] a range
181
183
  def range(name_or_address, address2 = :__not_provided)
182
184
  begin
185
+ worksheet = self if self.is_a?(Worksheet)
183
186
  if address2 == :__not_provided
184
187
  range = if name_or_address.is_a?(String)
185
188
  begin
186
- RobustExcelOle::Range.new(name_object(name_or_address).RefersToRange)
189
+ RobustExcelOle::Range.new(name_object(name_or_address).RefersToRange, worksheet)
187
190
  rescue NameNotFound
188
191
  nil
189
192
  end
@@ -193,7 +196,7 @@ module RobustExcelOle
193
196
  address = name_or_address
194
197
  address = [name_or_address,address2] unless address2 == :__not_provided
195
198
  self.Names.Add('__dummy001',nil,true,nil,nil,nil,nil,nil,nil,'=' + address_tool.as_r1c1(address))
196
- range = RobustExcelOle::Range.new(name_object('__dummy001').RefersToRange)
199
+ range = RobustExcelOle::Range.new(name_object('__dummy001').RefersToRange, worksheet)
197
200
  self.Names.Item('__dummy001').Delete
198
201
  workbook = self.is_a?(Workbook) ? self : self.workbook
199
202
  workbook.save
@@ -0,0 +1,30 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ module RobustExcelOle
4
+
5
+ class VbaObjects < Base
6
+
7
+ def to_reo
8
+ self
9
+ end
10
+
11
+ # @private
12
+ def address_tool
13
+ excel.address_tool
14
+ end
15
+
16
+ end
17
+
18
+ # @private
19
+ class RangeNotEvaluatable < MiscREOError
20
+ end
21
+
22
+ # @private
23
+ class OptionInvalid < MiscREOError
24
+ end
25
+
26
+ # @private
27
+ class ObjectNotAlive < MiscREOError
28
+ end
29
+
30
+ end
@@ -1,3 +1,3 @@
1
1
  module RobustExcelOle
2
- VERSION = "1.17"
2
+ VERSION = "1.18.4"
3
3
  end
@@ -783,12 +783,22 @@ module RobustExcelOle
783
783
  raise NameNotFound, "could not return a sheet with name #{name.inspect}"
784
784
  end
785
785
 
786
+ def worksheets_count
787
+ @ole_workbook.Worksheets.Count
788
+ end
789
+
786
790
  def each
787
791
  @ole_workbook.Worksheets.each do |sheet|
788
792
  yield worksheet_class.new(sheet)
789
793
  end
790
794
  end
791
795
 
796
+ def worksheets
797
+ result = []
798
+ each { |worksheet| result << worksheet }
799
+ result
800
+ end
801
+
792
802
  def each_with_index(offset = 0)
793
803
  i = offset
794
804
  @ole_workbook.Worksheets.each do |sheet|
@@ -813,24 +823,18 @@ module RobustExcelOle
813
823
  new_sheet_name = opts.delete(:as)
814
824
  last_sheet_local = last_sheet
815
825
  after_or_before, base_sheet = opts.to_a.first || [:after, last_sheet_local]
826
+ base_sheet_ole = base_sheet.ole_worksheet
816
827
  begin
817
828
  if !::COPYSHEETS_JRUBY_BUG
818
- #if sheet
819
- # sheet.Copy({ after_or_before.to_s => base_sheet.ole_worksheet })
820
- #else
821
- # ole_workbook.Worksheets.Add({ after_or_before.to_s => base_sheet.ole_worksheet })
822
- #end
823
- add_or_copy_sheet_simple(sheet, { after_or_before.to_s => base_sheet.ole_worksheet })
829
+ add_or_copy_sheet_simple(sheet, { after_or_before.to_s => base_sheet_ole })
824
830
  else
825
831
  if after_or_before == :before
826
- #add_or_copy_sheet_simple(sheet,base_sheet)
827
- add_or_copy_sheet_simple(sheet, base_sheet.ole_worksheet)
832
+ add_or_copy_sheet_simple(sheet, base_sheet_ole)
828
833
  else
829
834
  if base_sheet.name != last_sheet_local.name
830
835
  add_or_copy_sheet_simple(sheet, base_sheet.Next)
831
836
  else
832
- #add_or_copy_sheet_simple(sheet,base_sheet)
833
- add_or_copy_sheet_simple(sheet, base_sheet.ole_worksheet)
837
+ add_or_copy_sheet_simple(sheet, base_sheet_ole)
834
838
  base_sheet.Move(ole_workbook.Worksheets.Item(ole_workbook.Worksheets.Count-1))
835
839
  ole_workbook.Worksheets.Item(ole_workbook.Worksheets.Count).Activate
836
840
  end
@@ -846,19 +850,11 @@ module RobustExcelOle
846
850
 
847
851
  private
848
852
 
849
- #def add_or_copy_sheet_simple(sheet, base_sheet)
850
- # if sheet
851
- # sheet.Copy(base_sheet.ole_worksheet)
852
- # else
853
- # ole_workbook.Worksheets.Add(base_sheet.ole_worksheet)
854
- # end
855
- #end
856
-
857
- def add_or_copy_sheet_simple(sheet, base_ole_worksheet)
853
+ def add_or_copy_sheet_simple(sheet, base_sheet_ole_or_hash)
858
854
  if sheet
859
- sheet.Copy(base_ole_worksheet)
855
+ sheet.Copy(base_sheet_ole_or_hash)
860
856
  else
861
- ole_workbook.Worksheets.Add(base_ole_worksheet)
857
+ ole_workbook.Worksheets.Add(base_sheet_ole_or_hash)
862
858
  end
863
859
  end
864
860