poi_pond 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ gem "rjb", "1.3.2"
9
+ group :development do
10
+ gem "shoulda", ">= 0"
11
+ gem "bundler", "~> 1.0.0"
12
+ gem "jeweler", "~> 1.5.2"
13
+ gem "rcov", ">= 0"
14
+ end
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Lance Gleason
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,28 @@
1
+ = poi_pond
2
+
3
+ This is a gem to encapsulate the Apache Java POI library for use in a native (non jruby) environment.
4
+
5
+ == History
6
+ My motivation for creating this gem was the need to create an excel spreadsheet in Ruby that offers some advanced features that currently do not exist in other Ruby Excel generator gems. POI has a gem that is supposed to compile POI as a native Ruby extension. I tried it and had a lot of problems with it and finally gave up on that approach. I really didn't feel like converting my project to jruby just to use POI, and using an approach that required running a JVM based process in the background or doing system calls just seemed wrong. So I created POI pond which uses RJB to access the the jvm, load POI and encapsulate the poi functions in a form that is not as doing direct RJB/POI calls.
7
+
8
+ == Dependencies
9
+ You will need to have a jvm installed and in your path to work correctly. At the time of this writing this gem has only been tested on MacOS version 10.6.6. Your mileage may vary.
10
+
11
+ == Conventions
12
+ This is a light wrapper around POI which is a Java library. If a method is added by the gem it follows standard Ruby conventions ie: my_method_name. If a a method is coming directly from the POI java library it follows the Java camel case convention myMethodName. My thought is that it would make it easier to know where to look for documentation etc. following that approach. If this really bothers you send me a message or just fork the project and add some wrappers to make things more ruby like. As long as you have good tests around you code I will be happy to bring the changes back into the main project.
13
+
14
+ == Contributing to poi_pond
15
+
16
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
17
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
18
+ * Fork the project
19
+ * Start a feature/bugfix branch
20
+ * Commit and push until you are happy with your contribution
21
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
22
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
23
+
24
+ == Copyright
25
+
26
+ Copyright (c) 2011 Lance Gleason. See LICENSE.txt for
27
+ further details.
28
+
@@ -0,0 +1,57 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "poi_pond"
16
+ gem.homepage = "http://github.com/lgleasain/poi_pond"
17
+ gem.license = "MIT"
18
+ gem.summary = %Q{A gem to user POI in a native (non-jruby) environment}
19
+ gem.description = %Q{This gem wraps the }
20
+ gem.email = "lgleasain@yahoo.com"
21
+ gem.authors = ["Lance Gleason"]
22
+ # gem.add_runtime_dependency 'rjb'
23
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
24
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
25
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
26
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
27
+ end
28
+ Jeweler::RubygemsDotOrgTasks.new
29
+ # rescue LoadError
30
+ # puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler."
31
+ # end
32
+
33
+ require 'rake/testtask'
34
+ Rake::TestTask.new(:test) do |test|
35
+ test.libs << 'lib' << 'test'
36
+ test.pattern = 'test/**/test_*.rb'
37
+ test.verbose = true
38
+ end
39
+
40
+ require 'rcov/rcovtask'
41
+ Rcov::RcovTask.new do |test|
42
+ test.libs << 'test'
43
+ test.pattern = 'test/**/test_*.rb'
44
+ test.verbose = true
45
+ end
46
+
47
+ task :default => :test
48
+
49
+ require 'rake/rdoctask'
50
+ Rake::RDocTask.new do |rdoc|
51
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
52
+
53
+ rdoc.rdoc_dir = 'rdoc'
54
+ rdoc.title = "poi_pond #{version}"
55
+ rdoc.rdoc_files.include('README*')
56
+ rdoc.rdoc_files.include('lib/**/*.rb')
57
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,93 @@
1
+ require 'rjb'
2
+ require 'style'
3
+
4
+ module POIPond
5
+ include Style
6
+ def initialize_poi
7
+ dir = File.join(File.dirname(File.dirname(__FILE__)), 'javalibs')
8
+ if File.exist?(dir)
9
+ jardir = File.join(File.dirname(File.dirname(__FILE__)), 'javalibs', '**', '*.jar')
10
+ else
11
+ jardir = File.join('.','javalibs', '**', '*.jar')
12
+ end
13
+ Rjb::load(classpath = Dir.glob(jardir).join(':'), jvmargs=['-Djava.awt.headless=true'])
14
+ end
15
+
16
+ def create_excel_workbook(file = nil)
17
+ file ? Rjb::import('org.apache.poi.hssf.usermodel.HSSFWorkbook').new(file) : Rjb::import('org.apache.poi.hssf.usermodel.HSSFWorkbook').new
18
+ end
19
+
20
+ def create_excel_cell_range_address
21
+ Rjb::import('org.apache.poi.ss.util.CellRangeAddress')
22
+ end
23
+
24
+ def hssf_data_format
25
+ Rjb::import('org.apache.poi.hssf.usermodel.HSSFDataFormat')
26
+ end
27
+
28
+ def poi_output_file(file)
29
+ Rjb::import('java.io.FileOutputStream').new(file)
30
+ end
31
+
32
+ def poi_byte_array_output_stream
33
+ Rjb::import('java.io.ByteArrayOutputStream').new
34
+ end
35
+
36
+ def poi_input_file(file)
37
+ Rjb::import('java.io.FileInputStream').new(file)
38
+ end
39
+
40
+ def add_photo_to_sheet(workbook, sheet, row, column, image)
41
+ picture_index = workbook.addPicture image, workbook.PICTURE_TYPE_JPEG
42
+ drawing = workbook.getSheet(sheet).createDrawingPatriarch
43
+ anchor = workbook.getCreationHelper.createClientAnchor
44
+ anchor.setCol1 column
45
+ anchor.setRow1 row
46
+ drawing.createPicture(anchor, picture_index).resize
47
+ end
48
+
49
+ def create_spreadsheet(options, passed_styles = nil)
50
+ workbook = create_excel_workbook
51
+ if passed_styles
52
+ styles = {}
53
+ passed_styles.each do |style|
54
+ styles[style.first] = create_cell_style workbook, style.last
55
+ end
56
+ end
57
+ options.each do |sheet_hash|
58
+ sheet = workbook.createSheet sheet_hash[:sheet][:name]
59
+ sheet.setPrintGridlines(!!sheet_hash[:sheet][:print_grid_lines])
60
+ sheet.setDisplayGridlines(!!sheet_hash[:sheet][:display_grid_lines])
61
+ if sheet_hash[:sheet][:merged_regions]
62
+ sheet_hash[:sheet][:merged_regions].each do |merged_region|
63
+ sheet.addMergedRegion create_excel_cell_range_address.new(merged_region[:start_row], merged_region[:end_row],
64
+ merged_region[:start_column], merged_region[:end_column])
65
+ end
66
+ end
67
+ if sheet_hash[:sheet][:photos]
68
+ sheet_hash[:sheet][:photos].each do |photo|
69
+ add_photo_to_sheet workbook, sheet_hash[:sheet][:name], photo[:row], photo[:column], photo[:photo]
70
+ end
71
+ end
72
+ if sheet_hash[:sheet][:column_widths]
73
+ sheet_hash[:sheet][:column_widths].each do |column_width|
74
+ sheet.setColumnWidth column_width.first, column_width.last
75
+ end
76
+ end
77
+ if sheet_hash[:sheet][:row]
78
+ sheet_hash[:sheet][:row].each do |row_hash|
79
+ row = sheet.createRow row_hash[:row_index]
80
+ row_hash[:row_height] ? row.setHeight(row_hash[:row_height]) : nil
81
+ if row_hash[:cell]
82
+ row_hash[:cell].each do |cell_hash|
83
+ cell = row.createCell cell_hash[:cell_index]
84
+ cell_hash[:value] ? cell.setCellValue(cell_hash[:value].to_s) : nil
85
+ cell_hash[:style] ? cell.setCellStyle(styles[cell_hash[:style]]) : nil
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
91
+ workbook
92
+ end
93
+ end
@@ -0,0 +1,81 @@
1
+ module Style
2
+ def poi_color color
3
+ nil_out_runtime_error {Rjb::import('org.apache.poi.ss.usermodel.IndexedColors').send(color).getIndex}
4
+ end
5
+
6
+ def excel_cell_style
7
+ Rjb::import('org.apache.poi.ss.usermodel.CellStyle')
8
+ end
9
+
10
+ def hssf_cell_style
11
+ Rjb::import('org.apache.poi.hssf.usermodel.HSSFCellStyle')
12
+ end
13
+
14
+ def create_cell_style workbook, options
15
+ workbook_font = workbook.createFont
16
+ style = workbook.createCellStyle
17
+ options[:font_height] ? workbook_font.setFontHeightInPoints(options[:font_height]) : nil
18
+ options[:font_color] && poi_color(options[:font_color]) ? workbook_font.setColor(poi_color(options[:font_color])) : nil
19
+ options[:font_name] ? workbook_font.setFontName(options[:font_name]) : nil
20
+ options[:bold] ? workbook_font.setBoldweight(workbook_font.BOLDWEIGHT_BOLD) : nil
21
+ nil_out_runtime_error {options[:horizontal_alignment] ?
22
+ style.setAlignment(excel_cell_style.send(options[:horizontal_alignment])) : nil}
23
+ nil_out_runtime_error {options[:vertical_alignment] ?
24
+ style.setVerticalAlignment(excel_cell_style.send(options[:vertical_alignment])) : nil}
25
+ nil_out_runtime_error {options[:border_left] ?
26
+ style.setBorderLeft(excel_cell_style.send(options[:border_left])) : nil}
27
+ nil_out_runtime_error {options[:border_right] ?
28
+ style.setBorderRight(excel_cell_style.send(options[:border_right])) : nil}
29
+ nil_out_runtime_error {options[:border_top] ?
30
+ style.setBorderTop(excel_cell_style.send(options[:border_top])) : nil}
31
+ nil_out_runtime_error { options[:border_bottom] ?
32
+ style.setBorderBottom(excel_cell_style.send(options[:border_bottom])) : nil }
33
+ options[:border_left_color] && poi_color(options[:border_left_color]) ?
34
+ style.setLeftBorderColor(poi_color options[:border_left_color]) : nil
35
+ options[:border_right_color] && poi_color(options[:border_right_color]) ?
36
+ style.setRightBorderColor(poi_color options[:border_right_color]) : nil
37
+ options[:border_top_color] && poi_color(options[:border_top_color]) ?
38
+ style.setTopBorderColor(poi_color options[:border_top_color]) : nil
39
+ options[:border_bottom_color] && poi_color(options[:border_bottom_color]) ?
40
+ style.setBottomBorderColor(poi_color options[:border_bottom_color]) : nil
41
+ options[:wrap_text] ? style.setWrapText(options[:wrap_text]) : nil
42
+ options[:indentation] ? style.setIndention(options[:indentation]) : nil
43
+
44
+ if options[:border]
45
+ nil_out_runtime_error {
46
+ style.setBorderBottom excel_cell_style.send(options[:border])
47
+ style.setBorderTop excel_cell_style.send(options[:border])
48
+ style.setBorderLeft excel_cell_style.send(options[:border])
49
+ style.setBorderRight excel_cell_style.send(options[:border])
50
+ }
51
+ end
52
+
53
+ if options[:border_color] && poi_color(options[:border_color])
54
+ style.setTopBorderColor poi_color(options[:border_color])
55
+ style.setBottomBorderColor poi_color(options[:border_color])
56
+ style.setLeftBorderColor poi_color(options[:border_color])
57
+ style.setRightBorderColor poi_color(options[:border_color])
58
+ end
59
+
60
+ if options[:background_color] && poi_color(options[:background_color])
61
+ style.setFillForegroundColor(poi_color(options[:background_color]))
62
+ style.setFillPattern 1
63
+ end
64
+
65
+ style.setFont workbook_font
66
+ # next three lines are a hack to test fonts due to a rjb bug
67
+ def style.set_font(font); @font = font; end;
68
+ def style.get_font; @font; end;
69
+ style.set_font workbook_font
70
+ style
71
+ end
72
+
73
+ private
74
+ def nil_out_runtime_error(&block)
75
+ begin
76
+ block.call
77
+ rescue RuntimeError
78
+ nil
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'poi_pond'
16
+
17
+ class Test::Unit::TestCase
18
+ end
Binary file
@@ -0,0 +1,173 @@
1
+ require 'helper'
2
+ require 'rjb'
3
+
4
+ class TestPoiPond < Test::Unit::TestCase
5
+ include POIPond
6
+ context "initialize" do
7
+ should "initialize rjb and return a valid object" do
8
+ initialize_poi
9
+ assert_not_nil Rjb::import('org.apache.poi.hssf.usermodel.HSSFWorkbook').new
10
+ end
11
+ end
12
+
13
+ context "spreadsheet" do
14
+ setup do
15
+ initialize_poi
16
+ end
17
+
18
+ context "primitive functions" do
19
+ should "create a workbook" do
20
+ workbook = create_excel_workbook
21
+ assert workbook.createSheet('sheet1')
22
+ end
23
+
24
+ should "create a cell range address object" do
25
+ assert create_excel_cell_range_address.valueOf('$A$2')
26
+ end
27
+
28
+ should "create a HSSFDataFormat object" do
29
+ reference_built_in_format = Rjb::import('org.apache.poi.hssf.usermodel.HSSFDataFormat').getBuiltinFormat("m/d/yy h:mm")
30
+ assert_equal reference_built_in_format, hssf_data_format.getBuiltinFormat("m/d/yy h:mm")
31
+ end
32
+
33
+ should "create a FileOutputStream object" do
34
+ assert_equal Rjb::import('java.io.FileOutputStream').new('foo').java_methods, poi_output_file('foo').java_methods
35
+ File.delete 'foo'
36
+ end
37
+
38
+ should "create a byteArrayOutputStream object" do
39
+ assert_equal Rjb::import('java.io.ByteArrayOutputStream').new.java_methods, poi_byte_array_output_stream.java_methods
40
+ end
41
+
42
+ should "create a FileInputStream object" do
43
+ poi_output_file('foo')
44
+ assert_equal Rjb::import('java.io.FileInputStream').new('foo').java_methods, poi_input_file('foo').java_methods
45
+ File.delete 'foo'
46
+ end
47
+
48
+ should "create a image and place it on a worksheet" do
49
+ test_image = File.join(File.dirname(File.dirname(__FILE__)), 'test', 'image001.jpg')
50
+ workbook = create_excel_workbook
51
+ workbook.createSheet('sheet1')
52
+ add_photo_to_sheet(workbook, 'sheet1', 1, 1, File.new(test_image).bytes.to_a)
53
+ workbook.write(poi_output_file('my_test.xls'))
54
+ read_workbook = create_excel_workbook poi_input_file('my_test.xls')
55
+ assert_equal File.new(test_image).bytes.to_a, read_workbook.getAllPictures.get(0).getData.bytes.to_a
56
+ File.delete 'my_test.xls'
57
+ end
58
+ end
59
+
60
+ context "create" do
61
+ should "create a spreadsheet with one a sheet called sheet1" do
62
+ assert_not_nil create_spreadsheet([:sheet => {:name => 'sheet1'}]).getSheet('sheet1')
63
+ end
64
+
65
+ should "create a spreadsheet with multiple sheets" do
66
+ spreadsheet = create_spreadsheet([{:sheet => {:name => 'sheet1'}}, {:sheet => {:name => 'sheet2'}}])
67
+ assert_not_nil spreadsheet.getSheet('sheet1')
68
+ assert_not_nil spreadsheet.getSheet('sheet2')
69
+ end
70
+
71
+ should "set printGridlines to true for sheet1" do
72
+ assert create_spreadsheet([:sheet => {:name => 'sheet1', :print_grid_lines => true}]).getSheet('sheet1').isPrintGridlines
73
+ end
74
+
75
+ should "set displayGridlines to false for sheet1" do
76
+ assert !create_spreadsheet([:sheet => {:name => 'sheet1', :display_grid_lines => false}]).getSheet('sheet1').isDisplayGridlines
77
+ end
78
+
79
+ should "create a row 1" do
80
+ assert create_spreadsheet([:sheet => {:name => 'sheet1', :row => [{:row_index => 1}]}]).getSheet('sheet1').getRow(1)
81
+ end
82
+
83
+ should "create row 3 and 7" do
84
+ sheet = create_spreadsheet([:sheet => {:name => 'sheet1', :row => [{:row_index => 3}, {:row_index => 7}]}]).getSheet('sheet1')
85
+ assert sheet.getRow(3)
86
+ assert sheet.getRow(7)
87
+ end
88
+
89
+ should "create a row with a height of 666" do
90
+ assert_equal 666, create_spreadsheet([:sheet => {:name => 'sheet1',
91
+ :row => [{:row_index => 1, :row_height => 666}]}]).getSheet('sheet1').getRow(1).getHeight
92
+ end
93
+
94
+ should "create cell 1" do
95
+ assert create_spreadsheet([:sheet => {:name => 'sheet1',
96
+ :row => [{:row_index => 1, :cell => [{:cell_index => 1}]}]}]).getSheet('sheet1').getRow(1).getCell(1)
97
+ end
98
+
99
+ should "create cell 2 and 6" do
100
+ sheet = create_spreadsheet([:sheet => {:name => 'sheet1',
101
+ :row => [{:row_index => 1, :cell => [{:cell_index => 2}, {:cell_index => 6}]}]}]).getSheet('sheet1')
102
+ assert sheet.getRow(1).getCell(2)
103
+ assert sheet.getRow(1).getCell(6)
104
+ end
105
+
106
+ should "create a cell at row 3 cell 3 with the text hello" do
107
+ sheet = create_spreadsheet([:sheet => {:name => 'sheet1',
108
+ :row => [{:row_index => 3, :cell => [{:cell_index => 3, :value => 'hello'}]}]}]).getSheet('sheet1')
109
+ assert_equal 'hello', sheet.getRow(3).getCell(3).getStringCellValue
110
+ end
111
+
112
+ should "create a merged cell region from row 3 column 2 to row 5 column 10" do
113
+ sheet = create_spreadsheet([:sheet => {:name => 'sheet1', :merged_regions => [{:start_row => 2, :start_column => 1,
114
+ :end_row => 4, :end_column => 9}]}]).getSheet('sheet1')
115
+ assert_equal 'B3:J5', sheet.getMergedRegion(0).formatAsString
116
+ end
117
+
118
+ should "create a merged cell region from row 3 column 2 to row 5 column 10 and one from row 10 column 1 to row 15 column 20" do
119
+ sheet = create_spreadsheet([:sheet => {:name => 'sheet1', :merged_regions => [{:start_row => 2, :start_column => 1,
120
+ :end_row => 4, :end_column => 9}, {:start_row => 9, :start_column => 0, :end_row => 14,
121
+ :end_column => 19}]}]).getSheet('sheet1')
122
+ assert_equal 'B3:J5', sheet.getMergedRegion(0).formatAsString
123
+ assert_equal 'A10:T15', sheet.getMergedRegion(1).formatAsString
124
+ end
125
+
126
+ should "create a cell with a 24 point font" do
127
+ sheet = create_spreadsheet([:sheet => {:name => 'sheet1', :row => [{:row_index => 1, :cell => [{:cell_index => 1,
128
+ :style => 'title'}]}]}], {'title' => {:font_height => 24}}).getSheet('sheet1')
129
+ assert_equal 24, sheet.getRow(1).getCell(1).getCellStyle.getFont(sheet.getWorkbook).getFontHeightInPoints
130
+ end
131
+
132
+ should "create a cell with a 24 point font and another one with a 34 point one" do
133
+ sheet = create_spreadsheet([:sheet => {:name => 'sheet1', :row => [{:row_index => 1, :cell => [{:cell_index => 1,
134
+ :style => 'title'}, {:cell_index => 2, :style => 'text'}]}]}], {'title' =>
135
+ {:font_height => 24}, 'text' => {:font_height => 34}}).getSheet('sheet1')
136
+ assert_equal 24, sheet.getRow(1).getCell(1).getCellStyle.getFont(sheet.getWorkbook).getFontHeightInPoints
137
+ assert_equal 34, sheet.getRow(1).getCell(2).getCellStyle.getFont(sheet.getWorkbook).getFontHeightInPoints
138
+ end
139
+
140
+ should "set the column 1 width to 666" do
141
+ assert_equal 666,
142
+ create_spreadsheet([:sheet => {:name => 'sh1', :column_widths => { 1 => 666}}]).getSheet('sh1').getColumnWidth(1)
143
+ end
144
+
145
+ should "set the column 1 width to 666 and column 3 width to 2323" do
146
+ sheet = create_spreadsheet([:sheet => {:name => 'sheet1', :column_widths => { 1 => 666, 3 => 2323}}]).getSheet('sheet1')
147
+ assert_equal 666, sheet.getColumnWidth(1)
148
+ assert_equal 2323, sheet.getColumnWidth(3)
149
+ end
150
+
151
+ should "add a photo to the workbook" do
152
+ file = File.new(File.join(File.dirname(File.dirname(__FILE__)), 'test', 'image001.jpg')).bytes.to_a
153
+ workbook = create_spreadsheet([:sheet => {:name => 'sheet1', :photos => [:row => 1, :column => 1,
154
+ :photo => file]}])
155
+ workbook.write(poi_output_file('my_test.xls'))
156
+ read_workbook = create_excel_workbook poi_input_file('my_test.xls')
157
+ assert_equal file, read_workbook.getAllPictures.get(0).getData.bytes.to_a
158
+ File.delete 'my_test.xls'
159
+ end
160
+
161
+ should "add multiple photos to the workbook" do
162
+ file = File.new(File.join(File.dirname(File.dirname(__FILE__)), 'test', 'image001.jpg')).bytes.to_a
163
+ workbook = create_spreadsheet([:sheet => {:name => 'sheet1', :photos => [{:row => 1, :column => 1,
164
+ :photo => file}, {:row => 100, :column => 1, :photo => file}]}])
165
+ workbook.write(poi_output_file('my_test.xls'))
166
+ read_workbook = create_excel_workbook poi_input_file('my_test.xls')
167
+ assert_equal file, read_workbook.getAllPictures.get(0).getData.bytes.to_a
168
+ assert_equal file, read_workbook.getAllPictures.get(1).getData.bytes.to_a
169
+ File.delete 'my_test.xls'
170
+ end
171
+ end
172
+ end
173
+ end
@@ -0,0 +1,213 @@
1
+ require 'helper'
2
+ require 'rjb'
3
+
4
+ class TestStyle < Test::Unit::TestCase
5
+ include POIPond
6
+ context "style" do
7
+ setup do
8
+ initialize_poi
9
+ end
10
+
11
+ context "class function" do
12
+ should "get POI dark blue color index" do
13
+ assert_equal Rjb::import('org.apache.poi.ss.usermodel.IndexedColors').DARK_BLUE.getIndex, poi_color('DARK_BLUE')
14
+ end
15
+
16
+ should "return -1 if color isn't found" do
17
+ assert_equal nil, poi_color('foo')
18
+ end
19
+
20
+ should "return a cellStyle object" do
21
+ assert_equal Rjb::import('org.apache.poi.ss.usermodel.CellStyle').ALIGN_LEFT, excel_cell_style.ALIGN_LEFT
22
+ end
23
+
24
+ should "return a hssfCellStyle object" do
25
+ assert Rjb::import('org.apache.poi.hssf.usermodel.HSSFCellStyle').ALIGN_CENTER, hssf_cell_style.ALIGN_CENTER
26
+ end
27
+ end
28
+
29
+ context "font" do
30
+ should "create a style with a 24 point default font" do
31
+ assert_equal 24, create_cell_style(create_excel_workbook, {:font_height => 24}).get_font.getFontHeightInPoints
32
+ assert_equal 'Arial', create_cell_style(create_excel_workbook, {:font_height => 24}).get_font.getFontName
33
+ end
34
+
35
+ should "create a style with a dark blue Tahoma 24 point font" do
36
+ cell_style = create_cell_style create_excel_workbook, {:font_height => 24, :font_color => 'DARK_BLUE', :font_name => 'Tahoma'}
37
+ assert_equal 24, cell_style.get_font.getFontHeightInPoints
38
+ assert_equal poi_color('DARK_BLUE'), cell_style.get_font.getColor
39
+ assert_equal 'Tahoma', cell_style.get_font.getFontName
40
+ end
41
+
42
+ should "not modify the color if a invalid one is passed" do
43
+ assert_equal create_cell_style(create_excel_workbook, {}).get_font.getColor,
44
+ create_cell_style(create_excel_workbook, {:font_color => 'foo'}).get_font.getColor
45
+ end
46
+
47
+ should "create a style with a left justified horizontal alignment" do
48
+ assert_equal excel_cell_style.ALIGN_LEFT,
49
+ create_cell_style(create_excel_workbook, {:horizontal_alignment => 'ALIGN_LEFT'}).getAlignment
50
+ end
51
+
52
+ should "not modify the horizontal alignment if a invalid one is specified" do
53
+ assert_equal create_cell_style(create_excel_workbook, {}).getAlignment,
54
+ create_cell_style(create_excel_workbook, {:horizontal_alignment => 'foo'}).getAlignment
55
+ end
56
+
57
+ should "not modify the vertical alignment if a invalid one is specified" do
58
+ assert_equal create_cell_style(create_excel_workbook, {}).getVerticalAlignment,
59
+ create_cell_style(create_excel_workbook, {:vertical_alignment => 'foo'}).getVerticalAlignment
60
+ end
61
+
62
+ should "create a style with a vertical top vertical alignment" do
63
+ assert_equal excel_cell_style.VERTICAL_TOP,
64
+ create_cell_style(create_excel_workbook, {:vertical_alignment => 'VERTICAL_TOP'}).getVerticalAlignment
65
+ end
66
+
67
+ should "set the font weight to bold" do
68
+ my_font = create_cell_style(create_excel_workbook, {:bold => true}).get_font
69
+ assert_equal my_font.BOLDWEIGHT_BOLD, my_font.getBoldweight
70
+ end
71
+
72
+ should "not set the font to bold if no bold setting is specified" do
73
+ my_font = create_cell_style(create_excel_workbook, {}).get_font
74
+ assert_equal my_font.BOLDWEIGHT_NORMAL, my_font.getBoldweight
75
+ end
76
+ end
77
+
78
+ context "border" do
79
+ should "set a left border for the cell" do
80
+ assert_equal excel_cell_style.BORDER_MEDIUM, create_cell_style(create_excel_workbook, {:border_left => 'BORDER_MEDIUM'}).getBorderLeft
81
+ end
82
+
83
+ should "not set a left border for a cell if a invalid one is specified" do
84
+ assert_equal create_cell_style(create_excel_workbook, {}).getBorderLeft,
85
+ create_cell_style(create_excel_workbook, {:border_left => 'foo'}).getBorderLeft
86
+ end
87
+
88
+ should "set a right border for the cell" do
89
+ assert_equal excel_cell_style.BORDER_MEDIUM, create_cell_style(create_excel_workbook, {:border_right => 'BORDER_MEDIUM'}).getBorderRight
90
+ end
91
+
92
+ should "not set a right border for a cell if a invalid one is specified" do
93
+ assert_equal create_cell_style(create_excel_workbook, {}).getBorderRight,
94
+ create_cell_style(create_excel_workbook, {:border_right => 'foo'}).getBorderRight
95
+ end
96
+
97
+ should "set a top border for the cell" do
98
+ assert_equal excel_cell_style.BORDER_MEDIUM, create_cell_style(create_excel_workbook, {:border_top => 'BORDER_MEDIUM'}).getBorderTop
99
+ end
100
+
101
+ should "not set a top border for a cell if a invalid one is specified" do
102
+ assert_equal create_cell_style(create_excel_workbook, {}).getBorderTop,
103
+ create_cell_style(create_excel_workbook, {:border_top => 'foo'}).getBorderTop
104
+ end
105
+
106
+ should "set a bottom border for the cell" do
107
+ assert_equal excel_cell_style.BORDER_MEDIUM, create_cell_style(create_excel_workbook, {:border_bottom => 'BORDER_MEDIUM'}).getBorderBottom
108
+ end
109
+
110
+ should "not set a bottom border for a cell if a invalid one is specified" do
111
+ assert_equal create_cell_style(create_excel_workbook, {}).getBorderBottom,
112
+ create_cell_style(create_excel_workbook, {:border_bottom => 'foo'}).getBorderBottom
113
+ end
114
+
115
+ should "set a border for the cell" do
116
+ assert_equal excel_cell_style.BORDER_MEDIUM, create_cell_style(create_excel_workbook, {:border => 'BORDER_MEDIUM'}).getBorderBottom
117
+ assert_equal excel_cell_style.BORDER_MEDIUM, create_cell_style(create_excel_workbook, {:border => 'BORDER_MEDIUM'}).getBorderTop
118
+ assert_equal excel_cell_style.BORDER_MEDIUM, create_cell_style(create_excel_workbook, {:border => 'BORDER_MEDIUM'}).getBorderLeft
119
+ assert_equal excel_cell_style.BORDER_MEDIUM, create_cell_style(create_excel_workbook, {:border => 'BORDER_MEDIUM'}).getBorderRight
120
+ end
121
+
122
+ should "not set a border for a cell if none is specified" do
123
+ assert_equal create_cell_style(create_excel_workbook, {}).getBorderBottom,
124
+ create_cell_style(create_excel_workbook, {:border => 'foo'}).getBorderBottom
125
+ assert_equal create_cell_style(create_excel_workbook, {}).getBorderTop,
126
+ create_cell_style(create_excel_workbook, {:border => 'foo'}).getBorderTop
127
+ assert_equal create_cell_style(create_excel_workbook, {}).getBorderLeft,
128
+ create_cell_style(create_excel_workbook, {:border => 'foo'}).getBorderLeft
129
+ assert_equal create_cell_style(create_excel_workbook, {}).getBorderRight,
130
+ create_cell_style(create_excel_workbook, {:border => 'foo'}).getBorderRight
131
+ end
132
+
133
+ should "set a left border color for the cell" do
134
+ assert_equal poi_color('DARK_BLUE'), create_cell_style(create_excel_workbook, {:border_left => 'BORDER_MEDIUM',
135
+ :border_left_color => 'DARK_BLUE'}).getLeftBorderColor
136
+ end
137
+
138
+ should "not set a left border color for a cell if a invalid one is specified" do
139
+ assert_equal create_cell_style(create_excel_workbook, {}).getLeftBorderColor,
140
+ create_cell_style(create_excel_workbook, {:border_left => 'foo', :border_left_color => 'foo'}).getLeftBorderColor
141
+ end
142
+
143
+ should "set a right border color for the cell" do
144
+ assert_equal poi_color('DARK_BLUE'), create_cell_style(create_excel_workbook, {:border_right => 'BORDER_MEDIUM',
145
+ :border_right_color => 'DARK_BLUE'}).getRightBorderColor
146
+ end
147
+
148
+ should "not set a right border color for a cell if a invalid one is specified" do
149
+ assert_equal create_cell_style(create_excel_workbook, {}).getRightBorderColor,
150
+ create_cell_style(create_excel_workbook, {:border_right => 'foo',
151
+ :border_right_color => 'foo'}).getRightBorderColor
152
+ end
153
+
154
+ should "set a top border color for the cell" do
155
+ assert_equal poi_color('DARK_BLUE'), create_cell_style(create_excel_workbook, {:border_top => 'BORDER_MEDIUM',
156
+ :border_top_color => 'DARK_BLUE'}).getTopBorderColor
157
+ end
158
+
159
+ should "not set a top border color for a cell if a invalid one is specified" do
160
+ assert_equal create_cell_style(create_excel_workbook, {}).getTopBorderColor,
161
+ create_cell_style(create_excel_workbook, {:border_top => 'foo',
162
+ :border_top_color => 'foo'}).getTopBorderColor
163
+ end
164
+
165
+ should "set a bottom border color for the cell" do
166
+ assert_equal poi_color('DARK_BLUE'), create_cell_style(create_excel_workbook, {:border_bottom => 'BORDER_MEDIUM',
167
+ :border_bottom_color => 'DARK_BLUE'}).getBottomBorderColor
168
+ end
169
+
170
+ should "not set a bottom border color for a cell if a invalid one is specified" do
171
+ assert_equal create_cell_style(create_excel_workbook, {}).getBottomBorderColor,
172
+ create_cell_style(create_excel_workbook, {:border_bottom => 'foo', :border_bottom_color => 'foo'}).getBottomBorderColor
173
+ end
174
+
175
+ should "set a border color for the cell" do
176
+ style = create_cell_style(create_excel_workbook, {:border => 'BORDER_MEDIUM', :border_color => 'DARK_BLUE'})
177
+ assert_equal poi_color('DARK_BLUE'), style.getBottomBorderColor
178
+ assert_equal poi_color('DARK_BLUE'), style.getTopBorderColor
179
+ assert_equal poi_color('DARK_BLUE'), style.getLeftBorderColor
180
+ assert_equal poi_color('DARK_BLUE'), style.getRightBorderColor
181
+ end
182
+
183
+ should "not set a border for a cell if none is specified" do
184
+ blank_style = create_cell_style(create_excel_workbook, {})
185
+ style = create_cell_style(create_excel_workbook, {:border => 'foo', :border_color => 'foo'})
186
+ assert_equal blank_style.getBottomBorderColor, style.getBottomBorderColor
187
+ assert_equal blank_style.getTopBorderColor, style.getTopBorderColor
188
+ assert_equal blank_style.getLeftBorderColor, style.getLeftBorderColor
189
+ assert_equal blank_style.getRightBorderColor, style.getRightBorderColor
190
+ end
191
+ end
192
+
193
+ should "set the background color to DARK_BLUE" do
194
+ style = create_cell_style(create_excel_workbook, {:background_color => 'DARK_BLUE'})
195
+ assert_equal poi_color('DARK_BLUE'), style.getFillForegroundColor
196
+ assert_equal 1, style.getFillPattern
197
+ end
198
+
199
+ should "not set a background color if a invalid one is specified" do
200
+ style = create_cell_style(create_excel_workbook, {:background_color => 'foo'})
201
+ assert_equal create_cell_style(create_excel_workbook, {}).getFillBackgroundColor, style.getFillBackgroundColor
202
+ assert_equal create_cell_style(create_excel_workbook, {}).getFillPattern, style.getFillPattern
203
+ end
204
+
205
+ should "set text wrap to true" do
206
+ assert create_cell_style(create_excel_workbook, {:wrap_text => true}).getWrapText
207
+ end
208
+
209
+ should "set indentation to 6" do
210
+ assert_equal 6, create_cell_style(create_excel_workbook, {:indentation => 6}).getIndention
211
+ end
212
+ end
213
+ end
metadata ADDED
@@ -0,0 +1,161 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: poi_pond
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Lance Gleason
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-04-10 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ type: :runtime
23
+ prerelease: false
24
+ name: rjb
25
+ version_requirements: &id001 !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - "="
29
+ - !ruby/object:Gem::Version
30
+ hash: 31
31
+ segments:
32
+ - 1
33
+ - 3
34
+ - 2
35
+ version: 1.3.2
36
+ requirement: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ type: :development
39
+ prerelease: false
40
+ name: shoulda
41
+ version_requirements: &id002 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ hash: 3
47
+ segments:
48
+ - 0
49
+ version: "0"
50
+ requirement: *id002
51
+ - !ruby/object:Gem::Dependency
52
+ type: :development
53
+ prerelease: false
54
+ name: bundler
55
+ version_requirements: &id003 !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ~>
59
+ - !ruby/object:Gem::Version
60
+ hash: 23
61
+ segments:
62
+ - 1
63
+ - 0
64
+ - 0
65
+ version: 1.0.0
66
+ requirement: *id003
67
+ - !ruby/object:Gem::Dependency
68
+ type: :development
69
+ prerelease: false
70
+ name: jeweler
71
+ version_requirements: &id004 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ~>
75
+ - !ruby/object:Gem::Version
76
+ hash: 7
77
+ segments:
78
+ - 1
79
+ - 5
80
+ - 2
81
+ version: 1.5.2
82
+ requirement: *id004
83
+ - !ruby/object:Gem::Dependency
84
+ type: :development
85
+ prerelease: false
86
+ name: rcov
87
+ version_requirements: &id005 !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ hash: 3
93
+ segments:
94
+ - 0
95
+ version: "0"
96
+ requirement: *id005
97
+ description: "This gem wraps the "
98
+ email: lgleasain@yahoo.com
99
+ executables: []
100
+
101
+ extensions: []
102
+
103
+ extra_rdoc_files:
104
+ - LICENSE.txt
105
+ - README.rdoc
106
+ files:
107
+ - .document
108
+ - Gemfile
109
+ - LICENSE.txt
110
+ - README.rdoc
111
+ - Rakefile
112
+ - VERSION
113
+ - javalibs/poi-3.7-20101029.jar
114
+ - javalibs/poi-examples-3.7-20101029.jar
115
+ - javalibs/poi-ooxml-3.7-20101029.jar
116
+ - javalibs/poi-ooxml-schemas-3.7-20101029.jar
117
+ - javalibs/poi-scratchpad-3.7-20101029.jar
118
+ - lib/poi_pond.rb
119
+ - lib/style.rb
120
+ - test/helper.rb
121
+ - test/image001.jpg
122
+ - test/test_poi_pond.rb
123
+ - test/test_style.rb
124
+ has_rdoc: true
125
+ homepage: http://github.com/lgleasain/poi_pond
126
+ licenses:
127
+ - MIT
128
+ post_install_message:
129
+ rdoc_options: []
130
+
131
+ require_paths:
132
+ - lib
133
+ required_ruby_version: !ruby/object:Gem::Requirement
134
+ none: false
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ hash: 3
139
+ segments:
140
+ - 0
141
+ version: "0"
142
+ required_rubygems_version: !ruby/object:Gem::Requirement
143
+ none: false
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ hash: 3
148
+ segments:
149
+ - 0
150
+ version: "0"
151
+ requirements: []
152
+
153
+ rubyforge_project:
154
+ rubygems_version: 1.3.7
155
+ signing_key:
156
+ specification_version: 3
157
+ summary: A gem to user POI in a native (non-jruby) environment
158
+ test_files:
159
+ - test/helper.rb
160
+ - test/test_poi_pond.rb
161
+ - test/test_style.rb