jruby-poi 0.6.0 → 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.6.0
1
+ 0.6.1
data/jruby-poi.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{jruby-poi}
8
- s.version = "0.6.0"
8
+ s.version = "0.6.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Scott Deming", "Jason Rogers"]
@@ -35,6 +35,7 @@ Gem::Specification.new do |s|
35
35
  "lib/poi-scratchpad-3.6-20091214.jar",
36
36
  "lib/poi.rb",
37
37
  "lib/poi/workbook.rb",
38
+ "lib/poi/workbook/area.rb",
38
39
  "lib/poi/workbook/cell.rb",
39
40
  "lib/poi/workbook/named_range.rb",
40
41
  "lib/poi/workbook/row.rb",
@@ -0,0 +1,24 @@
1
+ module POI
2
+ class Area
3
+ def initialize reference
4
+ @ref = reference
5
+ end
6
+
7
+ def in workbook
8
+ begin
9
+ area.getAllReferencedCells.collect{|c| workbook.cell c.formatAsString}
10
+ rescue
11
+ []
12
+ end
13
+ end
14
+
15
+ def single_cell_reference?
16
+ @ref == area.getFirstCell.formatAsString
17
+ end
18
+
19
+ private
20
+ def area
21
+ @area ||= org.apache.poi.ss.util.AreaReference.new(@ref)
22
+ end
23
+ end
24
+ end
@@ -1,45 +1,30 @@
1
- class NamedRange
1
+ module POI
2
+ class NamedRange
2
3
 
3
- # takes an instance of org.apache.poi.ss.usermodel.Name, and a POI::Workbook
4
- def initialize name, workbook
5
- @name = name
6
- @workbook = workbook
7
- end
8
-
9
- def name
10
- @name.getNameName
11
- end
12
-
13
- def sheet
14
- @workbook[@name.getSheetName]
15
- end
16
-
17
- def formula
18
- @name.getRefersToFormula
19
- end
4
+ # takes an instance of org.apache.poi.ss.usermodel.Name, and a POI::Workbook
5
+ def initialize name, workbook
6
+ @name = name
7
+ @workbook = workbook
8
+ end
20
9
 
21
- def cells
22
- area_range? ? all_cells_from_area : @workbook.cell(formula)
23
- end
10
+ def name
11
+ @name.getNameName
12
+ end
24
13
 
25
- def values
26
- cells.collect{|c| c.value}
27
- end
14
+ def sheet
15
+ @workbook[@name.getSheetName]
16
+ end
28
17
 
29
- private
30
- def area_range?
31
- area.nil? == false
18
+ def formula
19
+ @name.getRefersToFormula
32
20
  end
33
-
34
- def area
35
- @area ||= begin
36
- org.apache.poi.ss.util.AreaReference.new formula
37
- rescue
38
- nil
39
- end
21
+
22
+ def cells
23
+ [@workbook.cell(formula)].flatten
40
24
  end
41
-
42
- def all_cells_from_area
43
- area.getAllReferencedCells.collect{|c| @workbook.cell c.formatAsString}
25
+
26
+ def values
27
+ cells.collect{|c| c.value}
44
28
  end
29
+ end
45
30
  end
@@ -63,18 +63,19 @@ module POI
63
63
  # a String which is the sheet name or a cell reference.
64
64
  #
65
65
  # If a cell reference is passed the value of that cell is returned.
66
- def [](sheet_index)
66
+ def [](reference)
67
67
  begin
68
- cell = cell(sheet_index)
68
+ cell = cell(reference)
69
69
  Array === cell ? cell.collect{|e| e.value} : cell.value
70
70
  rescue
71
- answer = worksheets[sheet_index]
71
+ answer = worksheets[reference]
72
72
  answer.poi_worksheet.nil? ? nil : answer
73
73
  end
74
74
  end
75
75
 
76
76
  # takes a String in the form of a 3D cell reference and returns the Cell (eg. "Sheet 1!A1")
77
77
  def cell reference
78
+ # if the reference is to a named range of cells, get that range and return it
78
79
  if named_range = named_ranges.detect{|e| e.name == reference}
79
80
  cells = named_range.cells.compact
80
81
  if cells.empty?
@@ -83,7 +84,13 @@ module POI
83
84
  return cells.length == 1 ? cells.first : cells
84
85
  end
85
86
  end
86
-
87
+
88
+ # if the reference is to an area of cells, get all the cells in that area and return them
89
+ cells = cells_in_area(reference)
90
+ unless cells.empty?
91
+ return cells.length == 1 ? cells.first : cells
92
+ end
93
+
87
94
  ref = org.apache.poi.ss.util.CellReference.new(reference)
88
95
  if ref.getSheetName.nil?
89
96
  raise 'cell references at the workbook level must include a sheet reference (eg. Sheet1!A1)'
@@ -91,6 +98,15 @@ module POI
91
98
  worksheets[ref.getSheetName][ref.getRow][ref.getCol]
92
99
  end
93
100
  end
101
+
102
+ def cells_in_area reference
103
+ area = Area.new(reference)
104
+ if area.single_cell_reference?
105
+ []
106
+ else
107
+ area.in(self).compact
108
+ end
109
+ end
94
110
 
95
111
  def poi_workbook
96
112
  @workbook
data/lib/poi/workbook.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require File.join(JRUBY_POI_LIB_PATH, 'poi', 'workbook', 'area')
1
2
  require File.join(JRUBY_POI_LIB_PATH, 'poi', 'workbook', 'named_range')
2
3
  require File.join(JRUBY_POI_LIB_PATH, 'poi', 'workbook', 'workbook')
3
4
  require File.join(JRUBY_POI_LIB_PATH, 'poi', 'workbook', 'worksheet')
Binary file
@@ -71,6 +71,12 @@ describe POI::Workbook do
71
71
  end
72
72
  end
73
73
 
74
+ it "should return an array of cell values by reference" do
75
+ name = TestDataFile.expand_path("various_samples.xlsx")
76
+ book = POI::Workbook.open(name)
77
+ book['dates!A2:A16'].should == (Date.parse('2010-02-28')..Date.parse('2010-03-14')).to_a
78
+ end
79
+
74
80
  it "should return cell values by reference" do
75
81
  name = TestDataFile.expand_path("various_samples.xlsx")
76
82
  book = POI::Workbook.open(name)
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 6
8
- - 0
9
- version: 0.6.0
8
+ - 1
9
+ version: 0.6.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Scott Deming
@@ -49,6 +49,7 @@ files:
49
49
  - lib/poi-scratchpad-3.6-20091214.jar
50
50
  - lib/poi.rb
51
51
  - lib/poi/workbook.rb
52
+ - lib/poi/workbook/area.rb
52
53
  - lib/poi/workbook/cell.rb
53
54
  - lib/poi/workbook/named_range.rb
54
55
  - lib/poi/workbook/row.rb