writeexcel 0.1.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (143) hide show
  1. data/README +26 -31
  2. data/examples/a_simple.rb +42 -42
  3. data/examples/{autofilters.rb → autofilter.rb} +264 -266
  4. data/examples/bigfile.rb +29 -0
  5. data/examples/chart_area.rb +120 -0
  6. data/examples/chart_bar.rb +119 -0
  7. data/examples/chart_column.rb +119 -0
  8. data/examples/chart_line.rb +119 -0
  9. data/examples/chart_pie.rb +107 -0
  10. data/examples/chart_scatter.rb +120 -0
  11. data/examples/chart_stock.rb +147 -0
  12. data/examples/copyformat.rb +51 -51
  13. data/examples/data_validate.rb +278 -278
  14. data/examples/date_time.rb +86 -86
  15. data/examples/defined_name.rb +31 -0
  16. data/examples/demo.rb +120 -118
  17. data/examples/diag_border.rb +35 -35
  18. data/examples/formats.rb +489 -489
  19. data/examples/header.rb +136 -136
  20. data/examples/hidden.rb +28 -28
  21. data/examples/hyperlink.rb +42 -42
  22. data/examples/images.rb +52 -52
  23. data/examples/merge1.rb +39 -39
  24. data/examples/merge2.rb +44 -44
  25. data/examples/merge3.rb +65 -65
  26. data/examples/merge4.rb +82 -82
  27. data/examples/merge5.rb +79 -79
  28. data/examples/properties.rb +33 -0
  29. data/examples/properties_jp.rb +32 -0
  30. data/examples/protection.rb +46 -46
  31. data/examples/regions.rb +52 -52
  32. data/examples/repeat.rb +42 -42
  33. data/examples/stats.rb +75 -75
  34. data/examples/stocks.rb +80 -80
  35. data/examples/tab_colors.rb +30 -30
  36. data/examples/write_arrays.rb +82 -0
  37. data/lib/writeexcel.rb +1134 -18
  38. data/lib/writeexcel/biffwriter.rb +273 -260
  39. data/lib/writeexcel/chart.rb +2306 -217
  40. data/lib/writeexcel/charts/area.rb +152 -0
  41. data/lib/writeexcel/charts/bar.rb +177 -0
  42. data/lib/writeexcel/charts/column.rb +156 -0
  43. data/lib/writeexcel/charts/external.rb +61 -0
  44. data/lib/writeexcel/charts/line.rb +152 -0
  45. data/lib/writeexcel/charts/pie.rb +169 -0
  46. data/lib/writeexcel/charts/scatter.rb +192 -0
  47. data/lib/writeexcel/charts/stock.rb +211 -0
  48. data/lib/writeexcel/excelformulaparser.rb +208 -195
  49. data/lib/writeexcel/format.rb +1697 -1108
  50. data/lib/writeexcel/formula.rb +1050 -986
  51. data/lib/writeexcel/olewriter.rb +322 -322
  52. data/lib/writeexcel/properties.rb +251 -250
  53. data/lib/writeexcel/storage_lite.rb +968 -0
  54. data/lib/writeexcel/workbook.rb +3294 -2630
  55. data/lib/writeexcel/worksheet.rb +9012 -6377
  56. data/test/excelfile/Chart1.xls +0 -0
  57. data/test/excelfile/Chart2.xls +0 -0
  58. data/test/excelfile/Chart3.xls +0 -0
  59. data/test/excelfile/Chart4.xls +0 -0
  60. data/test/excelfile/Chart5.xls +0 -0
  61. data/test/perl_output/Chart1.xls.data +0 -0
  62. data/test/perl_output/Chart2.xls.data +0 -0
  63. data/test/perl_output/Chart3.xls.data +0 -0
  64. data/test/perl_output/Chart4.xls.data +0 -0
  65. data/test/perl_output/Chart5.xls.data +0 -0
  66. data/test/perl_output/a_simple.xls +0 -0
  67. data/test/perl_output/autofilter.xls +0 -0
  68. data/test/perl_output/chart_area.xls +0 -0
  69. data/test/perl_output/chart_bar.xls +0 -0
  70. data/test/perl_output/chart_column.xls +0 -0
  71. data/test/perl_output/chart_line.xls +0 -0
  72. data/test/perl_output/data_validate.xls +0 -0
  73. data/test/perl_output/date_time.xls +0 -0
  74. data/test/perl_output/demo.xls +0 -0
  75. data/test/perl_output/demo101.bin +0 -0
  76. data/test/perl_output/demo201.bin +0 -0
  77. data/test/perl_output/demo301.bin +0 -0
  78. data/test/perl_output/demo401.bin +0 -0
  79. data/test/perl_output/demo501.bin +0 -0
  80. data/test/perl_output/diag_border.xls +0 -0
  81. data/test/perl_output/headers.xls +0 -0
  82. data/test/perl_output/hyperlink.xls +0 -0
  83. data/test/perl_output/images.xls +0 -0
  84. data/test/perl_output/merge1.xls +0 -0
  85. data/test/perl_output/merge2.xls +0 -0
  86. data/test/perl_output/merge3.xls +0 -0
  87. data/test/perl_output/merge4.xls +0 -0
  88. data/test/perl_output/merge5.xls +0 -0
  89. data/test/perl_output/protection.xls +0 -0
  90. data/test/perl_output/regions.xls +0 -0
  91. data/test/perl_output/stats.xls +0 -0
  92. data/test/perl_output/stocks.xls +0 -0
  93. data/test/perl_output/tab_colors.xls +0 -0
  94. data/test/perl_output/unicode_cyrillic.xls +0 -0
  95. data/test/perl_output/workbook1.xls +0 -0
  96. data/test/perl_output/workbook2.xls +0 -0
  97. data/test/tc_all.rb +32 -31
  98. data/test/tc_biff.rb +104 -104
  99. data/test/tc_chart.rb +22 -22
  100. data/test/tc_example_match.rb +1944 -1280
  101. data/test/tc_format.rb +1254 -1267
  102. data/test/tc_formula.rb +63 -63
  103. data/test/tc_ole.rb +110 -110
  104. data/test/tc_storage_lite.rb +149 -0
  105. data/test/tc_workbook.rb +140 -115
  106. data/test/tc_worksheet.rb +115 -115
  107. data/test/test_00_IEEE_double.rb +14 -14
  108. data/test/test_01_add_worksheet.rb +12 -12
  109. data/test/test_02_merge_formats.rb +58 -58
  110. data/test/test_04_dimensions.rb +397 -397
  111. data/test/test_05_rows.rb +182 -182
  112. data/test/test_06_extsst.rb +80 -80
  113. data/test/test_11_date_time.rb +484 -484
  114. data/test/test_12_date_only.rb +506 -506
  115. data/test/test_13_date_seconds.rb +486 -486
  116. data/test/test_21_escher.rb +642 -629
  117. data/test/test_22_mso_drawing_group.rb +750 -739
  118. data/test/test_23_note.rb +78 -78
  119. data/test/test_24_txo.rb +80 -80
  120. data/test/test_25_position_object.rb +82 -0
  121. data/test/test_26_autofilter.rb +327 -327
  122. data/test/test_27_autofilter.rb +144 -144
  123. data/test/test_28_autofilter.rb +174 -174
  124. data/test/test_29_process_jpg.rb +681 -131
  125. data/test/test_30_validation_dval.rb +82 -82
  126. data/test/test_31_validation_dv_strings.rb +131 -131
  127. data/test/test_32_validation_dv_formula.rb +211 -211
  128. data/test/test_40_property_types.rb +191 -191
  129. data/test/test_41_properties.rb +238 -238
  130. data/test/test_42_set_properties.rb +442 -419
  131. data/test/test_50_name_stored.rb +305 -0
  132. data/test/test_51_name_print_area.rb +363 -0
  133. data/test/test_52_name_print_titles.rb +460 -0
  134. data/test/test_53_autofilter.rb +209 -0
  135. data/test/test_60_chart_generic.rb +576 -0
  136. data/test/test_61_chart_subclasses.rb +97 -0
  137. data/test/test_62_chart_formats.rb +270 -0
  138. data/test/test_63_chart_area_formats.rb +647 -0
  139. data/test/test_chartex.rb +35 -0
  140. data/test/ts_all.rb +46 -34
  141. data/writeexcel.gemspec +18 -0
  142. data/writeexcel.rdoc +583 -0
  143. metadata +162 -108
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
data/test/tc_all.rb CHANGED
@@ -1,31 +1,32 @@
1
- require "tc_biff"
2
- require "tc_ole"
3
- require "tc_workbook"
4
- require "tc_worksheet"
5
- require "tc_format"
6
- require "tc_formula"
7
- require 'tc_chart'
8
- require "test_00_IEEE_double"
9
- require 'test_01_add_worksheet'
10
- require 'test_02_merge_formats'
11
- require 'test_04_dimensions'
12
- require 'test_05_rows'
13
- require 'test_06_extsst'
14
- require 'test_11_date_time'
15
- require 'test_12_date_only'
16
- require 'test_13_date_seconds'
17
- require 'test_21_escher'
18
- require 'test_22_mso_drawing_group'
19
- require 'test_23_note'
20
- require 'test_24_txo'
21
- require 'test_26_autofilter'
22
- require 'test_27_autofilter'
23
- require 'test_28_autofilter'
24
- require 'test_29_process_jpg'
25
- require 'test_30_validation_dval'
26
- require 'test_31_validation_dv_strings'
27
- require 'test_32_validation_dv_formula'
28
- require 'test_40_property_types'
29
- require 'test_41_properties'
30
- require 'test_42_set_properties'
31
- require 'tc_example_match'
1
+ require "tc_biff"
2
+ require "tc_ole"
3
+ require "tc_workbook"
4
+ require "tc_worksheet"
5
+ require "tc_format"
6
+ require "tc_formula"
7
+ require 'tc_chart'
8
+ require "test_00_IEEE_double"
9
+ require 'test_01_add_worksheet'
10
+ require 'test_02_merge_formats'
11
+ require 'test_04_dimensions'
12
+ require 'test_05_rows'
13
+ require 'test_06_extsst'
14
+ require 'test_11_date_time'
15
+ require 'test_12_date_only'
16
+ require 'test_13_date_seconds'
17
+ require 'test_21_escher'
18
+ require 'test_22_mso_drawing_group'
19
+ require 'test_23_note'
20
+ require 'test_24_txo'
21
+ require 'test_26_autofilter'
22
+ require 'test_27_autofilter'
23
+ require 'test_28_autofilter'
24
+ require 'test_29_process_jpg'
25
+ require 'test_30_validation_dval'
26
+ require 'test_31_validation_dv_strings'
27
+ require 'test_32_validation_dv_formula'
28
+ require 'test_40_property_types'
29
+ require 'test_41_properties'
30
+ require 'test_42_set_properties'
31
+ require 'tc_example_match'
32
+ require 'tc_storage_lite'
data/test/tc_biff.rb CHANGED
@@ -1,104 +1,104 @@
1
- $LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib"
2
-
3
- require "test/unit"
4
- require "writeexcel"
5
-
6
- class TC_BIFFWriter < Test::Unit::TestCase
7
-
8
- TEST_DIR = File.expand_path(File.dirname(__FILE__))
9
- PERL_OUTDIR = File.join(TEST_DIR, 'perl_output')
10
-
11
- def setup
12
- t = Time.now.strftime("%Y%m%d")
13
- path = "temp#{t}-#{$$}-#{rand(0x100000000).to_s(36)}"
14
- @test_file = File.join(Dir.tmpdir, path)
15
- @biff = BIFFWriter.new
16
- @ruby_file = @test_file
17
- end
18
-
19
- def test_append_no_error
20
- assert_nothing_raised{ @biff.append("World") }
21
- end
22
-
23
- def test_prepend_no_error
24
- assert_nothing_raised{ @biff.prepend("Hello") }
25
- end
26
-
27
- def test_data_added
28
- assert_nothing_raised{ @biff.append("Hello", "World") }
29
- data = ''
30
- while d = @biff.get_data
31
- data = data + d
32
- end
33
- assert_equal("HelloWorld", data, "Bad data contents")
34
- assert_equal(10, @biff.datasize, "Bad data size")
35
- end
36
-
37
- def test_data_prepended
38
-
39
- assert_nothing_raised{ @biff.append("Hello") }
40
- assert_nothing_raised{ @biff.prepend("World") }
41
- data = ''
42
- while d = @biff.get_data
43
- data = data + d
44
- end
45
- assert_equal("WorldHello", data, "Bad data contents")
46
- assert_equal(10, @biff.datasize, "Bad data size")
47
- end
48
-
49
- def test_store_bof_length
50
- assert_nothing_raised{ @biff.store_bof }
51
- assert_equal(20, @biff.datasize, "Bad data size after store_bof call")
52
- end
53
-
54
- def test_store_eof_length
55
- assert_nothing_raised{ @biff.store_eof }
56
- assert_equal(4, @biff.datasize, "Bad data size after store_eof call")
57
- end
58
-
59
- def test_datasize_mixed
60
- assert_nothing_raised{ @biff.append("Hello") }
61
- assert_nothing_raised{ @biff.prepend("World") }
62
- assert_nothing_raised{ @biff.store_bof }
63
- assert_nothing_raised{ @biff.store_eof }
64
- assert_equal(34, @biff.datasize, "Bad data size for mixed data")
65
- end
66
-
67
- def test_add_continue
68
- perl_file = "#{PERL_OUTDIR}/biff_add_continue_testdata"
69
- size = File.size(perl_file)
70
- @fh = File.new(@ruby_file,"w+")
71
- @fh.print(@biff.add_continue('testdata'))
72
- @fh.close
73
- rsize = File.size(@ruby_file)
74
- assert_equal(size,rsize,"File sizes not the same")
75
- compare_file(perl_file, @ruby_file)
76
- end
77
-
78
- def teardown
79
- @biff = nil
80
- File.delete(@ruby_file) if File.exist?(@ruby_file)
81
- end
82
-
83
- def compare_file(expected, target)
84
- fh_e = File.open(expected, "r")
85
- fh_t = File.open(target, "r")
86
- while true do
87
- e1 = fh_e.read(1)
88
- t1 = fh_t.read(1)
89
- if e1.nil?
90
- assert( t1.nil?, "#{expexted} is EOF but #{target} is NOT EOF.")
91
- break
92
- elsif t1.nil?
93
- assert( e1.nil?, '#{target} is EOF but #{expected} is NOT EOF.')
94
- break
95
- end
96
- assert_equal(e1, t1, sprintf(" #{expected} = '%s' but #{target} = '%s'", e1, t1))
97
- break
98
- end
99
- fh_e.close
100
- fh_t.close
101
- end
102
-
103
-
104
- end
1
+ $LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib"
2
+
3
+ require "test/unit"
4
+ require "writeexcel"
5
+
6
+ class TC_BIFFWriter < Test::Unit::TestCase
7
+
8
+ TEST_DIR = File.expand_path(File.dirname(__FILE__))
9
+ PERL_OUTDIR = File.join(TEST_DIR, 'perl_output')
10
+
11
+ def setup
12
+ t = Time.now.strftime("%Y%m%d")
13
+ path = "temp#{t}-#{$$}-#{rand(0x100000000).to_s(36)}"
14
+ @test_file = File.join(Dir.tmpdir, path)
15
+ @biff = BIFFWriter.new
16
+ @ruby_file = @test_file
17
+ end
18
+
19
+ def test_append_no_error
20
+ assert_nothing_raised{ @biff.append("World") }
21
+ end
22
+
23
+ def test_prepend_no_error
24
+ assert_nothing_raised{ @biff.prepend("Hello") }
25
+ end
26
+
27
+ def test_data_added
28
+ assert_nothing_raised{ @biff.append("Hello", "World") }
29
+ data = ''
30
+ while d = @biff.get_data
31
+ data = data + d
32
+ end
33
+ assert_equal("HelloWorld", data, "Bad data contents")
34
+ assert_equal(10, @biff.datasize, "Bad data size")
35
+ end
36
+
37
+ def test_data_prepended
38
+
39
+ assert_nothing_raised{ @biff.append("Hello") }
40
+ assert_nothing_raised{ @biff.prepend("World") }
41
+ data = ''
42
+ while d = @biff.get_data
43
+ data = data + d
44
+ end
45
+ assert_equal("WorldHello", data, "Bad data contents")
46
+ assert_equal(10, @biff.datasize, "Bad data size")
47
+ end
48
+
49
+ def test_store_bof_length
50
+ assert_nothing_raised{ @biff.store_bof }
51
+ assert_equal(20, @biff.datasize, "Bad data size after store_bof call")
52
+ end
53
+
54
+ def test_store_eof_length
55
+ assert_nothing_raised{ @biff.store_eof }
56
+ assert_equal(4, @biff.datasize, "Bad data size after store_eof call")
57
+ end
58
+
59
+ def test_datasize_mixed
60
+ assert_nothing_raised{ @biff.append("Hello") }
61
+ assert_nothing_raised{ @biff.prepend("World") }
62
+ assert_nothing_raised{ @biff.store_bof }
63
+ assert_nothing_raised{ @biff.store_eof }
64
+ assert_equal(34, @biff.datasize, "Bad data size for mixed data")
65
+ end
66
+
67
+ def test_add_continue
68
+ perl_file = "#{PERL_OUTDIR}/biff_add_continue_testdata"
69
+ size = File.size(perl_file)
70
+ @fh = File.new(@ruby_file,"w+")
71
+ @fh.print(@biff.add_continue('testdata'))
72
+ @fh.close
73
+ rsize = File.size(@ruby_file)
74
+ assert_equal(size,rsize,"File sizes not the same")
75
+ compare_file(perl_file, @ruby_file)
76
+ end
77
+
78
+ def teardown
79
+ @biff = nil
80
+ File.delete(@ruby_file) if File.exist?(@ruby_file)
81
+ end
82
+
83
+ def compare_file(expected, target)
84
+ fh_e = File.open(expected, "r")
85
+ fh_t = File.open(target, "r")
86
+ while true do
87
+ e1 = fh_e.read(1)
88
+ t1 = fh_t.read(1)
89
+ if e1.nil?
90
+ assert( t1.nil?, "#{expexted} is EOF but #{target} is NOT EOF.")
91
+ break
92
+ elsif t1.nil?
93
+ assert( e1.nil?, '#{target} is EOF but #{expected} is NOT EOF.')
94
+ break
95
+ end
96
+ assert_equal(e1, t1, sprintf(" #{expected} = '%s' but #{target} = '%s'", e1, t1))
97
+ break
98
+ end
99
+ fh_e.close
100
+ fh_t.close
101
+ end
102
+
103
+
104
+ end
data/test/tc_chart.rb CHANGED
@@ -1,22 +1,22 @@
1
- #####################################################
2
- # tc_chart.rb
3
- #
4
- # Test suite for the Chart class (chart.rb)
5
- #####################################################
6
- base = File.basename(Dir.pwd)
7
- if base == "test" || base =~ /spreadsheet/i
8
- Dir.chdir("..") if base == "test"
9
- $LOAD_PATH.unshift(Dir.pwd + "/lib/spreadsheet")
10
- Dir.chdir("test") rescue nil
11
- end
12
-
13
- require "test/unit"
14
- require "writeexcel"
15
-
16
- class TC_Chart < Test::Unit::TestCase
17
-
18
- def test_
19
- assert(true)
20
- end
21
-
22
- end
1
+ #####################################################
2
+ # tc_chart.rb
3
+ #
4
+ # Test suite for the Chart class (chart.rb)
5
+ #####################################################
6
+ base = File.basename(Dir.pwd)
7
+ if base == "test" || base =~ /spreadsheet/i
8
+ Dir.chdir("..") if base == "test"
9
+ $LOAD_PATH.unshift(Dir.pwd + "/lib/spreadsheet")
10
+ Dir.chdir("test") rescue nil
11
+ end
12
+
13
+ require "test/unit"
14
+ require "writeexcel"
15
+
16
+ class TC_Chart < Test::Unit::TestCase
17
+
18
+ def test_
19
+ assert(true)
20
+ end
21
+
22
+ end
@@ -1,1280 +1,1944 @@
1
- $LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib"
2
-
3
- require "test/unit"
4
- require 'writeexcel'
5
-
6
- class TC_example_match < Test::Unit::TestCase
7
-
8
- TEST_DIR = File.expand_path(File.dirname(__FILE__))
9
- PERL_OUTDIR = File.join(TEST_DIR, 'perl_output')
10
-
11
- def setup
12
- t = Time.now.strftime("%Y%m%d")
13
- path = "temp#{t}-#{$$}-#{rand(0x100000000).to_s(36)}"
14
- @test_file = File.join(Dir.tmpdir, path)
15
- @filename = @test_file
16
- @filename2 = @test_file + "2"
17
- end
18
-
19
- def teardown
20
- File.delete(@filename) if File.exist?(@filename)
21
- File.delete(@filename2) if File.exist?(@filename2)
22
- end
23
-
24
- def test_a_simple
25
- workbook = Spreadsheet::WriteExcel.new(@filename);
26
- worksheet = workbook.add_worksheet
27
-
28
- # The general syntax is write(row, column, token). Note that row and
29
- # column are zero indexed
30
- #
31
-
32
- # Write some text
33
- worksheet.write(0, 0, "Hi Excel!")
34
-
35
-
36
- # Write some numbers
37
- worksheet.write(2, 0, 3) # Writes 3
38
- worksheet.write(3, 0, 3.00000) # Writes 3
39
- worksheet.write(4, 0, 3.00001) # Writes 3.00001
40
- worksheet.write(5, 0, 3.14159) # TeX revision no.?
41
-
42
-
43
- # Write some formulas
44
- worksheet.write(7, 0, '=A3 + A6')
45
- worksheet.write(8, 0, '=IF(A5>3,"Yes", "No")')
46
-
47
-
48
- # Write a hyperlink
49
- worksheet.write(10, 0, 'http://www.perl.com/')
50
-
51
- # File save
52
- workbook.close
53
-
54
- # do assertion
55
- compare_file("#{PERL_OUTDIR}/a_simple.xls", @filename)
56
- end
57
-
58
- def test_regions
59
- workbook = Spreadsheet::WriteExcel.new(@filename)
60
-
61
- # Add some worksheets
62
- north = workbook.add_worksheet("North")
63
- south = workbook.add_worksheet("South")
64
- east = workbook.add_worksheet("East")
65
- west = workbook.add_worksheet("West")
66
-
67
- # Add a Format
68
- format = workbook.add_format()
69
- format.set_bold()
70
- format.set_color('blue')
71
-
72
- # Add a caption to each worksheet
73
- workbook.sheets.each do |worksheet|
74
- worksheet.write(0, 0, "Sales", format)
75
- end
76
-
77
- # Write some data
78
- north.write(0, 1, 200000)
79
- south.write(0, 1, 100000)
80
- east.write(0, 1, 150000)
81
- west.write(0, 1, 100000)
82
-
83
- # Set the active worksheet
84
- bp=1
85
- south.activate()
86
-
87
- # Set the width of the first column
88
- south.set_column(0, 0, 20)
89
-
90
- # Set the active cell
91
- south.set_selection(0, 1)
92
-
93
- workbook.close
94
-
95
- # do assertion
96
- compare_file("#{PERL_OUTDIR}/regions.xls", @filename)
97
- end
98
-
99
- def test_stats
100
- workbook = Spreadsheet::WriteExcel.new(@filename)
101
- worksheet = workbook.add_worksheet('Test data')
102
-
103
- # Set the column width for columns 1
104
- worksheet.set_column(0, 0, 20)
105
-
106
- # Create a format for the headings
107
- format = workbook.add_format
108
- format.set_bold
109
-
110
- # Write the sample data
111
- worksheet.write(0, 0, 'Sample', format)
112
- worksheet.write(0, 1, 1)
113
- worksheet.write(0, 2, 2)
114
- worksheet.write(0, 3, 3)
115
- worksheet.write(0, 4, 4)
116
- worksheet.write(0, 5, 5)
117
- worksheet.write(0, 6, 6)
118
- worksheet.write(0, 7, 7)
119
- worksheet.write(0, 8, 8)
120
-
121
- worksheet.write(1, 0, 'Length', format)
122
- worksheet.write(1, 1, 25.4)
123
- worksheet.write(1, 2, 25.4)
124
- worksheet.write(1, 3, 24.8)
125
- worksheet.write(1, 4, 25.0)
126
- worksheet.write(1, 5, 25.3)
127
- worksheet.write(1, 6, 24.9)
128
- worksheet.write(1, 7, 25.2)
129
- worksheet.write(1, 8, 24.8)
130
-
131
- # Write some statistical functions
132
- worksheet.write(4, 0, 'Count', format)
133
- worksheet.write(4, 1, '=COUNT(B1:I1)')
134
-
135
- worksheet.write(5, 0, 'Sum', format)
136
- worksheet.write(5, 1, '=SUM(B2:I2)')
137
-
138
- worksheet.write(6, 0, 'Average', format)
139
- worksheet.write(6, 1, '=AVERAGE(B2:I2)')
140
-
141
- worksheet.write(7, 0, 'Min', format)
142
- worksheet.write(7, 1, '=MIN(B2:I2)')
143
-
144
- worksheet.write(8, 0, 'Max', format)
145
- worksheet.write(8, 1, '=MAX(B2:I2)')
146
-
147
- worksheet.write(9, 0, 'Standard Deviation', format)
148
- worksheet.write(9, 1, '=STDEV(B2:I2)')
149
-
150
- worksheet.write(10, 0, 'Kurtosis', format)
151
- worksheet.write(10, 1, '=KURT(B2:I2)')
152
-
153
- workbook.close
154
-
155
- # do assertion
156
- compare_file("#{PERL_OUTDIR}/regions.xls", @filename)
157
- end
158
-
159
- def test_hidden
160
- workbook = Spreadsheet::WriteExcel.new(@filename)
161
- worksheet1 = workbook.add_worksheet
162
- worksheet2 = workbook.add_worksheet
163
- worksheet3 = workbook.add_worksheet
164
-
165
- # Sheet2 won't be visible until it is unhidden in Excel.
166
- worksheet2.hide
167
-
168
- worksheet1.write(0, 0, 'Sheet2 is hidden')
169
- worksheet2.write(0, 0, 'How did you find me?')
170
- worksheet3.write(0, 0, 'Sheet2 is hidden')
171
-
172
- workbook.close
173
-
174
- # do assertion
175
- compare_file("#{PERL_OUTDIR}/hidden.xls", @filename)
176
- end
177
-
178
- def test_hyperlink1
179
- # Create a new workbook and add a worksheet
180
- workbook = Spreadsheet::WriteExcel.new(@filename)
181
- worksheet = workbook.add_worksheet('Hyperlinks')
182
-
183
- # Format the first column
184
- worksheet.set_column('A:A', 30)
185
- worksheet.set_selection('B1')
186
-
187
-
188
- # Add a sample format
189
- format = workbook.add_format
190
- format.set_size(12)
191
- format.set_bold
192
- format.set_color('red')
193
- format.set_underline
194
-
195
-
196
- # Write some hyperlinks
197
- worksheet.write('A1', 'http://www.perl.com/' )
198
- worksheet.write('A3', 'http://www.perl.com/', 'Perl home' )
199
- worksheet.write('A5', 'http://www.perl.com/', nil, format)
200
- worksheet.write('A7', 'mailto:jmcnamara@cpan.org', 'Mail me')
201
-
202
- # Write a URL that isn't a hyperlink
203
- worksheet.write_string('A9', 'http://www.perl.com/')
204
-
205
- workbook.close
206
-
207
- # do assertion
208
- compare_file("#{PERL_OUTDIR}/hyperlink.xls", @filename)
209
- end
210
-
211
- def test_copyformat
212
- # Create workbook1
213
- workbook1 = Spreadsheet::WriteExcel.new(@filename)
214
- worksheet1 = workbook1.add_worksheet
215
- format1a = workbook1.add_format
216
- format1b = workbook1.add_format
217
-
218
- # Create workbook2
219
- workbook2 = Spreadsheet::WriteExcel.new(@filename2)
220
- worksheet2 = workbook2.add_worksheet
221
- format2a = workbook2.add_format
222
- format2b = workbook2.add_format
223
-
224
- # Create a global format object that isn't tied to a workbook
225
- global_format = Format.new
226
-
227
- # Set the formatting
228
- global_format.set_color('blue')
229
- global_format.set_bold
230
- global_format.set_italic
231
-
232
- # Create another example format
233
- format1b.set_color('red')
234
-
235
- # Copy the global format properties to the worksheet formats
236
- format1a.copy(global_format)
237
- format2a.copy(global_format)
238
-
239
- # Copy a format from worksheet1 to worksheet2
240
- format2b.copy(format1b)
241
-
242
- # Write some output
243
- worksheet1.write(0, 0, "Ciao", format1a)
244
- worksheet1.write(1, 0, "Ciao", format1b)
245
-
246
- worksheet2.write(0, 0, "Hello", format2a)
247
- worksheet2.write(1, 0, "Hello", format2b)
248
- workbook1.close
249
- workbook2.close
250
-
251
- # do assertion
252
- compare_file("#{PERL_OUTDIR}/workbook1.xls", @filename)
253
- compare_file("#{PERL_OUTDIR}/workbook2.xls", @filename2)
254
- end
255
-
256
- def test_data_validate
257
- workbook = Spreadsheet::WriteExcel.new(@filename)
258
- worksheet = workbook.add_worksheet
259
-
260
- # Add a format for the header cells.
261
- header_format = workbook.add_format(
262
- :border => 1,
263
- :bg_color => 43,
264
- :bold => 1,
265
- :text_wrap => 1,
266
- :valign => 'vcenter',
267
- :indent => 1
268
- )
269
-
270
- # Set up layout of the worksheet.
271
- worksheet.set_column('A:A', 64)
272
- worksheet.set_column('B:B', 15)
273
- worksheet.set_column('D:D', 15)
274
- worksheet.set_row(0, 36)
275
- worksheet.set_selection('B3')
276
-
277
-
278
- # Write the header cells and some data that will be used in the examples.
279
- row = 0
280
- heading1 = 'Some examples of data validation in Spreadsheet::WriteExcel'
281
- heading2 = 'Enter values in this column'
282
- heading3 = 'Sample Data'
283
-
284
- worksheet.write('A1', heading1, header_format)
285
- worksheet.write('B1', heading2, header_format)
286
- worksheet.write('D1', heading3, header_format)
287
-
288
- worksheet.write('D3', ['Integers', 1, 10])
289
- worksheet.write('D4', ['List data', 'open', 'high', 'close'])
290
- worksheet.write('D5', ['Formula', '=AND(F5=50,G5=60)', 50, 60])
291
-
292
-
293
- #
294
- # Example 1. Limiting input to an integer in a fixed range.
295
- #
296
- txt = 'Enter an integer between 1 and 10'
297
- row += 2
298
-
299
- worksheet.write(row, 0, txt)
300
- worksheet.data_validation(row, 1,
301
- {
302
- :validate => 'integer',
303
- :criteria => 'between',
304
- :minimum => 1,
305
- :maximum => 10
306
- })
307
-
308
-
309
- #
310
- # Example 2. Limiting input to an integer outside a fixed range.
311
- #
312
- txt = 'Enter an integer that is not between 1 and 10 (using cell references)'
313
- row += 2
314
-
315
- worksheet.write(row, 0, txt)
316
- worksheet.data_validation(row, 1,
317
- {
318
- :validate => 'integer',
319
- :criteria => 'not between',
320
- :minimum => '=E3',
321
- :maximum => '=F3'
322
- })
323
-
324
-
325
- #
326
- # Example 3. Limiting input to an integer greater than a fixed value.
327
- #
328
- txt = 'Enter an integer greater than 0'
329
- row += 2
330
-
331
- worksheet.write(row, 0, txt)
332
- worksheet.data_validation(row, 1,
333
- {
334
- :validate => 'integer',
335
- :criteria => '>',
336
- :value => 0
337
- })
338
-
339
-
340
- #
341
- # Example 4. Limiting input to an integer less than a fixed value.
342
- #
343
- txt = 'Enter an integer less than 10'
344
- row += 2
345
-
346
- worksheet.write(row, 0, txt)
347
- worksheet.data_validation(row, 1,
348
- {
349
- :validate => 'integer',
350
- :criteria => '<',
351
- :value => 10
352
- })
353
-
354
-
355
- #
356
- # Example 5. Limiting input to a decimal in a fixed range.
357
- #
358
- txt = 'Enter a decimal between 0.1 and 0.5'
359
- row += 2
360
-
361
- worksheet.write(row, 0, txt)
362
- worksheet.data_validation(row, 1,
363
- {
364
- :validate => 'decimal',
365
- :criteria => 'between',
366
- :minimum => 0.1,
367
- :maximum => 0.5
368
- })
369
-
370
-
371
- #
372
- # Example 6. Limiting input to a value in a dropdown list.
373
- #
374
- txt = 'Select a value from a drop down list'
375
- row += 2
376
- bp=1
377
- worksheet.write(row, 0, txt)
378
- worksheet.data_validation(row, 1,
379
- {
380
- :validate => 'list',
381
- :source => ['open', 'high', 'close']
382
- })
383
-
384
-
385
- #
386
- # Example 6. Limiting input to a value in a dropdown list.
387
- #
388
- txt = 'Select a value from a drop down list (using a cell range)'
389
- row += 2
390
-
391
- worksheet.write(row, 0, txt)
392
- worksheet.data_validation(row, 1,
393
- {
394
- :validate => 'list',
395
- :source => '=E4:G4'
396
- })
397
-
398
-
399
- #
400
- # Example 7. Limiting input to a date in a fixed range.
401
- #
402
- txt = 'Enter a date between 1/1/2008 and 12/12/2008'
403
- row += 2
404
-
405
- worksheet.write(row, 0, txt)
406
- worksheet.data_validation(row, 1,
407
- {
408
- :validate => 'date',
409
- :criteria => 'between',
410
- :minimum => '2008-01-01T',
411
- :maximum => '2008-12-12T'
412
- })
413
-
414
-
415
- #
416
- # Example 8. Limiting input to a time in a fixed range.
417
- #
418
- txt = 'Enter a time between 6:00 and 12:00'
419
- row += 2
420
-
421
- worksheet.write(row, 0, txt)
422
- worksheet.data_validation(row, 1,
423
- {
424
- :validate => 'time',
425
- :criteria => 'between',
426
- :minimum => 'T06:00',
427
- :maximum => 'T12:00'
428
- })
429
-
430
-
431
- #
432
- # Example 9. Limiting input to a string greater than a fixed length.
433
- #
434
- txt = 'Enter a string longer than 3 characters'
435
- row += 2
436
-
437
- worksheet.write(row, 0, txt)
438
- worksheet.data_validation(row, 1,
439
- {
440
- :validate => 'length',
441
- :criteria => '>',
442
- :value => 3
443
- })
444
-
445
-
446
- #
447
- # Example 10. Limiting input based on a formula.
448
- #
449
- txt = 'Enter a value if the following is true "=AND(F5=50,G5=60)"'
450
- row += 2
451
-
452
- worksheet.write(row, 0, txt)
453
- worksheet.data_validation(row, 1,
454
- {
455
- :validate => 'custom',
456
- :value => '=AND(F5=50,G5=60)'
457
- })
458
-
459
-
460
- #
461
- # Example 11. Displaying and modify data validation messages.
462
- #
463
- txt = 'Displays a message when you select the cell'
464
- row += 2
465
-
466
- worksheet.write(row, 0, txt)
467
- worksheet.data_validation(row, 1,
468
- {
469
- :validate => 'integer',
470
- :criteria => 'between',
471
- :minimum => 1,
472
- :maximum => 100,
473
- :input_title => 'Enter an integer:',
474
- :input_message => 'between 1 and 100'
475
- })
476
-
477
-
478
- #
479
- # Example 12. Displaying and modify data validation messages.
480
- #
481
- txt = 'Display a custom error message when integer isn\'t between 1 and 100'
482
- row += 2
483
-
484
- worksheet.write(row, 0, txt)
485
- worksheet.data_validation(row, 1,
486
- {
487
- :validate => 'integer',
488
- :criteria => 'between',
489
- :minimum => 1,
490
- :maximum => 100,
491
- :input_title => 'Enter an integer:',
492
- :input_message => 'between 1 and 100',
493
- :error_title => 'Input value is not valid!',
494
- :error_message => 'It should be an integer between 1 and 100'
495
- })
496
-
497
-
498
- #
499
- # Example 13. Displaying and modify data validation messages.
500
- #
501
- txt = 'Display a custom information message when integer isn\'t between 1 and 100'
502
- row += 2
503
-
504
- worksheet.write(row, 0, txt)
505
- worksheet.data_validation(row, 1,
506
- {
507
- :validate => 'integer',
508
- :criteria => 'between',
509
- :minimum => 1,
510
- :maximum => 100,
511
- :input_title => 'Enter an integer:',
512
- :input_message => 'between 1 and 100',
513
- :error_title => 'Input value is not valid!',
514
- :error_message => 'It should be an integer between 1 and 100',
515
- :error_type => 'information'
516
- })
517
-
518
- workbook.close
519
-
520
- # do assertion
521
- compare_file("#{PERL_OUTDIR}/data_validate.xls", @filename)
522
- end
523
-
524
- def test_merge1
525
- workbook = Spreadsheet::WriteExcel.new(@filename)
526
- worksheet = workbook.add_worksheet
527
-
528
- # Increase the cell size of the merged cells to highlight the formatting.
529
- worksheet.set_column('B:D', 20)
530
- worksheet.set_row(2, 30)
531
-
532
- # Create a merge format
533
- format = workbook.add_format(:center_across => 1)
534
-
535
- # Only one cell should contain text, the others should be blank.
536
- worksheet.write(2, 1, "Center across selection", format)
537
- worksheet.write_blank(2, 2, format)
538
- worksheet.write_blank(2, 3, format)
539
-
540
- workbook.close
541
-
542
- # do assertion
543
- compare_file("#{PERL_OUTDIR}/merge1.xls", @filename)
544
- end
545
-
546
- def test_merge2
547
- workbook = Spreadsheet::WriteExcel.new(@filename)
548
- worksheet = workbook.add_worksheet
549
-
550
- # Increase the cell size of the merged cells to highlight the formatting.
551
- worksheet.set_column(1, 2, 30)
552
- worksheet.set_row(2, 40)
553
-
554
- # Create a merged format
555
- format = workbook.add_format(
556
- :center_across => 1,
557
- :bold => 1,
558
- :size => 15,
559
- :pattern => 1,
560
- :border => 6,
561
- :color => 'white',
562
- :fg_color => 'green',
563
- :border_color => 'yellow',
564
- :align => 'vcenter'
565
- )
566
-
567
- # Only one cell should contain text, the others should be blank.
568
- worksheet.write(2, 1, "Center across selection", format)
569
- worksheet.write_blank(2, 2, format)
570
- workbook.close
571
-
572
- # do assertion
573
- compare_file("#{PERL_OUTDIR}/merge2.xls", @filename)
574
- end
575
-
576
- def test_merge3
577
- workbook = Spreadsheet::WriteExcel.new(@filename)
578
- worksheet = workbook.add_worksheet()
579
-
580
- # Increase the cell size of the merged cells to highlight the formatting.
581
- [1, 3,6,7].each { |row| worksheet.set_row(row, 30) }
582
- worksheet.set_column('B:D', 20)
583
-
584
- ###############################################################################
585
- #
586
- # Example 1: Merge cells containing a hyperlink using write_url_range()
587
- # and the standard Excel 5+ merge property.
588
- #
589
- format1 = workbook.add_format(
590
- :center_across => 1,
591
- :border => 1,
592
- :underline => 1,
593
- :color => 'blue'
594
- )
595
-
596
- # Write the cells to be merged
597
- worksheet.write_url_range('B2:D2', 'http://www.perl.com', format1)
598
- worksheet.write_blank('C2', format1)
599
- worksheet.write_blank('D2', format1)
600
-
601
-
602
-
603
- ###############################################################################
604
- #
605
- # Example 2: Merge cells containing a hyperlink using merge_range().
606
- #
607
- format2 = workbook.add_format(
608
- :border => 1,
609
- :underline => 1,
610
- :color => 'blue',
611
- :align => 'center',
612
- :valign => 'vcenter'
613
- )
614
-
615
- # Merge 3 cells
616
- worksheet.merge_range('B4:D4', 'http://www.perl.com', format2)
617
-
618
-
619
- # Merge 3 cells over two rows
620
- worksheet.merge_range('B7:D8', 'http://www.perl.com', format2)
621
-
622
- workbook.close
623
-
624
- # do assertion
625
- compare_file("#{PERL_OUTDIR}/merge3.xls", @filename)
626
- end
627
-
628
- def test_merge4
629
- # Create a new workbook and add a worksheet
630
- workbook = Spreadsheet::WriteExcel.new(@filename)
631
- worksheet = workbook.add_worksheet
632
-
633
- # Increase the cell size of the merged cells to highlight the formatting.
634
- (1..11).each { |row| worksheet.set_row(row, 30) }
635
- worksheet.set_column('B:D', 20)
636
-
637
- ###############################################################################
638
- #
639
- # Example 1: Text centered vertically and horizontally
640
- #
641
- format1 = workbook.add_format(
642
- :border => 6,
643
- :bold => 1,
644
- :color => 'red',
645
- :valign => 'vcenter',
646
- :align => 'center'
647
- )
648
-
649
- worksheet.merge_range('B2:D3', 'Vertical and horizontal', format1)
650
-
651
-
652
- ###############################################################################
653
- #
654
- # Example 2: Text aligned to the top and left
655
- #
656
- format2 = workbook.add_format(
657
- :border => 6,
658
- :bold => 1,
659
- :color => 'red',
660
- :valign => 'top',
661
- :align => 'left'
662
- )
663
-
664
- worksheet.merge_range('B5:D6', 'Aligned to the top and left', format2)
665
-
666
- ###############################################################################
667
- #
668
- # Example 3: Text aligned to the bottom and right
669
- #
670
- format3 = workbook.add_format(
671
- :border => 6,
672
- :bold => 1,
673
- :color => 'red',
674
- :valign => 'bottom',
675
- :align => 'right'
676
- )
677
-
678
- worksheet.merge_range('B8:D9', 'Aligned to the bottom and right', format3)
679
-
680
- ###############################################################################
681
- #
682
- # Example 4: Text justified (i.e. wrapped) in the cell
683
- #
684
- format4 = workbook.add_format(
685
- :border => 6,
686
- :bold => 1,
687
- :color => 'red',
688
- :valign => 'top',
689
- :align => 'justify'
690
- )
691
-
692
- worksheet.merge_range('B11:D12', 'Justified: '+'so on and '*18, format4)
693
-
694
- workbook.close
695
-
696
- # do assertion
697
- compare_file("#{PERL_OUTDIR}/merge4.xls", @filename)
698
- end
699
-
700
- def test_merge5
701
- # Create a new workbook and add a worksheet
702
- workbook = Spreadsheet::WriteExcel.new(@filename)
703
- worksheet = workbook.add_worksheet
704
-
705
-
706
- # Increase the cell size of the merged cells to highlight the formatting.
707
- (3..8).each { |col| worksheet.set_row(col, 36) }
708
- [1, 3, 5].each { |n| worksheet.set_column(n, n, 15) }
709
-
710
-
711
- ###############################################################################
712
- #
713
- # Rotation 1, letters run from top to bottom
714
- #
715
- format1 = workbook.add_format(
716
- :border => 6,
717
- :bold => 1,
718
- :color => 'red',
719
- :valign => 'vcentre',
720
- :align => 'centre',
721
- :rotation => 270
722
- )
723
-
724
-
725
- worksheet.merge_range('B4:B9', 'Rotation 270', format1)
726
-
727
-
728
- ###############################################################################
729
- #
730
- # Rotation 2, 90° anticlockwise
731
- #
732
- format2 = workbook.add_format(
733
- :border => 6,
734
- :bold => 1,
735
- :color => 'red',
736
- :valign => 'vcentre',
737
- :align => 'centre',
738
- :rotation => 90
739
- )
740
-
741
-
742
- worksheet.merge_range('D4:D9', 'Rotation 90°', format2)
743
-
744
-
745
-
746
- ###############################################################################
747
- #
748
- # Rotation 3, 90° clockwise
749
- #
750
- format3 = workbook.add_format(
751
- :border => 6,
752
- :bold => 1,
753
- :color => 'red',
754
- :valign => 'vcentre',
755
- :align => 'centre',
756
- :rotation => -90
757
- )
758
-
759
-
760
- worksheet.merge_range('F4:F9', 'Rotation -90°', format3)
761
-
762
- workbook.close
763
-
764
- # do assertion
765
- compare_file("#{PERL_OUTDIR}/merge5.xls", @filename)
766
- end
767
-
768
- def test_images
769
- # Create a new workbook called simple.xls and add a worksheet
770
- workbook = Spreadsheet::WriteExcel.new(@filename)
771
- worksheet1 = workbook.add_worksheet('Image 1')
772
- worksheet2 = workbook.add_worksheet('Image 2')
773
- worksheet3 = workbook.add_worksheet('Image 3')
774
- worksheet4 = workbook.add_worksheet('Image 4')
775
- bp=1
776
-
777
- # Insert a basic image
778
- worksheet1.write('A10', "Image inserted into worksheet.")
779
- worksheet1.insert_image('A1', File.join(TEST_DIR,'republic.png'))
780
-
781
-
782
- # Insert an image with an offset
783
- worksheet2.write('A10', "Image inserted with an offset.")
784
- worksheet2.insert_image('A1', File.join(TEST_DIR,'republic.png'), 32, 10)
785
-
786
- # Insert a scaled image
787
- worksheet3.write('A10', "Image scaled: width x 2, height x 0.8.")
788
- worksheet3.insert_image('A1', File.join(TEST_DIR,'republic.png'), 0, 0, 2, 0.8)
789
-
790
- # Insert an image over varied column and row sizes
791
- # This does not require any additional work
792
-
793
- # Set the cols and row sizes
794
- # NOTE: you must do this before you call insert_image()
795
- worksheet4.set_column('A:A', 5)
796
- worksheet4.set_column('B:B', nil, nil, 1) # Hidden
797
- worksheet4.set_column('C:D', 10)
798
- worksheet4.set_row(0, 30)
799
- worksheet4.set_row(3, 5)
800
-
801
- worksheet4.write('A10', "Image inserted over scaled rows and columns.")
802
- worksheet4.insert_image('A1', File.join(TEST_DIR,'republic.png'))
803
-
804
- workbook.close
805
-
806
- # do assertion
807
- compare_file("#{PERL_OUTDIR}/images.xls", @filename)
808
- end
809
-
810
- def test_tab_colors
811
- workbook = Spreadsheet::WriteExcel.new(@filename)
812
-
813
- worksheet1 = workbook.add_worksheet
814
- worksheet2 = workbook.add_worksheet
815
- worksheet3 = workbook.add_worksheet
816
- worksheet4 = workbook.add_worksheet
817
-
818
- # Worsheet1 will have the default tab colour.
819
- worksheet2.set_tab_color('red')
820
- worksheet3.set_tab_color('green')
821
- worksheet4.set_tab_color(0x35) # Orange
822
-
823
- workbook.close
824
-
825
- # do assertion
826
- compare_file("#{PERL_OUTDIR}/tab_colors.xls", @filename)
827
- end
828
-
829
- def test_stocks
830
- # Create a new workbook and add a worksheet
831
- workbook = Spreadsheet::WriteExcel.new(@filename)
832
- worksheet = workbook.add_worksheet
833
-
834
- # Set the column width for columns 1, 2, 3 and 4
835
- worksheet.set_column(0, 3, 15)
836
-
837
-
838
- # Create a format for the column headings
839
- header = workbook.add_format
840
- header.set_bold
841
- header.set_size(12)
842
- header.set_color('blue')
843
-
844
-
845
- # Create a format for the stock price
846
- f_price = workbook.add_format
847
- f_price.set_align('left')
848
- f_price.set_num_format('$0.00')
849
-
850
-
851
- # Create a format for the stock volume
852
- f_volume = workbook.add_format
853
- f_volume.set_align('left')
854
- f_volume.set_num_format('#,##0')
855
-
856
-
857
- # Create a format for the price change. This is an example of a conditional
858
- # format. The number is formatted as a percentage. If it is positive it is
859
- # formatted in green, if it is negative it is formatted in red and if it is
860
- # zero it is formatted as the default font colour (in this case black).
861
- # Note: the [Green] format produces an unappealing lime green. Try
862
- # [Color 10] instead for a dark green.
863
- #
864
- f_change = workbook.add_format
865
- f_change.set_align('left')
866
- f_change.set_num_format('[Green]0.0%;[Red]-0.0%;0.0%')
867
-
868
-
869
- # Write out the data
870
- worksheet.write(0, 0, 'Company', header)
871
- worksheet.write(0, 1, 'Price', header)
872
- worksheet.write(0, 2, 'Volume', header)
873
- worksheet.write(0, 3, 'Change', header)
874
-
875
- worksheet.write(1, 0, 'Damage Inc.' )
876
- worksheet.write(1, 1, 30.25, f_price) # $30.25
877
- worksheet.write(1, 2, 1234567, f_volume) # 1,234,567
878
- worksheet.write(1, 3, 0.085, f_change) # 8.5% in green
879
-
880
- worksheet.write(2, 0, 'Dump Corp.' )
881
- worksheet.write(2, 1, 1.56, f_price) # $1.56
882
- worksheet.write(2, 2, 7564, f_volume) # 7,564
883
- worksheet.write(2, 3, -0.015, f_change) # -1.5% in red
884
-
885
- worksheet.write(3, 0, 'Rev Ltd.' )
886
- worksheet.write(3, 1, 0.13, f_price) # $0.13
887
- worksheet.write(3, 2, 321, f_volume) # 321
888
- worksheet.write(3, 3, 0, f_change) # 0 in the font color (black)
889
-
890
- workbook.close
891
-
892
- # do assertion
893
- compare_file("#{PERL_OUTDIR}/stocks.xls", @filename)
894
- end
895
-
896
- def test_protection
897
- workbook = Spreadsheet::WriteExcel.new(@filename)
898
- worksheet = workbook.add_worksheet
899
-
900
- # Create some format objects
901
- locked = workbook.add_format(:locked => 1)
902
- unlocked = workbook.add_format(:locked => 0)
903
- hidden = workbook.add_format(:hidden => 1)
904
-
905
- # Format the columns
906
- worksheet.set_column('A:A', 42)
907
- worksheet.set_selection('B3:B3')
908
-
909
- # Protect the worksheet
910
- worksheet.protect
911
-
912
- # Examples of cell locking and hiding
913
- worksheet.write('A1', 'Cell B1 is locked. It cannot be edited.')
914
- worksheet.write('B1', '=1+2', locked)
915
-
916
- worksheet.write('A2', 'Cell B2 is unlocked. It can be edited.')
917
- worksheet.write('B2', '=1+2', unlocked)
918
-
919
- worksheet.write('A3', "Cell B3 is hidden. The formula isn't visible.")
920
- worksheet.write('B3', '=1+2', hidden)
921
-
922
- worksheet.write('A5', 'Use Menu->Tools->Protection->Unprotect Sheet')
923
- worksheet.write('A6', 'to remove the worksheet protection. ')
924
-
925
- workbook.close
926
-
927
- # do assertion
928
- compare_file("#{PERL_OUTDIR}/protection.xls", @filename)
929
- end
930
-
931
- def test_date_time
932
- # Create a new workbook and add a worksheet
933
- workbook = Spreadsheet::WriteExcel.new(@filename)
934
- worksheet = workbook.add_worksheet
935
- bold = workbook.add_format(:bold => 1)
936
-
937
- # Expand the first column so that the date is visible.
938
- worksheet.set_column("A:B", 30)
939
-
940
- # Write the column headers
941
- worksheet.write('A1', 'Formatted date', bold)
942
- worksheet.write('B1', 'Format', bold)
943
-
944
- # Examples date and time formats. In the output file compare how changing
945
- # the format codes change the appearance of the date.
946
- #
947
- date_formats = [
948
- 'dd/mm/yy',
949
- 'mm/dd/yy',
950
- '',
951
- 'd mm yy',
952
- 'dd mm yy',
953
- '',
954
- 'dd m yy',
955
- 'dd mm yy',
956
- 'dd mmm yy',
957
- 'dd mmmm yy',
958
- '',
959
- 'dd mm y',
960
- 'dd mm yyy',
961
- 'dd mm yyyy',
962
- '',
963
- 'd mmmm yyyy',
964
- '',
965
- 'dd/mm/yy',
966
- 'dd/mm/yy hh:mm',
967
- 'dd/mm/yy hh:mm:ss',
968
- 'dd/mm/yy hh:mm:ss.000',
969
- '',
970
- 'hh:mm',
971
- 'hh:mm:ss',
972
- 'hh:mm:ss.000',
973
- ]
974
-
975
- # Write the same date and time using each of the above formats. The empty
976
- # string formats create a blank line to make the example clearer.
977
- #
978
- row = 0
979
- date_formats.each do |date_format|
980
- row += 1
981
- next if date_format == ''
982
-
983
- # Create a format for the date or time.
984
- format = workbook.add_format(
985
- :num_format => date_format,
986
- :align => 'left'
987
- )
988
-
989
- # Write the same date using different formats.
990
- worksheet.write_date_time(row, 0, '2004-08-01T12:30:45.123', format)
991
- worksheet.write(row, 1, date_format)
992
- end
993
-
994
- # The following is an example of an invalid date. It is written as a string instead
995
- # of a number. This is also Excel's default behaviour.
996
- #
997
- row += 2
998
- worksheet.write_date_time(row, 0, '2004-13-01T12:30:45.123')
999
- worksheet.write(row, 1, 'Invalid date. Written as string.', bold)
1000
-
1001
- workbook.close
1002
-
1003
- # do assertion
1004
- compare_file("#{PERL_OUTDIR}/date_time.xls", @filename)
1005
- end
1006
- def test_diag_border
1007
- workbook = Spreadsheet::WriteExcel.new(@filename)
1008
- worksheet = workbook.add_worksheet
1009
-
1010
- format1 = workbook.add_format(:diag_type => 1)
1011
- format2 = workbook.add_format(:diag_type => 2)
1012
- format3 = workbook.add_format(:diag_type => 3)
1013
- format4 = workbook.add_format(
1014
- :diag_type => 3,
1015
- :diag_border => 7,
1016
- :diag_color => 'red'
1017
- )
1018
-
1019
- worksheet.write('B3', 'Text', format1)
1020
- worksheet.write('B6', 'Text', format2)
1021
- worksheet.write('B9', 'Text', format3)
1022
- worksheet.write('B12', 'Text', format4)
1023
-
1024
- workbook.close
1025
-
1026
- # do assertion
1027
- compare_file("#{PERL_OUTDIR}/diag_border.xls", @filename)
1028
- end
1029
-
1030
- def test_header
1031
- workbook = Spreadsheet::WriteExcel.new(@filename)
1032
- preview = "Select Print Preview to see the header and footer"
1033
-
1034
-
1035
- ######################################################################
1036
- #
1037
- # A simple example to start
1038
- #
1039
- worksheet1 = workbook.add_worksheet('Simple')
1040
-
1041
- header1 = '&CHere is some centred text.'
1042
-
1043
- footer1 = '&LHere is some left aligned text.'
1044
-
1045
-
1046
- worksheet1.set_header(header1)
1047
- worksheet1.set_footer(footer1)
1048
-
1049
- worksheet1.set_column('A:A', 50)
1050
- worksheet1.write('A1', preview)
1051
-
1052
-
1053
- ######################################################################
1054
- #
1055
- # This is an example of some of the header/footer variables.
1056
- #
1057
- worksheet2 = workbook.add_worksheet('Variables')
1058
-
1059
- header2 = '&LPage &P of &N'+
1060
- '&CFilename: &F' +
1061
- '&RSheetname: &A'
1062
-
1063
- footer2 = '&LCurrent date: &D'+
1064
- '&RCurrent time: &T'
1065
-
1066
- worksheet2.set_header(header2)
1067
- worksheet2.set_footer(footer2)
1068
-
1069
-
1070
- worksheet2.set_column('A:A', 50)
1071
- worksheet2.write('A1', preview)
1072
- worksheet2.write('A21', "Next sheet")
1073
- worksheet2.set_h_pagebreaks(20)
1074
-
1075
-
1076
- ######################################################################
1077
- #
1078
- # This example shows how to use more than one font
1079
- #
1080
- worksheet3 = workbook.add_worksheet('Mixed fonts')
1081
-
1082
- header3 = '&C' +
1083
- '&"Courier New,Bold"Hello ' +
1084
- '&"Arial,Italic"World'
1085
-
1086
- footer3 = '&C' +
1087
- '&"Symbol"e' +
1088
- '&"Arial" = mc&X2'
1089
-
1090
- worksheet3.set_header(header3)
1091
- worksheet3.set_footer(footer3)
1092
-
1093
- worksheet3.set_column('A:A', 50)
1094
- worksheet3.write('A1', preview)
1095
-
1096
-
1097
- ######################################################################
1098
- #
1099
- # Example of line wrapping
1100
- #
1101
- worksheet4 = workbook.add_worksheet('Word wrap')
1102
-
1103
- header4 = "&CHeading 1\nHeading 2\nHeading 3"
1104
-
1105
- worksheet4.set_header(header4)
1106
-
1107
- worksheet4.set_column('A:A', 50)
1108
- worksheet4.write('A1', preview)
1109
-
1110
-
1111
- ######################################################################
1112
- #
1113
- # Example of inserting a literal ampersand &
1114
- #
1115
- worksheet5 = workbook.add_worksheet('Ampersand')
1116
-
1117
- header5 = "&CCuriouser && Curiouser - Attorneys at Law"
1118
-
1119
- worksheet5.set_header(header5)
1120
-
1121
- worksheet5.set_column('A:A', 50)
1122
- worksheet5.write('A1', preview)
1123
-
1124
- workbook.close
1125
-
1126
- # do assertion
1127
- compare_file("#{PERL_OUTDIR}/headers.xls", @filename)
1128
- end
1129
-
1130
- def test_demo
1131
- workbook = Spreadsheet::WriteExcel.new(@filename)
1132
- worksheet = workbook.add_worksheet('Demo')
1133
- worksheet2 = workbook.add_worksheet('Another sheet')
1134
- worksheet3 = workbook.add_worksheet('And another')
1135
-
1136
- bold = workbook.add_format(:bold => 1)
1137
-
1138
- #######################################################################
1139
- #
1140
- # Write a general heading
1141
- #
1142
- worksheet.set_column('A:A', 36, bold)
1143
- worksheet.set_column('B:B', 20 )
1144
- worksheet.set_row(0, 40 )
1145
-
1146
- heading = workbook.add_format(
1147
- :bold => 1,
1148
- :color => 'blue',
1149
- :size => 16,
1150
- :merge => 1,
1151
- :align => 'vcenter'
1152
- )
1153
-
1154
- headings = ['Features of Spreadsheet::WriteExcel', '']
1155
- worksheet.write_row('A1', headings, heading)
1156
-
1157
-
1158
- #######################################################################
1159
- #
1160
- # Some text examples
1161
- #
1162
- text_format = workbook.add_format(
1163
- :bold => 1,
1164
- :italic => 1,
1165
- :color => 'red',
1166
- :size => 18,
1167
- :font =>'Lucida Calligraphy'
1168
- )
1169
-
1170
- # A phrase in Cyrillic
1171
- unicode = [
1172
- "042d0442043e002004440440043004370430002004"+
1173
- "3d043000200440044304410441043a043e043c0021"
1174
- ].pack('H*')
1175
-
1176
- worksheet.write('A2', "Text")
1177
- worksheet.write('B2', "Hello Excel")
1178
- worksheet.write('A3', "Formatted text")
1179
- worksheet.write('B3', "Hello Excel", text_format)
1180
- worksheet.write('A4', "Unicode text")
1181
- worksheet.write_utf16be_string('B4', unicode)
1182
-
1183
-
1184
- #######################################################################
1185
- #
1186
- # Some numeric examples
1187
- #
1188
- num1_format = workbook.add_format(:num_format => '$#,##0.00')
1189
- num2_format = workbook.add_format(:num_format => ' d mmmm yyy')
1190
-
1191
- worksheet.write('A5', "Numbers")
1192
- worksheet.write('B5', 1234.56)
1193
- worksheet.write('A6', "Formatted numbers")
1194
- worksheet.write('B6', 1234.56, num1_format)
1195
- worksheet.write('A7', "Formatted numbers")
1196
- worksheet.write('B7', 37257, num2_format)
1197
-
1198
-
1199
- #######################################################################
1200
- #
1201
- # Formulae
1202
- #
1203
- worksheet.set_selection('B8')
1204
- worksheet.write('A8', 'Formulas and functions, "=SIN(PI()/4)"')
1205
- worksheet.write('B8', '=SIN(PI()/4)')
1206
-
1207
-
1208
- #######################################################################
1209
- #
1210
- # Hyperlinks
1211
- #
1212
- worksheet.write('A9', "Hyperlinks")
1213
- worksheet.write('B9', 'http://www.perl.com/' )
1214
-
1215
-
1216
- #######################################################################
1217
- #
1218
- # Images
1219
- #
1220
- worksheet.write('A10', "Images")
1221
- worksheet.insert_image('B10', File.join(TEST_DIR,'republic.png'), 16, 8)
1222
-
1223
-
1224
- #######################################################################
1225
- #
1226
- # Misc
1227
- #
1228
- worksheet.write('A18', "Page/printer setup")
1229
- worksheet.write('A19', "Multiple worksheets")
1230
-
1231
- workbook.close
1232
-
1233
- # do assertion
1234
- compare_file("#{PERL_OUTDIR}/demo.xls", @filename)
1235
- end
1236
-
1237
- def test_unicode_cyrillic
1238
- # Create a Russian worksheet name in utf8.
1239
- sheet = [0x0421, 0x0442, 0x0440, 0x0430, 0x043D, 0x0438,
1240
- 0x0446, 0x0430].pack("U*")
1241
-
1242
- # Create a Russian string.
1243
- str = [0x0417, 0x0434, 0x0440, 0x0430, 0x0432, 0x0441,
1244
- 0x0442, 0x0432, 0x0443, 0x0439, 0x0020, 0x041C,
1245
- 0x0438, 0x0440, 0x0021].pack("U*")
1246
-
1247
- workbook = Spreadsheet::WriteExcel.new(@filename)
1248
- worksheet = workbook.add_worksheet(sheet + '1')
1249
-
1250
- worksheet.set_column('A:A', 18)
1251
- worksheet.write('A1', str)
1252
-
1253
- workbook.close
1254
-
1255
- # do assertion
1256
- compare_file("#{PERL_OUTDIR}/unicode_cyrillic.xls", @filename)
1257
- end
1258
-
1259
- def compare_file(expected, target)
1260
- fh_e = File.open(expected, "r")
1261
- fh_t = File.open(target, "r")
1262
- while true do
1263
- e1 = fh_e.read(1)
1264
- t1 = fh_t.read(1)
1265
- if e1.nil?
1266
- assert( t1.nil?, "#{expexted} is EOF but #{target} is NOT EOF.")
1267
- break
1268
- elsif t1.nil?
1269
- assert( e1.nil?, '#{target} is EOF but #{expected} is NOT EOF.')
1270
- break
1271
- end
1272
- assert_equal(e1, t1, sprintf(" #{expected} = '%s' but #{target} = '%s'", e1, t1))
1273
- break
1274
- end
1275
- fh_e.close
1276
- fh_t.close
1277
- end
1278
-
1279
-
1280
- end
1
+ $LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib"
2
+
3
+ require "test/unit"
4
+ require 'writeexcel'
5
+
6
+ class TC_example_match < Test::Unit::TestCase
7
+
8
+ TEST_DIR = File.expand_path(File.dirname(__FILE__))
9
+ PERL_OUTDIR = File.join(TEST_DIR, 'perl_output')
10
+
11
+ def setup
12
+ t = Time.now.strftime("%Y%m%d")
13
+ path = "temp#{t}-#{$$}-#{rand(0x100000000).to_s(36)}"
14
+ @test_file = File.join(Dir.tmpdir, path)
15
+ @filename = @test_file
16
+ @filename2 = @test_file + "2"
17
+ end
18
+
19
+ def teardown
20
+ File.delete(@filename) if File.exist?(@filename)
21
+ File.delete(@filename2) if File.exist?(@filename2)
22
+ end
23
+
24
+ def test_compare_file
25
+ expected = target = __FILE__
26
+ compare_file(expected, target)
27
+ end
28
+
29
+ def test_a_simple
30
+ workbook = WriteExcel.new(@filename);
31
+ worksheet = workbook.add_worksheet
32
+
33
+ # The general syntax is write(row, column, token). Note that row and
34
+ # column are zero indexed
35
+ #
36
+
37
+ # Write some text
38
+ worksheet.write(0, 0, "Hi Excel!")
39
+
40
+
41
+ # Write some numbers
42
+ worksheet.write(2, 0, 3) # Writes 3
43
+ worksheet.write(3, 0, 3.00000) # Writes 3
44
+ worksheet.write(4, 0, 3.00001) # Writes 3.00001
45
+ worksheet.write(5, 0, 3.14159) # TeX revision no.?
46
+
47
+
48
+ # Write some formulas
49
+ worksheet.write(7, 0, '=A3 + A6')
50
+ worksheet.write(8, 0, '=IF(A5>3,"Yes", "No")')
51
+
52
+
53
+ # Write a hyperlink
54
+ worksheet.write(10, 0, 'http://www.perl.com/')
55
+
56
+ # File save
57
+ workbook.close
58
+
59
+ # do assertion
60
+ compare_file("#{PERL_OUTDIR}/a_simple.xls", @filename)
61
+ end
62
+
63
+ def test_autofilter
64
+ workbook = WriteExcel.new(@filename)
65
+
66
+ worksheet1 = workbook.add_worksheet
67
+ worksheet2 = workbook.add_worksheet
68
+ worksheet3 = workbook.add_worksheet
69
+ worksheet4 = workbook.add_worksheet
70
+ worksheet5 = workbook.add_worksheet
71
+ worksheet6 = workbook.add_worksheet
72
+
73
+ bold = workbook.add_format(:bold => 1)
74
+
75
+ # Extract the data embedded at the end of this file.
76
+ headings = %w(Region Item Volume Month)
77
+ data = get_data_for_autofilter
78
+
79
+ # Set up several sheets with the same data.
80
+ workbook.sheets.each do |worksheet|
81
+ worksheet.set_column('A:D', 12)
82
+ worksheet.set_row(0, 20, bold)
83
+ worksheet.write('A1', headings)
84
+ end
85
+
86
+ ###############################################################################
87
+ #
88
+ # Example 1. Autofilter without conditions.
89
+ #
90
+
91
+ worksheet1.autofilter('A1:D51')
92
+ worksheet1.write('A2', [data])
93
+
94
+ ###############################################################################
95
+ #
96
+ #
97
+ # Example 2. Autofilter with a filter condition in the first column.
98
+ #
99
+
100
+ # The range in this example is the same as above but in row-column notation.
101
+ worksheet2.autofilter(0, 0, 50, 3)
102
+
103
+ # The placeholder "Region" in the filter is ignored and can be any string
104
+ # that adds clarity to the expression.
105
+ #
106
+ worksheet2.filter_column(0, 'Region eq East')
107
+
108
+ #
109
+ # Hide the rows that don't match the filter criteria.
110
+ #
111
+ row = 1
112
+
113
+ data.each do |row_data|
114
+ region = row_data[0]
115
+
116
+ if region == 'East'
117
+ # Row is visible.
118
+ else
119
+ # Hide row.
120
+ worksheet2.set_row(row, nil, nil, 1)
121
+ end
122
+
123
+ worksheet2.write(row, 0, row_data)
124
+ row += 1
125
+ end
126
+
127
+
128
+ ###############################################################################
129
+ #
130
+ #
131
+ # Example 3. Autofilter with a dual filter condition in one of the columns.
132
+ #
133
+
134
+ worksheet3.autofilter('A1:D51')
135
+
136
+ worksheet3.filter_column('A', 'x eq East or x eq South')
137
+
138
+ #
139
+ # Hide the rows that don't match the filter criteria.
140
+ #
141
+ row = 1
142
+
143
+ data.each do |row_data|
144
+ region = row_data[0]
145
+
146
+ if region == 'East' || region == 'South'
147
+ # Row is visible.
148
+ else
149
+ # Hide row.
150
+ worksheet3.set_row(row, nil, nil, 1)
151
+ end
152
+
153
+ worksheet3.write(row, 0, row_data)
154
+ row += 1
155
+ end
156
+
157
+
158
+ ###############################################################################
159
+ #
160
+ #
161
+ # Example 4. Autofilter with filter conditions in two columns.
162
+ #
163
+
164
+ worksheet4.autofilter('A1:D51')
165
+
166
+ worksheet4.filter_column('A', 'x eq East')
167
+ worksheet4.filter_column('C', 'x > 3000 and x < 8000' )
168
+
169
+ #
170
+ # Hide the rows that don't match the filter criteria.
171
+ #
172
+ row = 1
173
+
174
+ data.each do |row_data|
175
+ region = row_data[0]
176
+ volume = row_data[2]
177
+
178
+ if region == 'East' && volume > 3000 && volume < 8000
179
+ # Row is visible.
180
+ else
181
+ # Hide row.
182
+ worksheet4.set_row(row, nil, nil, 1)
183
+ end
184
+
185
+ worksheet4.write(row, 0, row_data)
186
+ row += 1
187
+ end
188
+
189
+
190
+ ###############################################################################
191
+ #
192
+ #
193
+ # Example 5. Autofilter with filter for blanks.
194
+ #
195
+
196
+ # Create a blank cell in our test data.
197
+ data[5][0] = ''
198
+
199
+ worksheet5.autofilter('A1:D51')
200
+ worksheet5.filter_column('A', 'x == Blanks')
201
+
202
+ #
203
+ # Hide the rows that don't match the filter criteria.
204
+ #
205
+ row = 1
206
+
207
+ data.each do |row_data|
208
+ region = row_data[0]
209
+
210
+ if region == ''
211
+ # Row is visible.
212
+ else
213
+ # Hide row.
214
+ worksheet5.set_row(row, nil, nil, 1)
215
+ end
216
+
217
+ worksheet5.write(row, 0, row_data)
218
+ row += 1
219
+ end
220
+
221
+
222
+ ###############################################################################
223
+ #
224
+ #
225
+ # Example 6. Autofilter with filter for non-blanks.
226
+ #
227
+
228
+ worksheet6.autofilter('A1:D51')
229
+ worksheet6.filter_column('A', 'x == NonBlanks')
230
+
231
+ #
232
+ # Hide the rows that don't match the filter criteria.
233
+ #
234
+ row = 1
235
+
236
+ data.each do |row_data|
237
+ region = row_data[0]
238
+
239
+ if region != ''
240
+ # Row is visible.
241
+ else
242
+ # Hide row.
243
+ worksheet6.set_row(row, nil, nil, 1)
244
+ end
245
+
246
+ worksheet6.write(row, 0, row_data)
247
+ row += 1
248
+ end
249
+
250
+ workbook.close
251
+
252
+ # do assertion
253
+ compare_file("#{PERL_OUTDIR}/autofilter.xls", @filename)
254
+ end
255
+
256
+ def get_data_for_autofilter
257
+ [
258
+ ['East', 'Apple', 9000, 'July'],
259
+ ['East', 'Apple', 5000, 'July'],
260
+ ['South', 'Orange', 9000, 'September'],
261
+ ['North', 'Apple', 2000, 'November'],
262
+ ['West', 'Apple', 9000, 'November'],
263
+ ['South', 'Pear', 7000, 'October'],
264
+ ['North', 'Pear', 9000, 'August'],
265
+ ['West', 'Orange', 1000, 'December'],
266
+ ['West', 'Grape', 1000, 'November'],
267
+ ['South', 'Pear', 10000, 'April'],
268
+ ['West', 'Grape', 6000, 'January'],
269
+ ['South', 'Orange', 3000, 'May'],
270
+ ['North', 'Apple', 3000, 'December'],
271
+ ['South', 'Apple', 7000, 'February'],
272
+ ['West', 'Grape', 1000, 'December'],
273
+ ['East', 'Grape', 8000, 'February'],
274
+ ['South', 'Grape', 10000, 'June'],
275
+ ['West', 'Pear', 7000, 'December'],
276
+ ['South', 'Apple', 2000, 'October'],
277
+ ['East', 'Grape', 7000, 'December'],
278
+ ['North', 'Grape', 6000, 'April'],
279
+ ['East', 'Pear', 8000, 'February'],
280
+ ['North', 'Apple', 7000, 'August'],
281
+ ['North', 'Orange', 7000, 'July'],
282
+ ['North', 'Apple', 6000, 'June'],
283
+ ['South', 'Grape', 8000, 'September'],
284
+ ['West', 'Apple', 3000, 'October'],
285
+ ['South', 'Orange', 10000, 'November'],
286
+ ['West', 'Grape', 4000, 'July'],
287
+ ['North', 'Orange', 5000, 'August'],
288
+ ['East', 'Orange', 1000, 'November'],
289
+ ['East', 'Orange', 4000, 'October'],
290
+ ['North', 'Grape', 5000, 'August'],
291
+ ['East', 'Apple', 1000, 'December'],
292
+ ['South', 'Apple', 10000, 'March'],
293
+ ['East', 'Grape', 7000, 'October'],
294
+ ['West', 'Grape', 1000, 'September'],
295
+ ['East', 'Grape', 10000, 'October'],
296
+ ['South', 'Orange', 8000, 'March'],
297
+ ['North', 'Apple', 4000, 'July'],
298
+ ['South', 'Orange', 5000, 'July'],
299
+ ['West', 'Apple', 4000, 'June'],
300
+ ['East', 'Apple', 5000, 'April'],
301
+ ['North', 'Pear', 3000, 'August'],
302
+ ['East', 'Grape', 9000, 'November'],
303
+ ['North', 'Orange', 8000, 'October'],
304
+ ['East', 'Apple', 10000, 'June'],
305
+ ['South', 'Pear', 1000, 'December'],
306
+ ['North', 'Grape', 10000, 'July'],
307
+ ['East', 'Grape', 6000, 'February'],
308
+ ]
309
+ end
310
+
311
+ def test_regions
312
+ workbook = WriteExcel.new(@filename)
313
+
314
+ # Add some worksheets
315
+ north = workbook.add_worksheet("North")
316
+ south = workbook.add_worksheet("South")
317
+ east = workbook.add_worksheet("East")
318
+ west = workbook.add_worksheet("West")
319
+
320
+ # Add a Format
321
+ format = workbook.add_format()
322
+ format.set_bold()
323
+ format.set_color('blue')
324
+
325
+ # Add a caption to each worksheet
326
+ workbook.sheets.each do |worksheet|
327
+ worksheet.write(0, 0, "Sales", format)
328
+ end
329
+
330
+ # Write some data
331
+ north.write(0, 1, 200000)
332
+ south.write(0, 1, 100000)
333
+ east.write(0, 1, 150000)
334
+ west.write(0, 1, 100000)
335
+
336
+ # Set the active worksheet
337
+ bp=1
338
+ south.activate()
339
+
340
+ # Set the width of the first column
341
+ south.set_column(0, 0, 20)
342
+
343
+ # Set the active cell
344
+ south.set_selection(0, 1)
345
+
346
+ workbook.close
347
+
348
+ # do assertion
349
+ compare_file("#{PERL_OUTDIR}/regions.xls", @filename)
350
+ end
351
+
352
+ def test_stats
353
+ workbook = WriteExcel.new(@filename)
354
+ worksheet = workbook.add_worksheet('Test data')
355
+
356
+ # Set the column width for columns 1
357
+ worksheet.set_column(0, 0, 20)
358
+
359
+ # Create a format for the headings
360
+ format = workbook.add_format
361
+ format.set_bold
362
+
363
+ # Write the sample data
364
+ worksheet.write(0, 0, 'Sample', format)
365
+ worksheet.write(0, 1, 1)
366
+ worksheet.write(0, 2, 2)
367
+ worksheet.write(0, 3, 3)
368
+ worksheet.write(0, 4, 4)
369
+ worksheet.write(0, 5, 5)
370
+ worksheet.write(0, 6, 6)
371
+ worksheet.write(0, 7, 7)
372
+ worksheet.write(0, 8, 8)
373
+
374
+ worksheet.write(1, 0, 'Length', format)
375
+ worksheet.write(1, 1, 25.4)
376
+ worksheet.write(1, 2, 25.4)
377
+ worksheet.write(1, 3, 24.8)
378
+ worksheet.write(1, 4, 25.0)
379
+ worksheet.write(1, 5, 25.3)
380
+ worksheet.write(1, 6, 24.9)
381
+ worksheet.write(1, 7, 25.2)
382
+ worksheet.write(1, 8, 24.8)
383
+
384
+ # Write some statistical functions
385
+ worksheet.write(4, 0, 'Count', format)
386
+ worksheet.write(4, 1, '=COUNT(B1:I1)')
387
+
388
+ worksheet.write(5, 0, 'Sum', format)
389
+ worksheet.write(5, 1, '=SUM(B2:I2)')
390
+
391
+ worksheet.write(6, 0, 'Average', format)
392
+ worksheet.write(6, 1, '=AVERAGE(B2:I2)')
393
+
394
+ worksheet.write(7, 0, 'Min', format)
395
+ worksheet.write(7, 1, '=MIN(B2:I2)')
396
+
397
+ worksheet.write(8, 0, 'Max', format)
398
+ worksheet.write(8, 1, '=MAX(B2:I2)')
399
+
400
+ worksheet.write(9, 0, 'Standard Deviation', format)
401
+ worksheet.write(9, 1, '=STDEV(B2:I2)')
402
+
403
+ worksheet.write(10, 0, 'Kurtosis', format)
404
+ worksheet.write(10, 1, '=KURT(B2:I2)')
405
+
406
+ workbook.close
407
+
408
+ # do assertion
409
+ compare_file("#{PERL_OUTDIR}/stats.xls", @filename)
410
+ end
411
+
412
+ def test_hyperlink1
413
+ # Create a new workbook and add a worksheet
414
+ workbook = WriteExcel.new(@filename)
415
+ worksheet = workbook.add_worksheet('Hyperlinks')
416
+
417
+ # Format the first column
418
+ worksheet.set_column('A:A', 30)
419
+ worksheet.set_selection('B1')
420
+
421
+
422
+ # Add a sample format
423
+ format = workbook.add_format
424
+ format.set_size(12)
425
+ format.set_bold
426
+ format.set_color('red')
427
+ format.set_underline
428
+
429
+
430
+ # Write some hyperlinks
431
+ worksheet.write('A1', 'http://www.perl.com/' )
432
+ worksheet.write('A3', 'http://www.perl.com/', 'Perl home' )
433
+ worksheet.write('A5', 'http://www.perl.com/', nil, format)
434
+ worksheet.write('A7', 'mailto:jmcnamara@cpan.org', 'Mail me')
435
+
436
+ # Write a URL that isn't a hyperlink
437
+ worksheet.write_string('A9', 'http://www.perl.com/')
438
+
439
+ workbook.close
440
+
441
+ # do assertion
442
+ compare_file("#{PERL_OUTDIR}/hyperlink.xls", @filename)
443
+ end
444
+
445
+ def test_copyformat
446
+ # Create workbook1
447
+ workbook1 = WriteExcel.new(@filename)
448
+ worksheet1 = workbook1.add_worksheet
449
+ format1a = workbook1.add_format
450
+ format1b = workbook1.add_format
451
+
452
+ # Create workbook2
453
+ workbook2 = WriteExcel.new(@filename2)
454
+ worksheet2 = workbook2.add_worksheet
455
+ format2a = workbook2.add_format
456
+ format2b = workbook2.add_format
457
+
458
+ # Create a global format object that isn't tied to a workbook
459
+ global_format = Format.new
460
+
461
+ # Set the formatting
462
+ global_format.set_color('blue')
463
+ global_format.set_bold
464
+ global_format.set_italic
465
+
466
+ # Create another example format
467
+ format1b.set_color('red')
468
+
469
+ # Copy the global format properties to the worksheet formats
470
+ format1a.copy(global_format)
471
+ format2a.copy(global_format)
472
+
473
+ # Copy a format from worksheet1 to worksheet2
474
+ format2b.copy(format1b)
475
+
476
+ # Write some output
477
+ worksheet1.write(0, 0, "Ciao", format1a)
478
+ worksheet1.write(1, 0, "Ciao", format1b)
479
+
480
+ worksheet2.write(0, 0, "Hello", format2a)
481
+ worksheet2.write(1, 0, "Hello", format2b)
482
+ workbook1.close
483
+ workbook2.close
484
+
485
+ # do assertion
486
+ compare_file("#{PERL_OUTDIR}/workbook1.xls", @filename)
487
+ compare_file("#{PERL_OUTDIR}/workbook2.xls", @filename2)
488
+ end
489
+
490
+ def test_data_validate
491
+ workbook = WriteExcel.new(@filename)
492
+ worksheet = workbook.add_worksheet
493
+
494
+ # Add a format for the header cells.
495
+ header_format = workbook.add_format(
496
+ :border => 1,
497
+ :bg_color => 43,
498
+ :bold => 1,
499
+ :text_wrap => 1,
500
+ :valign => 'vcenter',
501
+ :indent => 1
502
+ )
503
+
504
+ # Set up layout of the worksheet.
505
+ worksheet.set_column('A:A', 64)
506
+ worksheet.set_column('B:B', 15)
507
+ worksheet.set_column('D:D', 15)
508
+ worksheet.set_row(0, 36)
509
+ worksheet.set_selection('B3')
510
+
511
+
512
+ # Write the header cells and some data that will be used in the examples.
513
+ row = 0
514
+ heading1 = 'Some examples of data validation in WriteExcel'
515
+ heading2 = 'Enter values in this column'
516
+ heading3 = 'Sample Data'
517
+
518
+ worksheet.write('A1', heading1, header_format)
519
+ worksheet.write('B1', heading2, header_format)
520
+ worksheet.write('D1', heading3, header_format)
521
+
522
+ worksheet.write('D3', ['Integers', 1, 10])
523
+ worksheet.write('D4', ['List data', 'open', 'high', 'close'])
524
+ worksheet.write('D5', ['Formula', '=AND(F5=50,G5=60)', 50, 60])
525
+
526
+
527
+ #
528
+ # Example 1. Limiting input to an integer in a fixed range.
529
+ #
530
+ txt = 'Enter an integer between 1 and 10'
531
+ row += 2
532
+
533
+ worksheet.write(row, 0, txt)
534
+ worksheet.data_validation(row, 1,
535
+ {
536
+ :validate => 'integer',
537
+ :criteria => 'between',
538
+ :minimum => 1,
539
+ :maximum => 10
540
+ })
541
+
542
+
543
+ #
544
+ # Example 2. Limiting input to an integer outside a fixed range.
545
+ #
546
+ txt = 'Enter an integer that is not between 1 and 10 (using cell references)'
547
+ row += 2
548
+
549
+ worksheet.write(row, 0, txt)
550
+ worksheet.data_validation(row, 1,
551
+ {
552
+ :validate => 'integer',
553
+ :criteria => 'not between',
554
+ :minimum => '=E3',
555
+ :maximum => '=F3'
556
+ })
557
+
558
+
559
+ #
560
+ # Example 3. Limiting input to an integer greater than a fixed value.
561
+ #
562
+ txt = 'Enter an integer greater than 0'
563
+ row += 2
564
+
565
+ worksheet.write(row, 0, txt)
566
+ worksheet.data_validation(row, 1,
567
+ {
568
+ :validate => 'integer',
569
+ :criteria => '>',
570
+ :value => 0
571
+ })
572
+
573
+
574
+ #
575
+ # Example 4. Limiting input to an integer less than a fixed value.
576
+ #
577
+ txt = 'Enter an integer less than 10'
578
+ row += 2
579
+
580
+ worksheet.write(row, 0, txt)
581
+ worksheet.data_validation(row, 1,
582
+ {
583
+ :validate => 'integer',
584
+ :criteria => '<',
585
+ :value => 10
586
+ })
587
+
588
+
589
+ #
590
+ # Example 5. Limiting input to a decimal in a fixed range.
591
+ #
592
+ txt = 'Enter a decimal between 0.1 and 0.5'
593
+ row += 2
594
+
595
+ worksheet.write(row, 0, txt)
596
+ worksheet.data_validation(row, 1,
597
+ {
598
+ :validate => 'decimal',
599
+ :criteria => 'between',
600
+ :minimum => 0.1,
601
+ :maximum => 0.5
602
+ })
603
+
604
+
605
+ #
606
+ # Example 6. Limiting input to a value in a dropdown list.
607
+ #
608
+ txt = 'Select a value from a drop down list'
609
+ row += 2
610
+ bp=1
611
+ worksheet.write(row, 0, txt)
612
+ worksheet.data_validation(row, 1,
613
+ {
614
+ :validate => 'list',
615
+ :source => ['open', 'high', 'close']
616
+ })
617
+
618
+
619
+ #
620
+ # Example 6. Limiting input to a value in a dropdown list.
621
+ #
622
+ txt = 'Select a value from a drop down list (using a cell range)'
623
+ row += 2
624
+
625
+ worksheet.write(row, 0, txt)
626
+ worksheet.data_validation(row, 1,
627
+ {
628
+ :validate => 'list',
629
+ :source => '=E4:G4'
630
+ })
631
+
632
+
633
+ #
634
+ # Example 7. Limiting input to a date in a fixed range.
635
+ #
636
+ txt = 'Enter a date between 1/1/2008 and 12/12/2008'
637
+ row += 2
638
+
639
+ worksheet.write(row, 0, txt)
640
+ worksheet.data_validation(row, 1,
641
+ {
642
+ :validate => 'date',
643
+ :criteria => 'between',
644
+ :minimum => '2008-01-01T',
645
+ :maximum => '2008-12-12T'
646
+ })
647
+
648
+
649
+ #
650
+ # Example 8. Limiting input to a time in a fixed range.
651
+ #
652
+ txt = 'Enter a time between 6:00 and 12:00'
653
+ row += 2
654
+
655
+ worksheet.write(row, 0, txt)
656
+ worksheet.data_validation(row, 1,
657
+ {
658
+ :validate => 'time',
659
+ :criteria => 'between',
660
+ :minimum => 'T06:00',
661
+ :maximum => 'T12:00'
662
+ })
663
+
664
+
665
+ #
666
+ # Example 9. Limiting input to a string greater than a fixed length.
667
+ #
668
+ txt = 'Enter a string longer than 3 characters'
669
+ row += 2
670
+
671
+ worksheet.write(row, 0, txt)
672
+ worksheet.data_validation(row, 1,
673
+ {
674
+ :validate => 'length',
675
+ :criteria => '>',
676
+ :value => 3
677
+ })
678
+
679
+
680
+ #
681
+ # Example 10. Limiting input based on a formula.
682
+ #
683
+ txt = 'Enter a value if the following is true "=AND(F5=50,G5=60)"'
684
+ row += 2
685
+
686
+ worksheet.write(row, 0, txt)
687
+ worksheet.data_validation(row, 1,
688
+ {
689
+ :validate => 'custom',
690
+ :value => '=AND(F5=50,G5=60)'
691
+ })
692
+
693
+
694
+ #
695
+ # Example 11. Displaying and modify data validation messages.
696
+ #
697
+ txt = 'Displays a message when you select the cell'
698
+ row += 2
699
+
700
+ worksheet.write(row, 0, txt)
701
+ worksheet.data_validation(row, 1,
702
+ {
703
+ :validate => 'integer',
704
+ :criteria => 'between',
705
+ :minimum => 1,
706
+ :maximum => 100,
707
+ :input_title => 'Enter an integer:',
708
+ :input_message => 'between 1 and 100'
709
+ })
710
+
711
+
712
+ #
713
+ # Example 12. Displaying and modify data validation messages.
714
+ #
715
+ txt = 'Display a custom error message when integer isn\'t between 1 and 100'
716
+ row += 2
717
+
718
+ worksheet.write(row, 0, txt)
719
+ worksheet.data_validation(row, 1,
720
+ {
721
+ :validate => 'integer',
722
+ :criteria => 'between',
723
+ :minimum => 1,
724
+ :maximum => 100,
725
+ :input_title => 'Enter an integer:',
726
+ :input_message => 'between 1 and 100',
727
+ :error_title => 'Input value is not valid!',
728
+ :error_message => 'It should be an integer between 1 and 100'
729
+ })
730
+
731
+
732
+ #
733
+ # Example 13. Displaying and modify data validation messages.
734
+ #
735
+ txt = 'Display a custom information message when integer isn\'t between 1 and 100'
736
+ row += 2
737
+
738
+ worksheet.write(row, 0, txt)
739
+ worksheet.data_validation(row, 1,
740
+ {
741
+ :validate => 'integer',
742
+ :criteria => 'between',
743
+ :minimum => 1,
744
+ :maximum => 100,
745
+ :input_title => 'Enter an integer:',
746
+ :input_message => 'between 1 and 100',
747
+ :error_title => 'Input value is not valid!',
748
+ :error_message => 'It should be an integer between 1 and 100',
749
+ :error_type => 'information'
750
+ })
751
+
752
+ workbook.close
753
+
754
+ # do assertion
755
+ compare_file("#{PERL_OUTDIR}/data_validate.xls", @filename)
756
+ end
757
+
758
+ def test_merge1
759
+ workbook = WriteExcel.new(@filename)
760
+ worksheet = workbook.add_worksheet
761
+
762
+ # Increase the cell size of the merged cells to highlight the formatting.
763
+ worksheet.set_column('B:D', 20)
764
+ worksheet.set_row(2, 30)
765
+
766
+ # Create a merge format
767
+ format = workbook.add_format(:center_across => 1)
768
+
769
+ # Only one cell should contain text, the others should be blank.
770
+ worksheet.write(2, 1, "Center across selection", format)
771
+ worksheet.write_blank(2, 2, format)
772
+ worksheet.write_blank(2, 3, format)
773
+
774
+ workbook.close
775
+
776
+ # do assertion
777
+ compare_file("#{PERL_OUTDIR}/merge1.xls", @filename)
778
+ end
779
+
780
+ def test_merge2
781
+ workbook = WriteExcel.new(@filename)
782
+ worksheet = workbook.add_worksheet
783
+
784
+ # Increase the cell size of the merged cells to highlight the formatting.
785
+ worksheet.set_column(1, 2, 30)
786
+ worksheet.set_row(2, 40)
787
+
788
+ # Create a merged format
789
+ format = workbook.add_format(
790
+ :center_across => 1,
791
+ :bold => 1,
792
+ :size => 15,
793
+ :pattern => 1,
794
+ :border => 6,
795
+ :color => 'white',
796
+ :fg_color => 'green',
797
+ :border_color => 'yellow',
798
+ :align => 'vcenter'
799
+ )
800
+
801
+ # Only one cell should contain text, the others should be blank.
802
+ worksheet.write(2, 1, "Center across selection", format)
803
+ worksheet.write_blank(2, 2, format)
804
+ workbook.close
805
+
806
+ # do assertion
807
+ compare_file("#{PERL_OUTDIR}/merge2.xls", @filename)
808
+ end
809
+
810
+ def test_merge3
811
+ workbook = WriteExcel.new(@filename)
812
+ worksheet = workbook.add_worksheet()
813
+
814
+ # Increase the cell size of the merged cells to highlight the formatting.
815
+ [1, 3,6,7].each { |row| worksheet.set_row(row, 30) }
816
+ worksheet.set_column('B:D', 20)
817
+
818
+ ###############################################################################
819
+ #
820
+ # Example 1: Merge cells containing a hyperlink using write_url_range()
821
+ # and the standard Excel 5+ merge property.
822
+ #
823
+ format1 = workbook.add_format(
824
+ :center_across => 1,
825
+ :border => 1,
826
+ :underline => 1,
827
+ :color => 'blue'
828
+ )
829
+
830
+ # Write the cells to be merged
831
+ worksheet.write_url_range('B2:D2', 'http://www.perl.com', format1)
832
+ worksheet.write_blank('C2', format1)
833
+ worksheet.write_blank('D2', format1)
834
+
835
+
836
+
837
+ ###############################################################################
838
+ #
839
+ # Example 2: Merge cells containing a hyperlink using merge_range().
840
+ #
841
+ format2 = workbook.add_format(
842
+ :border => 1,
843
+ :underline => 1,
844
+ :color => 'blue',
845
+ :align => 'center',
846
+ :valign => 'vcenter'
847
+ )
848
+
849
+ # Merge 3 cells
850
+ worksheet.merge_range('B4:D4', 'http://www.perl.com', format2)
851
+
852
+
853
+ # Merge 3 cells over two rows
854
+ worksheet.merge_range('B7:D8', 'http://www.perl.com', format2)
855
+
856
+ workbook.close
857
+
858
+ # do assertion
859
+ compare_file("#{PERL_OUTDIR}/merge3.xls", @filename)
860
+ end
861
+
862
+ def test_merge4
863
+ # Create a new workbook and add a worksheet
864
+ workbook = WriteExcel.new(@filename)
865
+ worksheet = workbook.add_worksheet
866
+
867
+ # Increase the cell size of the merged cells to highlight the formatting.
868
+ (1..11).each { |row| worksheet.set_row(row, 30) }
869
+ worksheet.set_column('B:D', 20)
870
+
871
+ ###############################################################################
872
+ #
873
+ # Example 1: Text centered vertically and horizontally
874
+ #
875
+ format1 = workbook.add_format(
876
+ :border => 6,
877
+ :bold => 1,
878
+ :color => 'red',
879
+ :valign => 'vcenter',
880
+ :align => 'center'
881
+ )
882
+
883
+ worksheet.merge_range('B2:D3', 'Vertical and horizontal', format1)
884
+
885
+
886
+ ###############################################################################
887
+ #
888
+ # Example 2: Text aligned to the top and left
889
+ #
890
+ format2 = workbook.add_format(
891
+ :border => 6,
892
+ :bold => 1,
893
+ :color => 'red',
894
+ :valign => 'top',
895
+ :align => 'left'
896
+ )
897
+
898
+ worksheet.merge_range('B5:D6', 'Aligned to the top and left', format2)
899
+
900
+ ###############################################################################
901
+ #
902
+ # Example 3: Text aligned to the bottom and right
903
+ #
904
+ format3 = workbook.add_format(
905
+ :border => 6,
906
+ :bold => 1,
907
+ :color => 'red',
908
+ :valign => 'bottom',
909
+ :align => 'right'
910
+ )
911
+
912
+ worksheet.merge_range('B8:D9', 'Aligned to the bottom and right', format3)
913
+
914
+ ###############################################################################
915
+ #
916
+ # Example 4: Text justified (i.e. wrapped) in the cell
917
+ #
918
+ format4 = workbook.add_format(
919
+ :border => 6,
920
+ :bold => 1,
921
+ :color => 'red',
922
+ :valign => 'top',
923
+ :align => 'justify'
924
+ )
925
+
926
+ worksheet.merge_range('B11:D12', 'Justified: '+'so on and '*18, format4)
927
+
928
+ workbook.close
929
+
930
+ # do assertion
931
+ compare_file("#{PERL_OUTDIR}/merge4.xls", @filename)
932
+ end
933
+
934
+ def test_merge5
935
+ # Create a new workbook and add a worksheet
936
+ workbook = WriteExcel.new(@filename)
937
+ worksheet = workbook.add_worksheet
938
+
939
+
940
+ # Increase the cell size of the merged cells to highlight the formatting.
941
+ (3..8).each { |col| worksheet.set_row(col, 36) }
942
+ [1, 3, 5].each { |n| worksheet.set_column(n, n, 15) }
943
+
944
+
945
+ ###############################################################################
946
+ #
947
+ # Rotation 1, letters run from top to bottom
948
+ #
949
+ format1 = workbook.add_format(
950
+ :border => 6,
951
+ :bold => 1,
952
+ :color => 'red',
953
+ :valign => 'vcentre',
954
+ :align => 'centre',
955
+ :rotation => 270
956
+ )
957
+
958
+
959
+ worksheet.merge_range('B4:B9', 'Rotation 270', format1)
960
+
961
+
962
+ ###############################################################################
963
+ #
964
+ # Rotation 2, 90° anticlockwise
965
+ #
966
+ format2 = workbook.add_format(
967
+ :border => 6,
968
+ :bold => 1,
969
+ :color => 'red',
970
+ :valign => 'vcentre',
971
+ :align => 'centre',
972
+ :rotation => 90
973
+ )
974
+
975
+
976
+ worksheet.merge_range('D4:D9', 'Rotation 90', format2)
977
+
978
+
979
+
980
+ ###############################################################################
981
+ #
982
+ # Rotation 3, 90° clockwise
983
+ #
984
+ format3 = workbook.add_format(
985
+ :border => 6,
986
+ :bold => 1,
987
+ :color => 'red',
988
+ :valign => 'vcentre',
989
+ :align => 'centre',
990
+ :rotation => -90
991
+ )
992
+
993
+
994
+ worksheet.merge_range('F4:F9', 'Rotation -90', format3)
995
+
996
+ workbook.close
997
+
998
+ # do assertion
999
+ compare_file("#{PERL_OUTDIR}/merge5.xls", @filename)
1000
+ end
1001
+
1002
+ def test_images
1003
+ # Create a new workbook called simple.xls and add a worksheet
1004
+ workbook = WriteExcel.new(@filename)
1005
+ worksheet1 = workbook.add_worksheet('Image 1')
1006
+ worksheet2 = workbook.add_worksheet('Image 2')
1007
+ worksheet3 = workbook.add_worksheet('Image 3')
1008
+ worksheet4 = workbook.add_worksheet('Image 4')
1009
+ bp=1
1010
+
1011
+ # Insert a basic image
1012
+ worksheet1.write('A10', "Image inserted into worksheet.")
1013
+ worksheet1.insert_image('A1', File.join(TEST_DIR,'republic.png'))
1014
+
1015
+
1016
+ # Insert an image with an offset
1017
+ worksheet2.write('A10', "Image inserted with an offset.")
1018
+ worksheet2.insert_image('A1', File.join(TEST_DIR,'republic.png'), 32, 10)
1019
+
1020
+ # Insert a scaled image
1021
+ worksheet3.write('A10', "Image scaled: width x 2, height x 0.8.")
1022
+ worksheet3.insert_image('A1', File.join(TEST_DIR,'republic.png'), 0, 0, 2, 0.8)
1023
+
1024
+ # Insert an image over varied column and row sizes
1025
+ # This does not require any additional work
1026
+
1027
+ # Set the cols and row sizes
1028
+ # NOTE: you must do this before you call insert_image()
1029
+ worksheet4.set_column('A:A', 5)
1030
+ worksheet4.set_column('B:B', nil, nil, 1) # Hidden
1031
+ worksheet4.set_column('C:D', 10)
1032
+ worksheet4.set_row(0, 30)
1033
+ worksheet4.set_row(3, 5)
1034
+
1035
+ worksheet4.write('A10', "Image inserted over scaled rows and columns.")
1036
+ worksheet4.insert_image('A1', File.join(TEST_DIR,'republic.png'))
1037
+
1038
+ workbook.close
1039
+
1040
+ # do assertion
1041
+ compare_file("#{PERL_OUTDIR}/images.xls", @filename)
1042
+ end
1043
+
1044
+ def test_tab_colors
1045
+ workbook = WriteExcel.new(@filename)
1046
+
1047
+ worksheet1 = workbook.add_worksheet
1048
+ worksheet2 = workbook.add_worksheet
1049
+ worksheet3 = workbook.add_worksheet
1050
+ worksheet4 = workbook.add_worksheet
1051
+
1052
+ # Worsheet1 will have the default tab colour.
1053
+ worksheet2.set_tab_color('red')
1054
+ worksheet3.set_tab_color('green')
1055
+ worksheet4.set_tab_color(0x35) # Orange
1056
+
1057
+ workbook.close
1058
+
1059
+ # do assertion
1060
+ compare_file("#{PERL_OUTDIR}/tab_colors.xls", @filename)
1061
+ end
1062
+
1063
+ def test_stocks
1064
+ # Create a new workbook and add a worksheet
1065
+ workbook = WriteExcel.new(@filename)
1066
+ worksheet = workbook.add_worksheet
1067
+
1068
+ # Set the column width for columns 1, 2, 3 and 4
1069
+ worksheet.set_column(0, 3, 15)
1070
+
1071
+
1072
+ # Create a format for the column headings
1073
+ header = workbook.add_format
1074
+ header.set_bold
1075
+ header.set_size(12)
1076
+ header.set_color('blue')
1077
+
1078
+
1079
+ # Create a format for the stock price
1080
+ f_price = workbook.add_format
1081
+ f_price.set_align('left')
1082
+ f_price.set_num_format('$0.00')
1083
+
1084
+
1085
+ # Create a format for the stock volume
1086
+ f_volume = workbook.add_format
1087
+ f_volume.set_align('left')
1088
+ f_volume.set_num_format('#,##0')
1089
+
1090
+
1091
+ # Create a format for the price change. This is an example of a conditional
1092
+ # format. The number is formatted as a percentage. If it is positive it is
1093
+ # formatted in green, if it is negative it is formatted in red and if it is
1094
+ # zero it is formatted as the default font colour (in this case black).
1095
+ # Note: the [Green] format produces an unappealing lime green. Try
1096
+ # [Color 10] instead for a dark green.
1097
+ #
1098
+ f_change = workbook.add_format
1099
+ f_change.set_align('left')
1100
+ f_change.set_num_format('[Green]0.0%;[Red]-0.0%;0.0%')
1101
+
1102
+
1103
+ # Write out the data
1104
+ worksheet.write(0, 0, 'Company', header)
1105
+ worksheet.write(0, 1, 'Price', header)
1106
+ worksheet.write(0, 2, 'Volume', header)
1107
+ worksheet.write(0, 3, 'Change', header)
1108
+
1109
+ worksheet.write(1, 0, 'Damage Inc.' )
1110
+ worksheet.write(1, 1, 30.25, f_price) # $30.25
1111
+ worksheet.write(1, 2, 1234567, f_volume) # 1,234,567
1112
+ worksheet.write(1, 3, 0.085, f_change) # 8.5% in green
1113
+
1114
+ worksheet.write(2, 0, 'Dump Corp.' )
1115
+ worksheet.write(2, 1, 1.56, f_price) # $1.56
1116
+ worksheet.write(2, 2, 7564, f_volume) # 7,564
1117
+ worksheet.write(2, 3, -0.015, f_change) # -1.5% in red
1118
+
1119
+ worksheet.write(3, 0, 'Rev Ltd.' )
1120
+ worksheet.write(3, 1, 0.13, f_price) # $0.13
1121
+ worksheet.write(3, 2, 321, f_volume) # 321
1122
+ worksheet.write(3, 3, 0, f_change) # 0 in the font color (black)
1123
+
1124
+ workbook.close
1125
+
1126
+ # do assertion
1127
+ compare_file("#{PERL_OUTDIR}/stocks.xls", @filename)
1128
+ end
1129
+
1130
+ def test_protection
1131
+ workbook = WriteExcel.new(@filename)
1132
+ worksheet = workbook.add_worksheet
1133
+
1134
+ # Create some format objects
1135
+ locked = workbook.add_format(:locked => 1)
1136
+ unlocked = workbook.add_format(:locked => 0)
1137
+ hidden = workbook.add_format(:hidden => 1)
1138
+
1139
+ # Format the columns
1140
+ worksheet.set_column('A:A', 42)
1141
+ worksheet.set_selection('B3:B3')
1142
+
1143
+ # Protect the worksheet
1144
+ worksheet.protect
1145
+
1146
+ # Examples of cell locking and hiding
1147
+ worksheet.write('A1', 'Cell B1 is locked. It cannot be edited.')
1148
+ worksheet.write('B1', '=1+2', locked)
1149
+
1150
+ worksheet.write('A2', 'Cell B2 is unlocked. It can be edited.')
1151
+ worksheet.write('B2', '=1+2', unlocked)
1152
+
1153
+ worksheet.write('A3', "Cell B3 is hidden. The formula isn't visible.")
1154
+ worksheet.write('B3', '=1+2', hidden)
1155
+
1156
+ worksheet.write('A5', 'Use Menu->Tools->Protection->Unprotect Sheet')
1157
+ worksheet.write('A6', 'to remove the worksheet protection. ')
1158
+
1159
+ workbook.close
1160
+
1161
+ # do assertion
1162
+ compare_file("#{PERL_OUTDIR}/protection.xls", @filename)
1163
+ end
1164
+
1165
+ def test_date_time
1166
+ # Create a new workbook and add a worksheet
1167
+ workbook = WriteExcel.new(@filename)
1168
+ worksheet = workbook.add_worksheet
1169
+ bold = workbook.add_format(:bold => 1)
1170
+
1171
+ # Expand the first column so that the date is visible.
1172
+ worksheet.set_column("A:B", 30)
1173
+
1174
+ # Write the column headers
1175
+ worksheet.write('A1', 'Formatted date', bold)
1176
+ worksheet.write('B1', 'Format', bold)
1177
+
1178
+ # Examples date and time formats. In the output file compare how changing
1179
+ # the format codes change the appearance of the date.
1180
+ #
1181
+ date_formats = [
1182
+ 'dd/mm/yy',
1183
+ 'mm/dd/yy',
1184
+ '',
1185
+ 'd mm yy',
1186
+ 'dd mm yy',
1187
+ '',
1188
+ 'dd m yy',
1189
+ 'dd mm yy',
1190
+ 'dd mmm yy',
1191
+ 'dd mmmm yy',
1192
+ '',
1193
+ 'dd mm y',
1194
+ 'dd mm yyy',
1195
+ 'dd mm yyyy',
1196
+ '',
1197
+ 'd mmmm yyyy',
1198
+ '',
1199
+ 'dd/mm/yy',
1200
+ 'dd/mm/yy hh:mm',
1201
+ 'dd/mm/yy hh:mm:ss',
1202
+ 'dd/mm/yy hh:mm:ss.000',
1203
+ '',
1204
+ 'hh:mm',
1205
+ 'hh:mm:ss',
1206
+ 'hh:mm:ss.000',
1207
+ ]
1208
+
1209
+ # Write the same date and time using each of the above formats. The empty
1210
+ # string formats create a blank line to make the example clearer.
1211
+ #
1212
+ row = 0
1213
+ date_formats.each do |date_format|
1214
+ row += 1
1215
+ next if date_format == ''
1216
+
1217
+ # Create a format for the date or time.
1218
+ format = workbook.add_format(
1219
+ :num_format => date_format,
1220
+ :align => 'left'
1221
+ )
1222
+
1223
+ # Write the same date using different formats.
1224
+ worksheet.write_date_time(row, 0, '2004-08-01T12:30:45.123', format)
1225
+ worksheet.write(row, 1, date_format)
1226
+ end
1227
+
1228
+ # The following is an example of an invalid date. It is written as a string instead
1229
+ # of a number. This is also Excel's default behaviour.
1230
+ #
1231
+ row += 2
1232
+ worksheet.write_date_time(row, 0, '2004-13-01T12:30:45.123')
1233
+ worksheet.write(row, 1, 'Invalid date. Written as string.', bold)
1234
+
1235
+ workbook.close
1236
+
1237
+ # do assertion
1238
+ compare_file("#{PERL_OUTDIR}/date_time.xls", @filename)
1239
+ end
1240
+
1241
+ def test_diag_border
1242
+ workbook = WriteExcel.new(@filename)
1243
+ worksheet = workbook.add_worksheet
1244
+
1245
+ format1 = workbook.add_format(:diag_type => 1)
1246
+ format2 = workbook.add_format(:diag_type => 2)
1247
+ format3 = workbook.add_format(:diag_type => 3)
1248
+ format4 = workbook.add_format(
1249
+ :diag_type => 3,
1250
+ :diag_border => 7,
1251
+ :diag_color => 'red'
1252
+ )
1253
+
1254
+ worksheet.write('B3', 'Text', format1)
1255
+ worksheet.write('B6', 'Text', format2)
1256
+ worksheet.write('B9', 'Text', format3)
1257
+ worksheet.write('B12', 'Text', format4)
1258
+
1259
+ workbook.close
1260
+
1261
+ # do assertion
1262
+ compare_file("#{PERL_OUTDIR}/diag_border.xls", @filename)
1263
+ end
1264
+
1265
+ def test_headers
1266
+ workbook = WriteExcel.new(@filename)
1267
+ preview = "Select Print Preview to see the header and footer"
1268
+
1269
+
1270
+ ######################################################################
1271
+ #
1272
+ # A simple example to start
1273
+ #
1274
+ worksheet1 = workbook.add_worksheet('Simple')
1275
+
1276
+ header1 = '&CHere is some centred text.'
1277
+
1278
+ footer1 = '&LHere is some left aligned text.'
1279
+
1280
+
1281
+ worksheet1.set_header(header1)
1282
+ worksheet1.set_footer(footer1)
1283
+
1284
+ worksheet1.set_column('A:A', 50)
1285
+ worksheet1.write('A1', preview)
1286
+
1287
+
1288
+ ######################################################################
1289
+ #
1290
+ # This is an example of some of the header/footer variables.
1291
+ #
1292
+ worksheet2 = workbook.add_worksheet('Variables')
1293
+
1294
+ header2 = '&LPage &P of &N'+
1295
+ '&CFilename: &F' +
1296
+ '&RSheetname: &A'
1297
+
1298
+ footer2 = '&LCurrent date: &D'+
1299
+ '&RCurrent time: &T'
1300
+
1301
+ worksheet2.set_header(header2)
1302
+ worksheet2.set_footer(footer2)
1303
+
1304
+
1305
+ worksheet2.set_column('A:A', 50)
1306
+ worksheet2.write('A1', preview)
1307
+ worksheet2.write('A21', "Next sheet")
1308
+ worksheet2.set_h_pagebreaks(20)
1309
+
1310
+
1311
+ ######################################################################
1312
+ #
1313
+ # This example shows how to use more than one font
1314
+ #
1315
+ worksheet3 = workbook.add_worksheet('Mixed fonts')
1316
+
1317
+ header3 = '&C' +
1318
+ '&"Courier New,Bold"Hello ' +
1319
+ '&"Arial,Italic"World'
1320
+
1321
+ footer3 = '&C' +
1322
+ '&"Symbol"e' +
1323
+ '&"Arial" = mc&X2'
1324
+
1325
+ worksheet3.set_header(header3)
1326
+ worksheet3.set_footer(footer3)
1327
+
1328
+ worksheet3.set_column('A:A', 50)
1329
+ worksheet3.write('A1', preview)
1330
+
1331
+
1332
+ ######################################################################
1333
+ #
1334
+ # Example of line wrapping
1335
+ #
1336
+ worksheet4 = workbook.add_worksheet('Word wrap')
1337
+
1338
+ header4 = "&CHeading 1\nHeading 2\nHeading 3"
1339
+
1340
+ worksheet4.set_header(header4)
1341
+
1342
+ worksheet4.set_column('A:A', 50)
1343
+ worksheet4.write('A1', preview)
1344
+
1345
+
1346
+ ######################################################################
1347
+ #
1348
+ # Example of inserting a literal ampersand &
1349
+ #
1350
+ worksheet5 = workbook.add_worksheet('Ampersand')
1351
+
1352
+ header5 = "&CCuriouser && Curiouser - Attorneys at Law"
1353
+
1354
+ worksheet5.set_header(header5)
1355
+
1356
+ worksheet5.set_column('A:A', 50)
1357
+ worksheet5.write('A1', preview)
1358
+
1359
+ workbook.close
1360
+
1361
+ # do assertion
1362
+ compare_file("#{PERL_OUTDIR}/headers.xls", @filename)
1363
+ end
1364
+
1365
+ def test_demo
1366
+ workbook = WriteExcel.new(@filename)
1367
+ worksheet = workbook.add_worksheet('Demo')
1368
+ worksheet2 = workbook.add_worksheet('Another sheet')
1369
+ worksheet3 = workbook.add_worksheet('And another')
1370
+
1371
+ bold = workbook.add_format(:bold => 1)
1372
+
1373
+ #######################################################################
1374
+ #
1375
+ # Write a general heading
1376
+ #
1377
+ worksheet.set_column('A:A', 36, bold)
1378
+ worksheet.set_column('B:B', 20 )
1379
+ worksheet.set_row(0, 40 )
1380
+
1381
+ heading = workbook.add_format(
1382
+ :bold => 1,
1383
+ :color => 'blue',
1384
+ :size => 16,
1385
+ :merge => 1,
1386
+ :align => 'vcenter'
1387
+ )
1388
+
1389
+ headings = ['Features of Spreadsheet::WriteExcel', '']
1390
+ worksheet.write_row('A1', headings, heading)
1391
+
1392
+
1393
+ #######################################################################
1394
+ #
1395
+ # Some text examples
1396
+ #
1397
+ text_format = workbook.add_format(
1398
+ :bold => 1,
1399
+ :italic => 1,
1400
+ :color => 'red',
1401
+ :size => 18,
1402
+ :font =>'Lucida Calligraphy'
1403
+ )
1404
+
1405
+ # A phrase in Cyrillic
1406
+ unicode = [
1407
+ "042d0442043e002004440440043004370430002004"+
1408
+ "3d043000200440044304410441043a043e043c0021"
1409
+ ].pack('H*')
1410
+
1411
+ worksheet.write('A2', "Text")
1412
+ worksheet.write('B2', "Hello Excel")
1413
+ worksheet.write('A3', "Formatted text")
1414
+ worksheet.write('B3', "Hello Excel", text_format)
1415
+ worksheet.write('A4', "Unicode text")
1416
+ worksheet.write_utf16be_string('B4', unicode)
1417
+
1418
+
1419
+ #######################################################################
1420
+ #
1421
+ # Some numeric examples
1422
+ #
1423
+ num1_format = workbook.add_format(:num_format => '$#,##0.00')
1424
+ num2_format = workbook.add_format(:num_format => ' d mmmm yyy')
1425
+
1426
+ worksheet.write('A5', "Numbers")
1427
+ worksheet.write('B5', 1234.56)
1428
+ worksheet.write('A6', "Formatted numbers")
1429
+ worksheet.write('B6', 1234.56, num1_format)
1430
+ worksheet.write('A7', "Formatted numbers")
1431
+ worksheet.write('B7', 37257, num2_format)
1432
+
1433
+
1434
+ #######################################################################
1435
+ #
1436
+ # Formulae
1437
+ #
1438
+ worksheet.set_selection('B8')
1439
+ worksheet.write('A8', 'Formulas and functions, "=SIN(PI()/4)"')
1440
+ worksheet.write('B8', '=SIN(PI()/4)')
1441
+
1442
+
1443
+ #######################################################################
1444
+ #
1445
+ # Hyperlinks
1446
+ #
1447
+ worksheet.write('A9', "Hyperlinks")
1448
+ worksheet.write('B9', 'http://www.perl.com/' )
1449
+
1450
+
1451
+ #######################################################################
1452
+ #
1453
+ # Images
1454
+ #
1455
+ worksheet.write('A10', "Images")
1456
+ worksheet.insert_image('B10', 'republic.png', 16, 8)
1457
+
1458
+
1459
+ #######################################################################
1460
+ #
1461
+ # Misc
1462
+ #
1463
+ worksheet.write('A18', "Page/printer setup")
1464
+ worksheet.write('A19', "Multiple worksheets")
1465
+
1466
+ workbook.close
1467
+
1468
+ # do assertion
1469
+ compare_file("#{PERL_OUTDIR}/demo.xls", @filename)
1470
+ end
1471
+
1472
+ def test_unicode_cyrillic
1473
+ # Create a Russian worksheet name in utf8.
1474
+ sheet = [0x0421, 0x0442, 0x0440, 0x0430, 0x043D, 0x0438,
1475
+ 0x0446, 0x0430].pack("U*")
1476
+
1477
+ # Create a Russian string.
1478
+ str = [0x0417, 0x0434, 0x0440, 0x0430, 0x0432, 0x0441,
1479
+ 0x0442, 0x0432, 0x0443, 0x0439, 0x0020, 0x041C,
1480
+ 0x0438, 0x0440, 0x0021].pack("U*")
1481
+
1482
+ workbook = WriteExcel.new(@filename)
1483
+ worksheet = workbook.add_worksheet(sheet + '1')
1484
+
1485
+ worksheet.set_column('A:A', 18)
1486
+ worksheet.write('A1', str)
1487
+
1488
+ workbook.close
1489
+
1490
+ # do assertion
1491
+ compare_file("#{PERL_OUTDIR}/unicode_cyrillic.xls", @filename)
1492
+ end
1493
+
1494
+ def test_chart_area
1495
+ workbook = WriteExcel.new(@filename)
1496
+ worksheet = workbook.add_worksheet
1497
+ bold = workbook.add_format(:bold => 1)
1498
+
1499
+ # Add the data to the worksheet that the charts will refer to.
1500
+ headings = [ 'Category', 'Values 1', 'Values 2' ]
1501
+ data = [
1502
+ [ 2, 3, 4, 5, 6, 7 ],
1503
+ [ 1, 4, 5, 2, 1, 5 ],
1504
+ [ 3, 6, 7, 5, 4, 3 ]
1505
+ ]
1506
+
1507
+ worksheet.write('A1', headings, bold)
1508
+ worksheet.write('A2', data)
1509
+
1510
+
1511
+ ###############################################################################
1512
+ #
1513
+ # Example 1. A minimal chart.
1514
+ #
1515
+ chart1 = workbook.add_chart(:type => Chart::Area)
1516
+
1517
+ # Add values only. Use the default categories.
1518
+ chart1.add_series( :values => '=Sheet1!$B$2:$B$7' )
1519
+
1520
+ ###############################################################################
1521
+ #
1522
+ # Example 2. A minimal chart with user specified categories (X axis)
1523
+ # and a series name.
1524
+ #
1525
+ chart2 = workbook.add_chart(:type => Chart::Area)
1526
+
1527
+ # Configure the series.
1528
+ chart2.add_series(
1529
+ :categories => '=Sheet1!$A$2:$A$7',
1530
+ :values => '=Sheet1!$B$2:$B$7',
1531
+ :name => 'Test data series 1'
1532
+ )
1533
+
1534
+ ###############################################################################
1535
+ #
1536
+ # Example 3. Same as previous chart but with added title and axes labels.
1537
+ #
1538
+ chart3 = workbook.add_chart(:type => Chart::Area)
1539
+
1540
+ # Configure the series.
1541
+ chart3.add_series(
1542
+ :categories => '=Sheet1!$A$2:$A$7',
1543
+ :values => '=Sheet1!$B$2:$B$7',
1544
+ :name => 'Test data series 1'
1545
+ )
1546
+
1547
+ # Add some labels.
1548
+ chart3.set_title( :name => 'Results of sample analysis' )
1549
+ chart3.set_x_axis( :name => 'Sample number' )
1550
+ chart3.set_y_axis( :name => 'Sample length (cm)' )
1551
+
1552
+ ###############################################################################
1553
+ #
1554
+ # Example 4. Same as previous chart but with an added series
1555
+ #
1556
+ chart4 = workbook.add_chart(:name => 'Results Chart', :type => Chart::Area)
1557
+
1558
+ # Configure the series.
1559
+ chart4.add_series(
1560
+ :categories => '=Sheet1!$A$2:$A$7',
1561
+ :values => '=Sheet1!$B$2:$B$7',
1562
+ :name => 'Test data series 1'
1563
+ )
1564
+
1565
+ # Add another series.
1566
+ chart4.add_series(
1567
+ :categories => '=Sheet1!$A$2:$A$7',
1568
+ :values => '=Sheet1!$C$2:$C$7',
1569
+ :name => 'Test data series 2'
1570
+ )
1571
+
1572
+ # Add some labels.
1573
+ chart4.set_title( :name => 'Results of sample analysis' )
1574
+ chart4.set_x_axis( :name => 'Sample number' )
1575
+ chart4.set_y_axis( :name => 'Sample length (cm)' )
1576
+
1577
+ ###############################################################################
1578
+ #
1579
+ # Example 5. Same as Example 3 but as an embedded chart.
1580
+ #
1581
+ chart5 = workbook.add_chart(:type => Chart::Area, :embedded => 1)
1582
+
1583
+ # Configure the series.
1584
+ chart5.add_series(
1585
+ :categories => '=Sheet1!$A$2:$A$7',
1586
+ :values => '=Sheet1!$B$2:$B$7',
1587
+ :name => 'Test data series 1'
1588
+ )
1589
+
1590
+ # Add some labels.
1591
+ chart5.set_title(:name => 'Results of sample analysis' )
1592
+ chart5.set_x_axis(:name => 'Sample number')
1593
+ chart5.set_y_axis(:name => 'Sample length (cm)')
1594
+
1595
+ # Insert the chart into the main worksheet.
1596
+ worksheet.insert_chart('E2', chart5)
1597
+
1598
+ # File save
1599
+ workbook.close
1600
+
1601
+ # do assertion
1602
+ compare_file("#{PERL_OUTDIR}/chart_area.xls", @filename)
1603
+ end
1604
+
1605
+ def test_chart_bar
1606
+ workbook = WriteExcel.new(@filename)
1607
+ worksheet = workbook.add_worksheet
1608
+ bold = workbook.add_format(:bold => 1)
1609
+
1610
+ # Add the data to the worksheet that the charts will refer to.
1611
+ headings = [ 'Category', 'Values 1', 'Values 2' ]
1612
+ data = [
1613
+ [ 2, 3, 4, 5, 6, 7 ],
1614
+ [ 1, 4, 5, 2, 1, 5 ],
1615
+ [ 3, 6, 7, 5, 4, 3 ]
1616
+ ]
1617
+
1618
+ worksheet.write('A1', headings, bold)
1619
+ worksheet.write('A2', data)
1620
+
1621
+
1622
+ ###############################################################################
1623
+ #
1624
+ # Example 1. A minimal chart.
1625
+ #
1626
+ chart1 = workbook.add_chart(:type => Chart::Bar)
1627
+
1628
+ # Add values only. Use the default categories.
1629
+ chart1.add_series( :values => '=Sheet1!$B$2:$B$7' )
1630
+
1631
+ ###############################################################################
1632
+ #
1633
+ # Example 2. A minimal chart with user specified categories (X axis)
1634
+ # and a series name.
1635
+ #
1636
+ chart2 = workbook.add_chart(:type => Chart::Bar)
1637
+
1638
+ # Configure the series.
1639
+ chart2.add_series(
1640
+ :categories => '=Sheet1!$A$2:$A$7',
1641
+ :values => '=Sheet1!$B$2:$B$7',
1642
+ :name => 'Test data series 1'
1643
+ )
1644
+
1645
+ ###############################################################################
1646
+ #
1647
+ # Example 3. Same as previous chart but with added title and axes labels.
1648
+ #
1649
+ chart3 = workbook.add_chart(:type => Chart::Bar)
1650
+
1651
+ # Configure the series.
1652
+ chart3.add_series(
1653
+ :categories => '=Sheet1!$A$2:$A$7',
1654
+ :values => '=Sheet1!$B$2:$B$7',
1655
+ :name => 'Test data series 1'
1656
+ )
1657
+
1658
+ # Add some labels.
1659
+ chart3.set_title( :name => 'Results of sample analysis' )
1660
+ chart3.set_x_axis( :name => 'Sample number' )
1661
+ chart3.set_y_axis( :name => 'Sample length (cm)' )
1662
+
1663
+ ###############################################################################
1664
+ #
1665
+ # Example 4. Same as previous chart but with an added series
1666
+ #
1667
+ chart4 = workbook.add_chart(:name => 'Results Chart', :type => Chart::Bar)
1668
+
1669
+ # Configure the series.
1670
+ chart4.add_series(
1671
+ :categories => '=Sheet1!$A$2:$A$7',
1672
+ :values => '=Sheet1!$B$2:$B$7',
1673
+ :name => 'Test data series 1'
1674
+ )
1675
+
1676
+ # Add another series.
1677
+ chart4.add_series(
1678
+ :categories => '=Sheet1!$A$2:$A$7',
1679
+ :values => '=Sheet1!$C$2:$C$7',
1680
+ :name => 'Test data series 2'
1681
+ )
1682
+
1683
+ # Add some labels.
1684
+ chart4.set_title( :name => 'Results of sample analysis' )
1685
+ chart4.set_x_axis( :name => 'Sample number' )
1686
+ chart4.set_y_axis( :name => 'Sample length (cm)' )
1687
+
1688
+ ###############################################################################
1689
+ #
1690
+ # Example 5. Same as Example 3 but as an embedded chart.
1691
+ #
1692
+ chart5 = workbook.add_chart(:type => Chart::Bar, :embedded => 1)
1693
+
1694
+ # Configure the series.
1695
+ chart5.add_series(
1696
+ :categories => '=Sheet1!$A$2:$A$7',
1697
+ :values => '=Sheet1!$B$2:$B$7',
1698
+ :name => 'Test data series 1'
1699
+ )
1700
+
1701
+ # Add some labels.
1702
+ chart5.set_title(:name => 'Results of sample analysis' )
1703
+ chart5.set_x_axis(:name => 'Sample number')
1704
+ chart5.set_y_axis(:name => 'Sample length (cm)')
1705
+
1706
+ # Insert the chart into the main worksheet.
1707
+ worksheet.insert_chart('E2', chart5)
1708
+
1709
+ # File save
1710
+ workbook.close
1711
+
1712
+ # do assertion
1713
+ compare_file("#{PERL_OUTDIR}/chart_bar.xls", @filename)
1714
+ end
1715
+
1716
+ def test_chart_column
1717
+ workbook = WriteExcel.new(@filename)
1718
+ worksheet = workbook.add_worksheet
1719
+ bold = workbook.add_format(:bold => 1)
1720
+
1721
+ # Add the data to the worksheet that the charts will refer to.
1722
+ headings = [ 'Category', 'Values 1', 'Values 2' ]
1723
+ data = [
1724
+ [ 2, 3, 4, 5, 6, 7 ],
1725
+ [ 1, 4, 5, 2, 1, 5 ],
1726
+ [ 3, 6, 7, 5, 4, 3 ]
1727
+ ]
1728
+
1729
+ worksheet.write('A1', headings, bold)
1730
+ worksheet.write('A2', data)
1731
+
1732
+
1733
+ ###############################################################################
1734
+ #
1735
+ # Example 1. A minimal chart.
1736
+ #
1737
+ chart1 = workbook.add_chart(:type => Chart::Column)
1738
+
1739
+ # Add values only. Use the default categories.
1740
+ chart1.add_series( :values => '=Sheet1!$B$2:$B$7' )
1741
+
1742
+ ###############################################################################
1743
+ #
1744
+ # Example 2. A minimal chart with user specified categories (X axis)
1745
+ # and a series name.
1746
+ #
1747
+ chart2 = workbook.add_chart(:type => Chart::Column)
1748
+
1749
+ # Configure the series.
1750
+ chart2.add_series(
1751
+ :categories => '=Sheet1!$A$2:$A$7',
1752
+ :values => '=Sheet1!$B$2:$B$7',
1753
+ :name => 'Test data series 1'
1754
+ )
1755
+
1756
+ ###############################################################################
1757
+ #
1758
+ # Example 3. Same as previous chart but with added title and axes labels.
1759
+ #
1760
+ chart3 = workbook.add_chart(:type => Chart::Column)
1761
+
1762
+ # Configure the series.
1763
+ chart3.add_series(
1764
+ :categories => '=Sheet1!$A$2:$A$7',
1765
+ :values => '=Sheet1!$B$2:$B$7',
1766
+ :name => 'Test data series 1'
1767
+ )
1768
+
1769
+ # Add some labels.
1770
+ chart3.set_title( :name => 'Results of sample analysis' )
1771
+ chart3.set_x_axis( :name => 'Sample number' )
1772
+ chart3.set_y_axis( :name => 'Sample length (cm)' )
1773
+
1774
+ ###############################################################################
1775
+ #
1776
+ # Example 4. Same as previous chart but with an added series
1777
+ #
1778
+ chart4 = workbook.add_chart(:name => 'Results Chart', :type => Chart::Column)
1779
+
1780
+ # Configure the series.
1781
+ chart4.add_series(
1782
+ :categories => '=Sheet1!$A$2:$A$7',
1783
+ :values => '=Sheet1!$B$2:$B$7',
1784
+ :name => 'Test data series 1'
1785
+ )
1786
+
1787
+ # Add another series.
1788
+ chart4.add_series(
1789
+ :categories => '=Sheet1!$A$2:$A$7',
1790
+ :values => '=Sheet1!$C$2:$C$7',
1791
+ :name => 'Test data series 2'
1792
+ )
1793
+
1794
+ # Add some labels.
1795
+ chart4.set_title( :name => 'Results of sample analysis' )
1796
+ chart4.set_x_axis( :name => 'Sample number' )
1797
+ chart4.set_y_axis( :name => 'Sample length (cm)' )
1798
+
1799
+ ###############################################################################
1800
+ #
1801
+ # Example 5. Same as Example 3 but as an embedded chart.
1802
+ #
1803
+ chart5 = workbook.add_chart(:type => Chart::Column, :embedded => 1)
1804
+
1805
+ # Configure the series.
1806
+ chart5.add_series(
1807
+ :categories => '=Sheet1!$A$2:$A$7',
1808
+ :values => '=Sheet1!$B$2:$B$7',
1809
+ :name => 'Test data series 1'
1810
+ )
1811
+
1812
+ # Add some labels.
1813
+ chart5.set_title(:name => 'Results of sample analysis' )
1814
+ chart5.set_x_axis(:name => 'Sample number')
1815
+ chart5.set_y_axis(:name => 'Sample length (cm)')
1816
+
1817
+ # Insert the chart into the main worksheet.
1818
+ worksheet.insert_chart('E2', chart5)
1819
+
1820
+ # File save
1821
+ workbook.close
1822
+
1823
+ # do assertion
1824
+ compare_file("#{PERL_OUTDIR}/chart_column.xls", @filename)
1825
+ end
1826
+
1827
+ def test_chart_line
1828
+ workbook = WriteExcel.new(@filename)
1829
+ worksheet = workbook.add_worksheet
1830
+ bold = workbook.add_format(:bold => 1)
1831
+
1832
+ # Add the data to the worksheet that the charts will refer to.
1833
+ headings = [ 'Category', 'Values 1', 'Values 2' ]
1834
+ data = [
1835
+ [ 2, 3, 4, 5, 6, 7 ],
1836
+ [ 1, 4, 5, 2, 1, 5 ],
1837
+ [ 3, 6, 7, 5, 4, 3 ]
1838
+ ]
1839
+
1840
+ worksheet.write('A1', headings, bold)
1841
+ worksheet.write('A2', data)
1842
+
1843
+
1844
+ ###############################################################################
1845
+ #
1846
+ # Example 1. A minimal chart.
1847
+ #
1848
+ chart1 = workbook.add_chart(:type => Chart::Line)
1849
+
1850
+ # Add values only. Use the default categories.
1851
+ chart1.add_series( :values => '=Sheet1!$B$2:$B$7' )
1852
+
1853
+ ###############################################################################
1854
+ #
1855
+ # Example 2. A minimal chart with user specified categories (X axis)
1856
+ # and a series name.
1857
+ #
1858
+ chart2 = workbook.add_chart(:type => Chart::Line)
1859
+
1860
+ # Configure the series.
1861
+ chart2.add_series(
1862
+ :categories => '=Sheet1!$A$2:$A$7',
1863
+ :values => '=Sheet1!$B$2:$B$7',
1864
+ :name => 'Test data series 1'
1865
+ )
1866
+
1867
+ ###############################################################################
1868
+ #
1869
+ # Example 3. Same as previous chart but with added title and axes labels.
1870
+ #
1871
+ chart3 = workbook.add_chart(:type => Chart::Line)
1872
+
1873
+ # Configure the series.
1874
+ chart3.add_series(
1875
+ :categories => '=Sheet1!$A$2:$A$7',
1876
+ :values => '=Sheet1!$B$2:$B$7',
1877
+ :name => 'Test data series 1'
1878
+ )
1879
+
1880
+ # Add some labels.
1881
+ chart3.set_title( :name => 'Results of sample analysis' )
1882
+ chart3.set_x_axis( :name => 'Sample number' )
1883
+ chart3.set_y_axis( :name => 'Sample length (cm)' )
1884
+
1885
+ ###############################################################################
1886
+ #
1887
+ # Example 4. Same as previous chart but with an added series
1888
+ #
1889
+ chart4 = workbook.add_chart(:name => 'Results Chart', :type => Chart::Line)
1890
+
1891
+ # Configure the series.
1892
+ chart4.add_series(
1893
+ :categories => '=Sheet1!$A$2:$A$7',
1894
+ :values => '=Sheet1!$B$2:$B$7',
1895
+ :name => 'Test data series 1'
1896
+ )
1897
+
1898
+ # Add another series.
1899
+ chart4.add_series(
1900
+ :categories => '=Sheet1!$A$2:$A$7',
1901
+ :values => '=Sheet1!$C$2:$C$7',
1902
+ :name => 'Test data series 2'
1903
+ )
1904
+
1905
+ # Add some labels.
1906
+ chart4.set_title( :name => 'Results of sample analysis' )
1907
+ chart4.set_x_axis( :name => 'Sample number' )
1908
+ chart4.set_y_axis( :name => 'Sample length (cm)' )
1909
+
1910
+ ###############################################################################
1911
+ #
1912
+ # Example 5. Same as Example 3 but as an embedded chart.
1913
+ #
1914
+ chart5 = workbook.add_chart(:type => Chart::Line, :embedded => 1)
1915
+
1916
+ # Configure the series.
1917
+ chart5.add_series(
1918
+ :categories => '=Sheet1!$A$2:$A$7',
1919
+ :values => '=Sheet1!$B$2:$B$7',
1920
+ :name => 'Test data series 1'
1921
+ )
1922
+
1923
+ # Add some labels.
1924
+ chart5.set_title(:name => 'Results of sample analysis' )
1925
+ chart5.set_x_axis(:name => 'Sample number')
1926
+ chart5.set_y_axis(:name => 'Sample length (cm)')
1927
+
1928
+ # Insert the chart into the main worksheet.
1929
+ worksheet.insert_chart('E2', chart5)
1930
+
1931
+ # File save
1932
+ workbook.close
1933
+
1934
+ # do assertion
1935
+ compare_file("#{PERL_OUTDIR}/chart_line.xls", @filename)
1936
+ end
1937
+
1938
+ def compare_file(expected, target)
1939
+ assert_equal(
1940
+ open(expected, 'rb') { |f| f.read },
1941
+ open(target, 'rb') { |f| f.read }
1942
+ )
1943
+ end
1944
+ end