ricardoo27-writeexcel 0.6.12.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (245) hide show
  1. data/.document +5 -0
  2. data/.gitattributes +1 -0
  3. data/README.rdoc +136 -0
  4. data/Rakefile +52 -0
  5. data/VERSION +1 -0
  6. data/charts/chartex.rb +316 -0
  7. data/charts/demo1.rb +46 -0
  8. data/charts/demo101.bin +0 -0
  9. data/charts/demo2.rb +65 -0
  10. data/charts/demo201.bin +0 -0
  11. data/charts/demo3.rb +117 -0
  12. data/charts/demo301.bin +0 -0
  13. data/charts/demo4.rb +119 -0
  14. data/charts/demo401.bin +0 -0
  15. data/charts/demo5.rb +48 -0
  16. data/charts/demo501.bin +0 -0
  17. data/examples/a_simple.rb +43 -0
  18. data/examples/autofilter.rb +265 -0
  19. data/examples/bigfile.rb +30 -0
  20. data/examples/chart_area.rb +121 -0
  21. data/examples/chart_bar.rb +120 -0
  22. data/examples/chart_column.rb +120 -0
  23. data/examples/chart_line.rb +120 -0
  24. data/examples/chart_pie.rb +108 -0
  25. data/examples/chart_scatter.rb +121 -0
  26. data/examples/chart_stock.rb +148 -0
  27. data/examples/chess.rb +142 -0
  28. data/examples/colors.rb +129 -0
  29. data/examples/comments1.rb +27 -0
  30. data/examples/comments2.rb +352 -0
  31. data/examples/copyformat.rb +52 -0
  32. data/examples/data_validate.rb +279 -0
  33. data/examples/date_time.rb +87 -0
  34. data/examples/defined_name.rb +32 -0
  35. data/examples/demo.rb +124 -0
  36. data/examples/diag_border.rb +36 -0
  37. data/examples/formats.rb +490 -0
  38. data/examples/formula_result.rb +30 -0
  39. data/examples/header.rb +137 -0
  40. data/examples/hide_sheet.rb +29 -0
  41. data/examples/hyperlink.rb +43 -0
  42. data/examples/images.rb +63 -0
  43. data/examples/indent.rb +31 -0
  44. data/examples/merge1.rb +40 -0
  45. data/examples/merge2.rb +45 -0
  46. data/examples/merge3.rb +66 -0
  47. data/examples/merge4.rb +83 -0
  48. data/examples/merge5.rb +80 -0
  49. data/examples/merge6.rb +67 -0
  50. data/examples/outline.rb +255 -0
  51. data/examples/outline_collapsed.rb +209 -0
  52. data/examples/panes.rb +113 -0
  53. data/examples/password_protection.rb +33 -0
  54. data/examples/properties.rb +34 -0
  55. data/examples/properties_jp.rb +33 -0
  56. data/examples/protection.rb +47 -0
  57. data/examples/regions.rb +53 -0
  58. data/examples/repeat.rb +43 -0
  59. data/examples/republic.png +0 -0
  60. data/examples/right_to_left.rb +27 -0
  61. data/examples/row_wrap.rb +53 -0
  62. data/examples/set_first_sheet.rb +14 -0
  63. data/examples/stats.rb +74 -0
  64. data/examples/stocks.rb +81 -0
  65. data/examples/store_formula.rb +15 -0
  66. data/examples/tab_colors.rb +31 -0
  67. data/examples/utf8.rb +15 -0
  68. data/examples/write_arrays.rb +83 -0
  69. data/html/en/doc_en.html +5946 -0
  70. data/html/images/a_simple.jpg +0 -0
  71. data/html/images/area1.jpg +0 -0
  72. data/html/images/bar1.jpg +0 -0
  73. data/html/images/chart_area.xls +0 -0
  74. data/html/images/column1.jpg +0 -0
  75. data/html/images/data_validation.jpg +0 -0
  76. data/html/images/line1.jpg +0 -0
  77. data/html/images/pie1.jpg +0 -0
  78. data/html/images/regions.jpg +0 -0
  79. data/html/images/scatter1.jpg +0 -0
  80. data/html/images/stats.jpg +0 -0
  81. data/html/images/stock1.jpg +0 -0
  82. data/html/images/stocks.jpg +0 -0
  83. data/html/index.html +16 -0
  84. data/html/style.css +433 -0
  85. data/lib/writeexcel.rb +1159 -0
  86. data/lib/writeexcel/biffwriter.rb +223 -0
  87. data/lib/writeexcel/caller_info.rb +12 -0
  88. data/lib/writeexcel/cell_range.rb +332 -0
  89. data/lib/writeexcel/chart.rb +1968 -0
  90. data/lib/writeexcel/charts/area.rb +154 -0
  91. data/lib/writeexcel/charts/bar.rb +177 -0
  92. data/lib/writeexcel/charts/column.rb +156 -0
  93. data/lib/writeexcel/charts/external.rb +66 -0
  94. data/lib/writeexcel/charts/line.rb +154 -0
  95. data/lib/writeexcel/charts/pie.rb +169 -0
  96. data/lib/writeexcel/charts/scatter.rb +192 -0
  97. data/lib/writeexcel/charts/stock.rb +213 -0
  98. data/lib/writeexcel/col_info.rb +87 -0
  99. data/lib/writeexcel/colors.rb +68 -0
  100. data/lib/writeexcel/comments.rb +460 -0
  101. data/lib/writeexcel/compatibility.rb +65 -0
  102. data/lib/writeexcel/convert_date_time.rb +117 -0
  103. data/lib/writeexcel/data_validations.rb +370 -0
  104. data/lib/writeexcel/debug_info.rb +41 -0
  105. data/lib/writeexcel/embedded_chart.rb +35 -0
  106. data/lib/writeexcel/excelformula.y +139 -0
  107. data/lib/writeexcel/excelformulaparser.rb +587 -0
  108. data/lib/writeexcel/format.rb +1575 -0
  109. data/lib/writeexcel/formula.rb +987 -0
  110. data/lib/writeexcel/helper.rb +78 -0
  111. data/lib/writeexcel/image.rb +218 -0
  112. data/lib/writeexcel/olewriter.rb +305 -0
  113. data/lib/writeexcel/outline.rb +24 -0
  114. data/lib/writeexcel/properties.rb +242 -0
  115. data/lib/writeexcel/shared_string_table.rb +153 -0
  116. data/lib/writeexcel/storage_lite.rb +984 -0
  117. data/lib/writeexcel/workbook.rb +2478 -0
  118. data/lib/writeexcel/worksheet.rb +6925 -0
  119. data/lib/writeexcel/worksheets.rb +25 -0
  120. data/lib/writeexcel/write_file.rb +63 -0
  121. data/test/excelfile/Chart1.xls +0 -0
  122. data/test/excelfile/Chart2.xls +0 -0
  123. data/test/excelfile/Chart3.xls +0 -0
  124. data/test/excelfile/Chart4.xls +0 -0
  125. data/test/excelfile/Chart5.xls +0 -0
  126. data/test/helper.rb +31 -0
  127. data/test/perl_output/Chart1.xls.data +0 -0
  128. data/test/perl_output/Chart2.xls.data +0 -0
  129. data/test/perl_output/Chart3.xls.data +0 -0
  130. data/test/perl_output/Chart4.xls.data +0 -0
  131. data/test/perl_output/Chart5.xls.data +0 -0
  132. data/test/perl_output/README +31 -0
  133. data/test/perl_output/a_simple.xls +0 -0
  134. data/test/perl_output/autofilter.xls +0 -0
  135. data/test/perl_output/biff_add_continue_testdata +0 -0
  136. data/test/perl_output/chart_area.xls +0 -0
  137. data/test/perl_output/chart_bar.xls +0 -0
  138. data/test/perl_output/chart_column.xls +0 -0
  139. data/test/perl_output/chart_line.xls +0 -0
  140. data/test/perl_output/chess.xls +0 -0
  141. data/test/perl_output/colors.xls +0 -0
  142. data/test/perl_output/comments0.xls +0 -0
  143. data/test/perl_output/comments1.xls +0 -0
  144. data/test/perl_output/comments2.xls +0 -0
  145. data/test/perl_output/data_validate.xls +0 -0
  146. data/test/perl_output/date_time.xls +0 -0
  147. data/test/perl_output/defined_name.xls +0 -0
  148. data/test/perl_output/demo.xls +0 -0
  149. data/test/perl_output/demo101.bin +0 -0
  150. data/test/perl_output/demo201.bin +0 -0
  151. data/test/perl_output/demo301.bin +0 -0
  152. data/test/perl_output/demo401.bin +0 -0
  153. data/test/perl_output/demo501.bin +0 -0
  154. data/test/perl_output/diag_border.xls +0 -0
  155. data/test/perl_output/f_font_biff +0 -0
  156. data/test/perl_output/f_font_key +1 -0
  157. data/test/perl_output/f_xf_biff +0 -0
  158. data/test/perl_output/file_font_biff +0 -0
  159. data/test/perl_output/file_font_key +1 -0
  160. data/test/perl_output/file_xf_biff +0 -0
  161. data/test/perl_output/formula_result.xls +0 -0
  162. data/test/perl_output/headers.xls +0 -0
  163. data/test/perl_output/hidden.xls +0 -0
  164. data/test/perl_output/hide_zero.xls +0 -0
  165. data/test/perl_output/hyperlink.xls +0 -0
  166. data/test/perl_output/images.xls +0 -0
  167. data/test/perl_output/indent.xls +0 -0
  168. data/test/perl_output/merge1.xls +0 -0
  169. data/test/perl_output/merge2.xls +0 -0
  170. data/test/perl_output/merge3.xls +0 -0
  171. data/test/perl_output/merge4.xls +0 -0
  172. data/test/perl_output/merge5.xls +0 -0
  173. data/test/perl_output/merge6.xls +0 -0
  174. data/test/perl_output/ole_write_header +0 -0
  175. data/test/perl_output/outline.xls +0 -0
  176. data/test/perl_output/outline_collapsed.xls +0 -0
  177. data/test/perl_output/panes.xls +0 -0
  178. data/test/perl_output/password_protection.xls +0 -0
  179. data/test/perl_output/protection.xls +0 -0
  180. data/test/perl_output/regions.xls +0 -0
  181. data/test/perl_output/right_to_left.xls +0 -0
  182. data/test/perl_output/set_first_sheet.xls +0 -0
  183. data/test/perl_output/stats.xls +0 -0
  184. data/test/perl_output/stocks.xls +0 -0
  185. data/test/perl_output/store_formula.xls +0 -0
  186. data/test/perl_output/tab_colors.xls +0 -0
  187. data/test/perl_output/unicode_cyrillic.xls +0 -0
  188. data/test/perl_output/utf8.xls +0 -0
  189. data/test/perl_output/workbook1.xls +0 -0
  190. data/test/perl_output/workbook2.xls +0 -0
  191. data/test/perl_output/ws_colinfo +1 -0
  192. data/test/perl_output/ws_store_colinfo +0 -0
  193. data/test/perl_output/ws_store_dimensions +0 -0
  194. data/test/perl_output/ws_store_filtermode +0 -0
  195. data/test/perl_output/ws_store_filtermode_off +0 -0
  196. data/test/perl_output/ws_store_filtermode_on +0 -0
  197. data/test/perl_output/ws_store_selection +0 -0
  198. data/test/perl_output/ws_store_window2 +1 -0
  199. data/test/republic.png +0 -0
  200. data/test/test_00_IEEE_double.rb +13 -0
  201. data/test/test_01_add_worksheet.rb +10 -0
  202. data/test/test_02_merge_formats.rb +49 -0
  203. data/test/test_04_dimensions.rb +388 -0
  204. data/test/test_05_rows.rb +175 -0
  205. data/test/test_06_extsst.rb +74 -0
  206. data/test/test_11_date_time.rb +475 -0
  207. data/test/test_12_date_only.rb +525 -0
  208. data/test/test_13_date_seconds.rb +477 -0
  209. data/test/test_21_escher.rb +624 -0
  210. data/test/test_22_mso_drawing_group.rb +741 -0
  211. data/test/test_23_note.rb +57 -0
  212. data/test/test_24_txo.rb +74 -0
  213. data/test/test_25_position_object.rb +80 -0
  214. data/test/test_26_autofilter.rb +309 -0
  215. data/test/test_27_autofilter.rb +126 -0
  216. data/test/test_28_autofilter.rb +156 -0
  217. data/test/test_29_process_jpg.rb +670 -0
  218. data/test/test_30_validation_dval.rb +74 -0
  219. data/test/test_31_validation_dv_strings.rb +123 -0
  220. data/test/test_32_validation_dv_formula.rb +203 -0
  221. data/test/test_40_property_types.rb +188 -0
  222. data/test/test_41_properties.rb +235 -0
  223. data/test/test_42_set_properties.rb +434 -0
  224. data/test/test_50_name_stored.rb +295 -0
  225. data/test/test_51_name_print_area.rb +353 -0
  226. data/test/test_52_name_print_titles.rb +450 -0
  227. data/test/test_53_autofilter.rb +199 -0
  228. data/test/test_60_chart_generic.rb +574 -0
  229. data/test/test_61_chart_subclasses.rb +84 -0
  230. data/test/test_62_chart_formats.rb +268 -0
  231. data/test/test_63_chart_area_formats.rb +645 -0
  232. data/test/test_biff.rb +71 -0
  233. data/test/test_big_workbook.rb +17 -0
  234. data/test/test_compatibility.rb +12 -0
  235. data/test/test_example_match.rb +3246 -0
  236. data/test/test_format.rb +1189 -0
  237. data/test/test_formula.rb +61 -0
  238. data/test/test_ole.rb +102 -0
  239. data/test/test_storage_lite.rb +116 -0
  240. data/test/test_workbook.rb +146 -0
  241. data/test/test_worksheet.rb +106 -0
  242. data/utils/add_magic_comment.rb +80 -0
  243. data/writeexcel.gemspec +278 -0
  244. data/writeexcel.rdoc +1425 -0
  245. metadata +292 -0
@@ -0,0 +1,71 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'helper'
3
+ require 'stringio'
4
+
5
+ class TC_BIFFWriter < Test::Unit::TestCase
6
+
7
+ TEST_DIR = File.expand_path(File.dirname(__FILE__))
8
+ PERL_OUTDIR = File.join(TEST_DIR, 'perl_output')
9
+
10
+ def setup
11
+ @biff = BIFFWriter.new
12
+ @ruby_file = StringIO.new
13
+ end
14
+
15
+ def test_append_no_error
16
+ assert_nothing_raised{ @biff.append("World") }
17
+ end
18
+
19
+ def test_prepend_no_error
20
+ assert_nothing_raised{ @biff.prepend("Hello") }
21
+ end
22
+
23
+ def test_data_added
24
+ assert_nothing_raised{ @biff.append("Hello", "World") }
25
+ data = ''
26
+ while d = @biff.get_data
27
+ data += d
28
+ end
29
+ assert_equal("HelloWorld", data, "Bad data contents")
30
+ assert_equal(10, @biff.datasize, "Bad data size")
31
+ end
32
+
33
+ def test_data_prepended
34
+
35
+ assert_nothing_raised{ @biff.append("Hello") }
36
+ assert_nothing_raised{ @biff.prepend("World") }
37
+ data = ''
38
+ while d = @biff.get_data
39
+ data += d
40
+ end
41
+ assert_equal("WorldHello", data, "Bad data contents")
42
+ assert_equal(10, @biff.datasize, "Bad data size")
43
+ end
44
+
45
+ def test_store_bof_length
46
+ assert_nothing_raised{ @biff.store_bof }
47
+ assert_equal(20, @biff.datasize, "Bad data size after store_bof call")
48
+ end
49
+
50
+ def test_store_eof_length
51
+ assert_nothing_raised{ @biff.store_eof }
52
+ assert_equal(4, @biff.datasize, "Bad data size after store_eof call")
53
+ end
54
+
55
+ def test_datasize_mixed
56
+ assert_nothing_raised{ @biff.append("Hello") }
57
+ assert_nothing_raised{ @biff.prepend("World") }
58
+ assert_nothing_raised{ @biff.store_bof }
59
+ assert_nothing_raised{ @biff.store_eof }
60
+ assert_equal(34, @biff.datasize, "Bad data size for mixed data")
61
+ end
62
+
63
+ def test_add_continue
64
+ perl_file = "#{PERL_OUTDIR}/biff_add_continue_testdata"
65
+ size = File.size(perl_file)
66
+ @ruby_file.print(@biff.add_continue('testdata'))
67
+ rsize = @ruby_file.size
68
+ assert_equal(size, rsize, "File sizes not the same")
69
+ compare_file(perl_file, @ruby_file)
70
+ end
71
+ end
@@ -0,0 +1,17 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'helper'
3
+ require 'stringio'
4
+
5
+ class TC_BigWorkbook < Test::Unit::TestCase
6
+
7
+ def test_big_workbook_saves
8
+ workbook = Workbook.new(StringIO.new)
9
+ 4.times do
10
+ worksheet = workbook.add_worksheet
11
+ 500.times {|i| worksheet.write_row(i, 0, [rand(10000).to_s])}
12
+ end
13
+
14
+ assert_nothing_raised { workbook.close }
15
+ end
16
+
17
+ end
@@ -0,0 +1,12 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'helper'
3
+ require 'nkf'
4
+
5
+ class TC_Compatibility < Test::Unit::TestCase
6
+ def test_ord
7
+ a = 'a'
8
+ abc = 'abc'
9
+ assert_equal(97, a.ord, "#{a}.ord faild\n")
10
+ assert_equal(97, abc.ord, "#{abc}.ord faild\n")
11
+ end
12
+ end
@@ -0,0 +1,3246 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'helper'
3
+ require 'stringio'
4
+
5
+ class TC_example_match < Test::Unit::TestCase
6
+
7
+ TEST_DIR = File.expand_path(File.dirname(__FILE__))
8
+ PERL_OUTDIR = File.join(TEST_DIR, 'perl_output')
9
+
10
+ def setup
11
+ @file = StringIO.new
12
+ end
13
+
14
+ def test_a_simple
15
+ workbook = WriteExcel.new(@file)
16
+ worksheet = workbook.add_worksheet
17
+
18
+ # The general syntax is write(row, column, token). Note that row and
19
+ # column are zero indexed
20
+ #
21
+
22
+ # Write some text
23
+ worksheet.write(0, 0, "Hi Excel!")
24
+
25
+
26
+ # Write some numbers
27
+ worksheet.write(2, 0, 3) # Writes 3
28
+ worksheet.write(3, 0, 3.00000) # Writes 3
29
+ worksheet.write(4, 0, 3.00001) # Writes 3.00001
30
+ worksheet.write(5, 0, 3.14159) # TeX revision no.?
31
+
32
+
33
+ # Write some formulas
34
+ worksheet.write(7, 0, '=A3 + A6')
35
+ worksheet.write(8, 0, '=IF(A5>3,"Yes", "No")')
36
+
37
+
38
+ # Write a hyperlink
39
+ worksheet.write(10, 0, 'http://www.perl.com/')
40
+
41
+ # File save
42
+ workbook.close
43
+ # do assertion
44
+ compare_file("#{PERL_OUTDIR}/a_simple.xls", @file)
45
+ end
46
+
47
+ def test_autofilter
48
+ workbook = WriteExcel.new(@file)
49
+
50
+ worksheet1 = workbook.add_worksheet
51
+ worksheet2 = workbook.add_worksheet
52
+ worksheet3 = workbook.add_worksheet
53
+ worksheet4 = workbook.add_worksheet
54
+ worksheet5 = workbook.add_worksheet
55
+ worksheet6 = workbook.add_worksheet
56
+
57
+ bold = workbook.add_format(:bold => 1)
58
+
59
+ # Extract the data embedded at the end of this file.
60
+ headings = %w(Region Item Volume Month)
61
+ data = get_data_for_autofilter
62
+
63
+ # Set up several sheets with the same data.
64
+ workbook.sheets.each do |worksheet|
65
+ worksheet.set_column('A:D', 12)
66
+ worksheet.set_row(0, 20, bold)
67
+ worksheet.write('A1', headings)
68
+ end
69
+
70
+ ###############################################################################
71
+ #
72
+ # Example 1. Autofilter without conditions.
73
+ #
74
+
75
+ worksheet1.autofilter('A1:D51')
76
+ worksheet1.write('A2', [data])
77
+
78
+ ###############################################################################
79
+ #
80
+ #
81
+ # Example 2. Autofilter with a filter condition in the first column.
82
+ #
83
+
84
+ # The range in this example is the same as above but in row-column notation.
85
+ worksheet2.autofilter(0, 0, 50, 3)
86
+
87
+ # The placeholder "Region" in the filter is ignored and can be any string
88
+ # that adds clarity to the expression.
89
+ #
90
+ worksheet2.filter_column(0, 'Region eq East')
91
+
92
+ #
93
+ # Hide the rows that don't match the filter criteria.
94
+ #
95
+ row = 1
96
+
97
+ data.each do |row_data|
98
+ region = row_data[0]
99
+
100
+ if region == 'East'
101
+ # Row is visible.
102
+ else
103
+ # Hide row.
104
+ worksheet2.set_row(row, nil, nil, 1)
105
+ end
106
+
107
+ worksheet2.write(row, 0, row_data)
108
+ row += 1
109
+ end
110
+
111
+
112
+ ###############################################################################
113
+ #
114
+ #
115
+ # Example 3. Autofilter with a dual filter condition in one of the columns.
116
+ #
117
+
118
+ worksheet3.autofilter('A1:D51')
119
+
120
+ worksheet3.filter_column('A', 'x eq East or x eq South')
121
+
122
+ #
123
+ # Hide the rows that don't match the filter criteria.
124
+ #
125
+ row = 1
126
+
127
+ data.each do |row_data|
128
+ region = row_data[0]
129
+
130
+ if region == 'East' || region == 'South'
131
+ # Row is visible.
132
+ else
133
+ # Hide row.
134
+ worksheet3.set_row(row, nil, nil, 1)
135
+ end
136
+
137
+ worksheet3.write(row, 0, row_data)
138
+ row += 1
139
+ end
140
+
141
+
142
+ ###############################################################################
143
+ #
144
+ #
145
+ # Example 4. Autofilter with filter conditions in two columns.
146
+ #
147
+
148
+ worksheet4.autofilter('A1:D51')
149
+
150
+ worksheet4.filter_column('A', 'x eq East')
151
+ worksheet4.filter_column('C', 'x > 3000 and x < 8000' )
152
+
153
+ #
154
+ # Hide the rows that don't match the filter criteria.
155
+ #
156
+ row = 1
157
+
158
+ data.each do |row_data|
159
+ region = row_data[0]
160
+ volume = row_data[2]
161
+
162
+ if region == 'East' && volume > 3000 && volume < 8000
163
+ # Row is visible.
164
+ else
165
+ # Hide row.
166
+ worksheet4.set_row(row, nil, nil, 1)
167
+ end
168
+
169
+ worksheet4.write(row, 0, row_data)
170
+ row += 1
171
+ end
172
+
173
+
174
+ ###############################################################################
175
+ #
176
+ #
177
+ # Example 5. Autofilter with filter for blanks.
178
+ #
179
+
180
+ # Create a blank cell in our test data.
181
+ data[5][0] = ''
182
+
183
+ worksheet5.autofilter('A1:D51')
184
+ worksheet5.filter_column('A', 'x == Blanks')
185
+
186
+ #
187
+ # Hide the rows that don't match the filter criteria.
188
+ #
189
+ row = 1
190
+
191
+ data.each do |row_data|
192
+ region = row_data[0]
193
+
194
+ if region == ''
195
+ # Row is visible.
196
+ else
197
+ # Hide row.
198
+ worksheet5.set_row(row, nil, nil, 1)
199
+ end
200
+
201
+ worksheet5.write(row, 0, row_data)
202
+ row += 1
203
+ end
204
+
205
+
206
+ ###############################################################################
207
+ #
208
+ #
209
+ # Example 6. Autofilter with filter for non-blanks.
210
+ #
211
+
212
+ worksheet6.autofilter('A1:D51')
213
+ worksheet6.filter_column('A', 'x == NonBlanks')
214
+
215
+ #
216
+ # Hide the rows that don't match the filter criteria.
217
+ #
218
+ row = 1
219
+
220
+ data.each do |row_data|
221
+ region = row_data[0]
222
+
223
+ if region != ''
224
+ # Row is visible.
225
+ else
226
+ # Hide row.
227
+ worksheet6.set_row(row, nil, nil, 1)
228
+ end
229
+
230
+ worksheet6.write(row, 0, row_data)
231
+ row += 1
232
+ end
233
+
234
+ workbook.close
235
+
236
+ # do assertion
237
+ compare_file("#{PERL_OUTDIR}/autofilter.xls", @file)
238
+ end
239
+
240
+ def get_data_for_autofilter
241
+ [
242
+ ['East', 'Apple', 9000, 'July'],
243
+ ['East', 'Apple', 5000, 'July'],
244
+ ['South', 'Orange', 9000, 'September'],
245
+ ['North', 'Apple', 2000, 'November'],
246
+ ['West', 'Apple', 9000, 'November'],
247
+ ['South', 'Pear', 7000, 'October'],
248
+ ['North', 'Pear', 9000, 'August'],
249
+ ['West', 'Orange', 1000, 'December'],
250
+ ['West', 'Grape', 1000, 'November'],
251
+ ['South', 'Pear', 10000, 'April'],
252
+ ['West', 'Grape', 6000, 'January'],
253
+ ['South', 'Orange', 3000, 'May'],
254
+ ['North', 'Apple', 3000, 'December'],
255
+ ['South', 'Apple', 7000, 'February'],
256
+ ['West', 'Grape', 1000, 'December'],
257
+ ['East', 'Grape', 8000, 'February'],
258
+ ['South', 'Grape', 10000, 'June'],
259
+ ['West', 'Pear', 7000, 'December'],
260
+ ['South', 'Apple', 2000, 'October'],
261
+ ['East', 'Grape', 7000, 'December'],
262
+ ['North', 'Grape', 6000, 'April'],
263
+ ['East', 'Pear', 8000, 'February'],
264
+ ['North', 'Apple', 7000, 'August'],
265
+ ['North', 'Orange', 7000, 'July'],
266
+ ['North', 'Apple', 6000, 'June'],
267
+ ['South', 'Grape', 8000, 'September'],
268
+ ['West', 'Apple', 3000, 'October'],
269
+ ['South', 'Orange', 10000, 'November'],
270
+ ['West', 'Grape', 4000, 'July'],
271
+ ['North', 'Orange', 5000, 'August'],
272
+ ['East', 'Orange', 1000, 'November'],
273
+ ['East', 'Orange', 4000, 'October'],
274
+ ['North', 'Grape', 5000, 'August'],
275
+ ['East', 'Apple', 1000, 'December'],
276
+ ['South', 'Apple', 10000, 'March'],
277
+ ['East', 'Grape', 7000, 'October'],
278
+ ['West', 'Grape', 1000, 'September'],
279
+ ['East', 'Grape', 10000, 'October'],
280
+ ['South', 'Orange', 8000, 'March'],
281
+ ['North', 'Apple', 4000, 'July'],
282
+ ['South', 'Orange', 5000, 'July'],
283
+ ['West', 'Apple', 4000, 'June'],
284
+ ['East', 'Apple', 5000, 'April'],
285
+ ['North', 'Pear', 3000, 'August'],
286
+ ['East', 'Grape', 9000, 'November'],
287
+ ['North', 'Orange', 8000, 'October'],
288
+ ['East', 'Apple', 10000, 'June'],
289
+ ['South', 'Pear', 1000, 'December'],
290
+ ['North', 'Grape', 10000, 'July'],
291
+ ['East', 'Grape', 6000, 'February'],
292
+ ]
293
+ end
294
+
295
+ def test_regions
296
+ workbook = WriteExcel.new(@file)
297
+
298
+ # Add some worksheets
299
+ north = workbook.add_worksheet("North")
300
+ south = workbook.add_worksheet("South")
301
+ east = workbook.add_worksheet("East")
302
+ west = workbook.add_worksheet("West")
303
+
304
+ # Add a Format
305
+ format = workbook.add_format()
306
+ format.set_bold()
307
+ format.set_color('blue')
308
+
309
+ # Add a caption to each worksheet
310
+ workbook.sheets.each do |worksheet|
311
+ worksheet.write(0, 0, "Sales", format)
312
+ end
313
+
314
+ # Write some data
315
+ north.write(0, 1, 200000)
316
+ south.write(0, 1, 100000)
317
+ east.write(0, 1, 150000)
318
+ west.write(0, 1, 100000)
319
+
320
+ # Set the active worksheet
321
+ bp=1
322
+ south.activate()
323
+
324
+ # Set the width of the first column
325
+ south.set_column(0, 0, 20)
326
+
327
+ # Set the active cell
328
+ south.set_selection(0, 1)
329
+
330
+ workbook.close
331
+
332
+ # do assertion
333
+ compare_file("#{PERL_OUTDIR}/regions.xls", @file)
334
+ end
335
+
336
+ def test_stats
337
+ workbook = WriteExcel.new(@file)
338
+ worksheet = workbook.add_worksheet('Test data')
339
+
340
+ # Set the column width for columns 1
341
+ worksheet.set_column(0, 0, 20)
342
+
343
+ # Create a format for the headings
344
+ format = workbook.add_format
345
+ format.set_bold
346
+
347
+ # Write the sample data
348
+ worksheet.write(0, 0, 'Sample', format)
349
+ worksheet.write(0, 1, 1)
350
+ worksheet.write(0, 2, 2)
351
+ worksheet.write(0, 3, 3)
352
+ worksheet.write(0, 4, 4)
353
+ worksheet.write(0, 5, 5)
354
+ worksheet.write(0, 6, 6)
355
+ worksheet.write(0, 7, 7)
356
+ worksheet.write(0, 8, 8)
357
+
358
+ worksheet.write(1, 0, 'Length', format)
359
+ worksheet.write(1, 1, 25.4)
360
+ worksheet.write(1, 2, 25.4)
361
+ worksheet.write(1, 3, 24.8)
362
+ worksheet.write(1, 4, 25.0)
363
+ worksheet.write(1, 5, 25.3)
364
+ worksheet.write(1, 6, 24.9)
365
+ worksheet.write(1, 7, 25.2)
366
+ worksheet.write(1, 8, 24.8)
367
+
368
+ # Write some statistical functions
369
+ worksheet.write(4, 0, 'Count', format)
370
+ worksheet.write(4, 1, '=COUNT(B1:I1)')
371
+
372
+ worksheet.write(5, 0, 'Sum', format)
373
+ worksheet.write(5, 1, '=SUM(B2:I2)')
374
+
375
+ worksheet.write(6, 0, 'Average', format)
376
+ worksheet.write(6, 1, '=AVERAGE(B2:I2)')
377
+
378
+ worksheet.write(7, 0, 'Min', format)
379
+ worksheet.write(7, 1, '=MIN(B2:I2)')
380
+
381
+ worksheet.write(8, 0, 'Max', format)
382
+ worksheet.write(8, 1, '=MAX(B2:I2)')
383
+
384
+ worksheet.write(9, 0, 'Standard Deviation', format)
385
+ worksheet.write(9, 1, '=STDEV(B2:I2)')
386
+
387
+ worksheet.write(10, 0, 'Kurtosis', format)
388
+ worksheet.write(10, 1, '=KURT(B2:I2)')
389
+
390
+ workbook.close
391
+
392
+ # do assertion
393
+ compare_file("#{PERL_OUTDIR}/stats.xls", @file)
394
+ end
395
+
396
+ def test_hyperlink1
397
+ # Create a new workbook and add a worksheet
398
+ workbook = WriteExcel.new(@file)
399
+ worksheet = workbook.add_worksheet('Hyperlinks')
400
+
401
+ # Format the first column
402
+ worksheet.set_column('A:A', 30)
403
+ worksheet.set_selection('B1')
404
+
405
+
406
+ # Add a sample format
407
+ format = workbook.add_format
408
+ format.set_size(12)
409
+ format.set_bold
410
+ format.set_color('red')
411
+ format.set_underline
412
+
413
+
414
+ # Write some hyperlinks
415
+ worksheet.write('A1', 'http://www.perl.com/' )
416
+ worksheet.write('A3', 'http://www.perl.com/', 'Perl home' )
417
+ worksheet.write('A5', 'http://www.perl.com/', nil, format)
418
+ worksheet.write('A7', 'mailto:jmcnamara@cpan.org', 'Mail me')
419
+
420
+ # Write a URL that isn't a hyperlink
421
+ worksheet.write_string('A9', 'http://www.perl.com/')
422
+
423
+ workbook.close
424
+
425
+ # do assertion
426
+ compare_file("#{PERL_OUTDIR}/hyperlink.xls", @file)
427
+ end
428
+
429
+ def test_copyformat
430
+ # Create workbook1
431
+ workbook1 = WriteExcel.new(@file)
432
+ worksheet1 = workbook1.add_worksheet
433
+ format1a = workbook1.add_format
434
+ format1b = workbook1.add_format
435
+
436
+ # Create workbook2
437
+ file2 = StringIO.new
438
+ workbook2 = WriteExcel.new(file2)
439
+ worksheet2 = workbook2.add_worksheet
440
+ format2a = workbook2.add_format
441
+ format2b = workbook2.add_format
442
+
443
+ # Create a global format object that isn't tied to a workbook
444
+ global_format = Writeexcel::Format.new
445
+
446
+ # Set the formatting
447
+ global_format.set_color('blue')
448
+ global_format.set_bold
449
+ global_format.set_italic
450
+
451
+ # Create another example format
452
+ format1b.set_color('red')
453
+
454
+ # Copy the global format properties to the worksheet formats
455
+ format1a.copy(global_format)
456
+ format2a.copy(global_format)
457
+
458
+ # Copy a format from worksheet1 to worksheet2
459
+ format2b.copy(format1b)
460
+
461
+ # Write some output
462
+ worksheet1.write(0, 0, "Ciao", format1a)
463
+ worksheet1.write(1, 0, "Ciao", format1b)
464
+
465
+ worksheet2.write(0, 0, "Hello", format2a)
466
+ worksheet2.write(1, 0, "Hello", format2b)
467
+ workbook1.close
468
+ workbook2.close
469
+
470
+ # do assertion
471
+ compare_file("#{PERL_OUTDIR}/workbook1.xls", @file)
472
+ compare_file("#{PERL_OUTDIR}/workbook2.xls", file2)
473
+ end
474
+
475
+ def test_data_validate
476
+ workbook = WriteExcel.new(@file)
477
+ worksheet = workbook.add_worksheet
478
+
479
+ # Add a format for the header cells.
480
+ header_format = workbook.add_format(
481
+ :border => 1,
482
+ :bg_color => 43,
483
+ :bold => 1,
484
+ :text_wrap => 1,
485
+ :valign => 'vcenter',
486
+ :indent => 1
487
+ )
488
+
489
+ # Set up layout of the worksheet.
490
+ worksheet.set_column('A:A', 64)
491
+ worksheet.set_column('B:B', 15)
492
+ worksheet.set_column('D:D', 15)
493
+ worksheet.set_row(0, 36)
494
+ worksheet.set_selection('B3')
495
+
496
+
497
+ # Write the header cells and some data that will be used in the examples.
498
+ row = 0
499
+ heading1 = 'Some examples of data validation in WriteExcel'
500
+ heading2 = 'Enter values in this column'
501
+ heading3 = 'Sample Data'
502
+
503
+ worksheet.write('A1', heading1, header_format)
504
+ worksheet.write('B1', heading2, header_format)
505
+ worksheet.write('D1', heading3, header_format)
506
+
507
+ worksheet.write('D3', ['Integers', 1, 10])
508
+ worksheet.write('D4', ['List data', 'open', 'high', 'close'])
509
+ worksheet.write('D5', ['Formula', '=AND(F5=50,G5=60)', 50, 60])
510
+
511
+
512
+ #
513
+ # Example 1. Limiting input to an integer in a fixed range.
514
+ #
515
+ txt = 'Enter an integer between 1 and 10'
516
+ row += 2
517
+
518
+ worksheet.write(row, 0, txt)
519
+ worksheet.data_validation(row, 1,
520
+ {
521
+ :validate => 'integer',
522
+ :criteria => 'between',
523
+ :minimum => 1,
524
+ :maximum => 10
525
+ })
526
+
527
+
528
+ #
529
+ # Example 2. Limiting input to an integer outside a fixed range.
530
+ #
531
+ txt = 'Enter an integer that is not between 1 and 10 (using cell references)'
532
+ row += 2
533
+
534
+ worksheet.write(row, 0, txt)
535
+ worksheet.data_validation(row, 1,
536
+ {
537
+ :validate => 'integer',
538
+ :criteria => 'not between',
539
+ :minimum => '=E3',
540
+ :maximum => '=F3'
541
+ })
542
+
543
+
544
+ #
545
+ # Example 3. Limiting input to an integer greater than a fixed value.
546
+ #
547
+ txt = 'Enter an integer greater than 0'
548
+ row += 2
549
+
550
+ worksheet.write(row, 0, txt)
551
+ worksheet.data_validation(row, 1,
552
+ {
553
+ :validate => 'integer',
554
+ :criteria => '>',
555
+ :value => 0
556
+ })
557
+
558
+
559
+ #
560
+ # Example 4. Limiting input to an integer less than a fixed value.
561
+ #
562
+ txt = 'Enter an integer less than 10'
563
+ row += 2
564
+
565
+ worksheet.write(row, 0, txt)
566
+ worksheet.data_validation(row, 1,
567
+ {
568
+ :validate => 'integer',
569
+ :criteria => '<',
570
+ :value => 10
571
+ })
572
+
573
+
574
+ #
575
+ # Example 5. Limiting input to a decimal in a fixed range.
576
+ #
577
+ txt = 'Enter a decimal between 0.1 and 0.5'
578
+ row += 2
579
+
580
+ worksheet.write(row, 0, txt)
581
+ worksheet.data_validation(row, 1,
582
+ {
583
+ :validate => 'decimal',
584
+ :criteria => 'between',
585
+ :minimum => 0.1,
586
+ :maximum => 0.5
587
+ })
588
+
589
+
590
+ #
591
+ # Example 6. Limiting input to a value in a dropdown list.
592
+ #
593
+ txt = 'Select a value from a drop down list'
594
+ row += 2
595
+ bp=1
596
+ worksheet.write(row, 0, txt)
597
+ worksheet.data_validation(row, 1,
598
+ {
599
+ :validate => 'list',
600
+ :source => ['open', 'high', 'close']
601
+ })
602
+
603
+
604
+ #
605
+ # Example 6. Limiting input to a value in a dropdown list.
606
+ #
607
+ txt = 'Select a value from a drop down list (using a cell range)'
608
+ row += 2
609
+
610
+ worksheet.write(row, 0, txt)
611
+ worksheet.data_validation(row, 1,
612
+ {
613
+ :validate => 'list',
614
+ :source => '=E4:G4'
615
+ })
616
+
617
+
618
+ #
619
+ # Example 7. Limiting input to a date in a fixed range.
620
+ #
621
+ txt = 'Enter a date between 1/1/2008 and 12/12/2008'
622
+ row += 2
623
+
624
+ worksheet.write(row, 0, txt)
625
+ worksheet.data_validation(row, 1,
626
+ {
627
+ :validate => 'date',
628
+ :criteria => 'between',
629
+ :minimum => '2008-01-01T',
630
+ :maximum => '2008-12-12T'
631
+ })
632
+
633
+
634
+ #
635
+ # Example 8. Limiting input to a time in a fixed range.
636
+ #
637
+ txt = 'Enter a time between 6:00 and 12:00'
638
+ row += 2
639
+
640
+ worksheet.write(row, 0, txt)
641
+ worksheet.data_validation(row, 1,
642
+ {
643
+ :validate => 'time',
644
+ :criteria => 'between',
645
+ :minimum => 'T06:00',
646
+ :maximum => 'T12:00'
647
+ })
648
+
649
+
650
+ #
651
+ # Example 9. Limiting input to a string greater than a fixed length.
652
+ #
653
+ txt = 'Enter a string longer than 3 characters'
654
+ row += 2
655
+
656
+ worksheet.write(row, 0, txt)
657
+ worksheet.data_validation(row, 1,
658
+ {
659
+ :validate => 'length',
660
+ :criteria => '>',
661
+ :value => 3
662
+ })
663
+
664
+
665
+ #
666
+ # Example 10. Limiting input based on a formula.
667
+ #
668
+ txt = 'Enter a value if the following is true "=AND(F5=50,G5=60)"'
669
+ row += 2
670
+
671
+ worksheet.write(row, 0, txt)
672
+ worksheet.data_validation(row, 1,
673
+ {
674
+ :validate => 'custom',
675
+ :value => '=AND(F5=50,G5=60)'
676
+ })
677
+
678
+
679
+ #
680
+ # Example 11. Displaying and modify data validation messages.
681
+ #
682
+ txt = 'Displays a message when you select the cell'
683
+ row += 2
684
+
685
+ worksheet.write(row, 0, txt)
686
+ worksheet.data_validation(row, 1,
687
+ {
688
+ :validate => 'integer',
689
+ :criteria => 'between',
690
+ :minimum => 1,
691
+ :maximum => 100,
692
+ :input_title => 'Enter an integer:',
693
+ :input_message => 'between 1 and 100'
694
+ })
695
+
696
+
697
+ #
698
+ # Example 12. Displaying and modify data validation messages.
699
+ #
700
+ txt = 'Display a custom error message when integer isn\'t between 1 and 100'
701
+ row += 2
702
+
703
+ worksheet.write(row, 0, txt)
704
+ worksheet.data_validation(row, 1,
705
+ {
706
+ :validate => 'integer',
707
+ :criteria => 'between',
708
+ :minimum => 1,
709
+ :maximum => 100,
710
+ :input_title => 'Enter an integer:',
711
+ :input_message => 'between 1 and 100',
712
+ :error_title => 'Input value is not valid!',
713
+ :error_message => 'It should be an integer between 1 and 100'
714
+ })
715
+
716
+
717
+ #
718
+ # Example 13. Displaying and modify data validation messages.
719
+ #
720
+ txt = 'Display a custom information message when integer isn\'t between 1 and 100'
721
+ row += 2
722
+
723
+ worksheet.write(row, 0, txt)
724
+ worksheet.data_validation(row, 1,
725
+ {
726
+ :validate => 'integer',
727
+ :criteria => 'between',
728
+ :minimum => 1,
729
+ :maximum => 100,
730
+ :input_title => 'Enter an integer:',
731
+ :input_message => 'between 1 and 100',
732
+ :error_title => 'Input value is not valid!',
733
+ :error_message => 'It should be an integer between 1 and 100',
734
+ :error_type => 'information'
735
+ })
736
+
737
+ workbook.close
738
+
739
+ # do assertion
740
+ compare_file("#{PERL_OUTDIR}/data_validate.xls", @file)
741
+ end
742
+
743
+ def test_merge1
744
+ workbook = WriteExcel.new(@file)
745
+ worksheet = workbook.add_worksheet
746
+
747
+ # Increase the cell size of the merged cells to highlight the formatting.
748
+ worksheet.set_column('B:D', 20)
749
+ worksheet.set_row(2, 30)
750
+
751
+ # Create a merge format
752
+ format = workbook.add_format(:center_across => 1)
753
+
754
+ # Only one cell should contain text, the others should be blank.
755
+ worksheet.write(2, 1, "Center across selection", format)
756
+ worksheet.write_blank(2, 2, format)
757
+ worksheet.write_blank(2, 3, format)
758
+
759
+ workbook.close
760
+
761
+ # do assertion
762
+ compare_file("#{PERL_OUTDIR}/merge1.xls", @file)
763
+ end
764
+
765
+ def test_merge2
766
+ workbook = WriteExcel.new(@file)
767
+ worksheet = workbook.add_worksheet
768
+
769
+ # Increase the cell size of the merged cells to highlight the formatting.
770
+ worksheet.set_column(1, 2, 30)
771
+ worksheet.set_row(2, 40)
772
+
773
+ # Create a merged format
774
+ format = workbook.add_format(
775
+ :center_across => 1,
776
+ :bold => 1,
777
+ :size => 15,
778
+ :pattern => 1,
779
+ :border => 6,
780
+ :color => 'white',
781
+ :fg_color => 'green',
782
+ :border_color => 'yellow',
783
+ :align => 'vcenter'
784
+ )
785
+
786
+ # Only one cell should contain text, the others should be blank.
787
+ worksheet.write(2, 1, "Center across selection", format)
788
+ worksheet.write_blank(2, 2, format)
789
+ workbook.close
790
+
791
+ # do assertion
792
+ compare_file("#{PERL_OUTDIR}/merge2.xls", @file)
793
+ end
794
+
795
+ def test_merge3
796
+ workbook = WriteExcel.new(@file)
797
+ worksheet = workbook.add_worksheet()
798
+
799
+ # Increase the cell size of the merged cells to highlight the formatting.
800
+ [1, 3,6,7].each { |row| worksheet.set_row(row, 30) }
801
+ worksheet.set_column('B:D', 20)
802
+
803
+ ###############################################################################
804
+ #
805
+ # Example 1: Merge cells containing a hyperlink using write_url_range()
806
+ # and the standard Excel 5+ merge property.
807
+ #
808
+ format1 = workbook.add_format(
809
+ :center_across => 1,
810
+ :border => 1,
811
+ :underline => 1,
812
+ :color => 'blue'
813
+ )
814
+
815
+ # Write the cells to be merged
816
+ worksheet.write_url_range('B2:D2', 'http://www.perl.com', format1)
817
+ worksheet.write_blank('C2', format1)
818
+ worksheet.write_blank('D2', format1)
819
+
820
+
821
+
822
+ ###############################################################################
823
+ #
824
+ # Example 2: Merge cells containing a hyperlink using merge_range().
825
+ #
826
+ format2 = workbook.add_format(
827
+ :border => 1,
828
+ :underline => 1,
829
+ :color => 'blue',
830
+ :align => 'center',
831
+ :valign => 'vcenter'
832
+ )
833
+
834
+ # Merge 3 cells
835
+ worksheet.merge_range('B4:D4', 'http://www.perl.com', format2)
836
+
837
+
838
+ # Merge 3 cells over two rows
839
+ worksheet.merge_range('B7:D8', 'http://www.perl.com', format2)
840
+
841
+ workbook.close
842
+
843
+ # do assertion
844
+ compare_file("#{PERL_OUTDIR}/merge3.xls", @file)
845
+ end
846
+
847
+ def test_merge4
848
+ # Create a new workbook and add a worksheet
849
+ workbook = WriteExcel.new(@file)
850
+ worksheet = workbook.add_worksheet
851
+
852
+ # Increase the cell size of the merged cells to highlight the formatting.
853
+ (1..11).each { |row| worksheet.set_row(row, 30) }
854
+ worksheet.set_column('B:D', 20)
855
+
856
+ ###############################################################################
857
+ #
858
+ # Example 1: Text centered vertically and horizontally
859
+ #
860
+ format1 = workbook.add_format(
861
+ :border => 6,
862
+ :bold => 1,
863
+ :color => 'red',
864
+ :valign => 'vcenter',
865
+ :align => 'center'
866
+ )
867
+
868
+ worksheet.merge_range('B2:D3', 'Vertical and horizontal', format1)
869
+
870
+
871
+ ###############################################################################
872
+ #
873
+ # Example 2: Text aligned to the top and left
874
+ #
875
+ format2 = workbook.add_format(
876
+ :border => 6,
877
+ :bold => 1,
878
+ :color => 'red',
879
+ :valign => 'top',
880
+ :align => 'left'
881
+ )
882
+
883
+ worksheet.merge_range('B5:D6', 'Aligned to the top and left', format2)
884
+
885
+ ###############################################################################
886
+ #
887
+ # Example 3: Text aligned to the bottom and right
888
+ #
889
+ format3 = workbook.add_format(
890
+ :border => 6,
891
+ :bold => 1,
892
+ :color => 'red',
893
+ :valign => 'bottom',
894
+ :align => 'right'
895
+ )
896
+
897
+ worksheet.merge_range('B8:D9', 'Aligned to the bottom and right', format3)
898
+
899
+ ###############################################################################
900
+ #
901
+ # Example 4: Text justified (i.e. wrapped) in the cell
902
+ #
903
+ format4 = workbook.add_format(
904
+ :border => 6,
905
+ :bold => 1,
906
+ :color => 'red',
907
+ :valign => 'top',
908
+ :align => 'justify'
909
+ )
910
+
911
+ worksheet.merge_range('B11:D12', 'Justified: '+'so on and '*18, format4)
912
+
913
+ workbook.close
914
+
915
+ # do assertion
916
+ compare_file("#{PERL_OUTDIR}/merge4.xls", @file)
917
+ end
918
+
919
+ def test_merge5
920
+ # Create a new workbook and add a worksheet
921
+ workbook = WriteExcel.new(@file)
922
+ worksheet = workbook.add_worksheet
923
+
924
+
925
+ # Increase the cell size of the merged cells to highlight the formatting.
926
+ (3..8).each { |col| worksheet.set_row(col, 36) }
927
+ [1, 3, 5].each { |n| worksheet.set_column(n, n, 15) }
928
+
929
+
930
+ ###############################################################################
931
+ #
932
+ # Rotation 1, letters run from top to bottom
933
+ #
934
+ format1 = workbook.add_format(
935
+ :border => 6,
936
+ :bold => 1,
937
+ :color => 'red',
938
+ :valign => 'vcentre',
939
+ :align => 'centre',
940
+ :rotation => 270
941
+ )
942
+
943
+
944
+ worksheet.merge_range('B4:B9', 'Rotation 270', format1)
945
+
946
+
947
+ ###############################################################################
948
+ #
949
+ # Rotation 2, 90° anticlockwise
950
+ #
951
+ format2 = workbook.add_format(
952
+ :border => 6,
953
+ :bold => 1,
954
+ :color => 'red',
955
+ :valign => 'vcentre',
956
+ :align => 'centre',
957
+ :rotation => 90
958
+ )
959
+
960
+
961
+ worksheet.merge_range('D4:D9', 'Rotation 90', format2)
962
+
963
+
964
+
965
+ ###############################################################################
966
+ #
967
+ # Rotation 3, 90° clockwise
968
+ #
969
+ format3 = workbook.add_format(
970
+ :border => 6,
971
+ :bold => 1,
972
+ :color => 'red',
973
+ :valign => 'vcentre',
974
+ :align => 'centre',
975
+ :rotation => -90
976
+ )
977
+
978
+
979
+ worksheet.merge_range('F4:F9', 'Rotation -90', format3)
980
+
981
+ workbook.close
982
+
983
+ # do assertion
984
+ compare_file("#{PERL_OUTDIR}/merge5.xls", @file)
985
+ end
986
+
987
+ def test_merge6
988
+ # Create a new workbook and add a worksheet
989
+ workbook = WriteExcel.new(@file)
990
+ worksheet = workbook.add_worksheet
991
+
992
+ # Increase the cell size of the merged cells to highlight the formatting.
993
+ (2..9).each { |i| worksheet.set_row(i, 36) }
994
+ worksheet.set_column('B:D', 25)
995
+
996
+ # Format for the merged cells.
997
+ format = workbook.add_format(
998
+ :border => 6,
999
+ :bold => 1,
1000
+ :color => 'red',
1001
+ :size => 20,
1002
+ :valign => 'vcentre',
1003
+ :align => 'left',
1004
+ :indent => 1
1005
+ )
1006
+
1007
+ ###############################################################################
1008
+ #
1009
+ # Write an Ascii string.
1010
+ #
1011
+
1012
+ worksheet.merge_range('B3:D4', 'ASCII: A simple string', format)
1013
+
1014
+ ###############################################################################
1015
+ #
1016
+ # Write a UTF-16 Unicode string.
1017
+ #
1018
+
1019
+ # A phrase in Cyrillic encoded as UTF-16BE.
1020
+ utf16_str = [
1021
+ '005500540046002d00310036003a0020' <<
1022
+ '042d0442043e002004440440043004370430002004' <<
1023
+ '3d043000200440044304410441043a043e043c0021'
1024
+ ].pack("H*")
1025
+
1026
+ # Note the extra parameter at the end to indicate UTF-16 encoding.
1027
+ worksheet.merge_range('B6:D7', utf16_str, format, 1)
1028
+
1029
+ ###############################################################################
1030
+ #
1031
+ # Write a UTF-8 Unicode string.
1032
+ #
1033
+
1034
+ smiley = '☺' # chr 0x263a in perl
1035
+ worksheet.merge_range('B9:D10', "UTF-8: A Unicode smiley #{smiley}", format)
1036
+
1037
+ workbook.close
1038
+
1039
+ # do assertion
1040
+ compare_file("#{PERL_OUTDIR}/merge6.xls", @file)
1041
+ end
1042
+
1043
+ def test_images
1044
+ # Create a new workbook called simple.xls and add a worksheet
1045
+ workbook = WriteExcel.new(@file)
1046
+ worksheet1 = workbook.add_worksheet('Image 1')
1047
+ worksheet2 = workbook.add_worksheet('Image 2')
1048
+ worksheet3 = workbook.add_worksheet('Image 3')
1049
+ worksheet4 = workbook.add_worksheet('Image 4')
1050
+ bp=1
1051
+
1052
+ # Insert a basic image
1053
+ worksheet1.write('A10', "Image inserted into worksheet.")
1054
+ worksheet1.insert_image('A1', File.join(TEST_DIR,'republic.png'))
1055
+
1056
+
1057
+ # Insert an image with an offset
1058
+ worksheet2.write('A10', "Image inserted with an offset.")
1059
+ worksheet2.insert_image('A1', File.join(TEST_DIR,'republic.png'), 32, 10)
1060
+
1061
+ # Insert a scaled image
1062
+ worksheet3.write('A10', "Image scaled: width x 2, height x 0.8.")
1063
+ worksheet3.insert_image('A1', File.join(TEST_DIR,'republic.png'), 0, 0, 2, 0.8)
1064
+
1065
+ # Insert an image over varied column and row sizes
1066
+ # This does not require any additional work
1067
+
1068
+ # Set the cols and row sizes
1069
+ # NOTE: you must do this before you call insert_image()
1070
+ worksheet4.set_column('A:A', 5)
1071
+ worksheet4.set_column('B:B', nil, nil, 1) # Hidden
1072
+ worksheet4.set_column('C:D', 10)
1073
+ worksheet4.set_row(0, 30)
1074
+ worksheet4.set_row(3, 5)
1075
+
1076
+ worksheet4.write('A10', "Image inserted over scaled rows and columns.")
1077
+ worksheet4.insert_image('A1', File.join(TEST_DIR,'republic.png'))
1078
+
1079
+ workbook.close
1080
+
1081
+ # do assertion
1082
+ compare_file("#{PERL_OUTDIR}/images.xls", @file)
1083
+ end
1084
+
1085
+ def test_tab_colors
1086
+ workbook = WriteExcel.new(@file)
1087
+
1088
+ worksheet1 = workbook.add_worksheet
1089
+ worksheet2 = workbook.add_worksheet
1090
+ worksheet3 = workbook.add_worksheet
1091
+ worksheet4 = workbook.add_worksheet
1092
+
1093
+ # Worsheet1 will have the default tab colour.
1094
+ worksheet2.set_tab_color('red')
1095
+ worksheet3.set_tab_color('green')
1096
+ worksheet4.set_tab_color(0x35) # Orange
1097
+
1098
+ workbook.close
1099
+
1100
+ # do assertion
1101
+ compare_file("#{PERL_OUTDIR}/tab_colors.xls", @file)
1102
+ end
1103
+
1104
+ def test_stocks
1105
+ # Create a new workbook and add a worksheet
1106
+ workbook = WriteExcel.new(@file)
1107
+ worksheet = workbook.add_worksheet
1108
+
1109
+ # Set the column width for columns 1, 2, 3 and 4
1110
+ worksheet.set_column(0, 3, 15)
1111
+
1112
+
1113
+ # Create a format for the column headings
1114
+ header = workbook.add_format
1115
+ header.set_bold
1116
+ header.set_size(12)
1117
+ header.set_color('blue')
1118
+
1119
+
1120
+ # Create a format for the stock price
1121
+ f_price = workbook.add_format
1122
+ f_price.set_align('left')
1123
+ f_price.set_num_format('$0.00')
1124
+
1125
+
1126
+ # Create a format for the stock volume
1127
+ f_volume = workbook.add_format
1128
+ f_volume.set_align('left')
1129
+ f_volume.set_num_format('#,##0')
1130
+
1131
+
1132
+ # Create a format for the price change. This is an example of a conditional
1133
+ # format. The number is formatted as a percentage. If it is positive it is
1134
+ # formatted in green, if it is negative it is formatted in red and if it is
1135
+ # zero it is formatted as the default font colour (in this case black).
1136
+ # Note: the [Green] format produces an unappealing lime green. Try
1137
+ # [Color 10] instead for a dark green.
1138
+ #
1139
+ f_change = workbook.add_format
1140
+ f_change.set_align('left')
1141
+ f_change.set_num_format('[Green]0.0%;[Red]-0.0%;0.0%')
1142
+
1143
+
1144
+ # Write out the data
1145
+ worksheet.write(0, 0, 'Company', header)
1146
+ worksheet.write(0, 1, 'Price', header)
1147
+ worksheet.write(0, 2, 'Volume', header)
1148
+ worksheet.write(0, 3, 'Change', header)
1149
+
1150
+ worksheet.write(1, 0, 'Damage Inc.' )
1151
+ worksheet.write(1, 1, 30.25, f_price) # $30.25
1152
+ worksheet.write(1, 2, 1234567, f_volume) # 1,234,567
1153
+ worksheet.write(1, 3, 0.085, f_change) # 8.5% in green
1154
+
1155
+ worksheet.write(2, 0, 'Dump Corp.' )
1156
+ worksheet.write(2, 1, 1.56, f_price) # $1.56
1157
+ worksheet.write(2, 2, 7564, f_volume) # 7,564
1158
+ worksheet.write(2, 3, -0.015, f_change) # -1.5% in red
1159
+
1160
+ worksheet.write(3, 0, 'Rev Ltd.' )
1161
+ worksheet.write(3, 1, 0.13, f_price) # $0.13
1162
+ worksheet.write(3, 2, 321, f_volume) # 321
1163
+ worksheet.write(3, 3, 0, f_change) # 0 in the font color (black)
1164
+
1165
+ workbook.close
1166
+
1167
+ # do assertion
1168
+ compare_file("#{PERL_OUTDIR}/stocks.xls", @file)
1169
+ end
1170
+
1171
+ def test_protection
1172
+ workbook = WriteExcel.new(@file)
1173
+ worksheet = workbook.add_worksheet
1174
+
1175
+ # Create some format objects
1176
+ locked = workbook.add_format(:locked => 1)
1177
+ unlocked = workbook.add_format(:locked => 0)
1178
+ hidden = workbook.add_format(:hidden => 1)
1179
+
1180
+ # Format the columns
1181
+ worksheet.set_column('A:A', 42)
1182
+ worksheet.set_selection('B3:B3')
1183
+
1184
+ # Protect the worksheet
1185
+ worksheet.protect
1186
+
1187
+ # Examples of cell locking and hiding
1188
+ worksheet.write('A1', 'Cell B1 is locked. It cannot be edited.')
1189
+ worksheet.write('B1', '=1+2', locked)
1190
+
1191
+ worksheet.write('A2', 'Cell B2 is unlocked. It can be edited.')
1192
+ worksheet.write('B2', '=1+2', unlocked)
1193
+
1194
+ worksheet.write('A3', "Cell B3 is hidden. The formula isn't visible.")
1195
+ worksheet.write('B3', '=1+2', hidden)
1196
+
1197
+ worksheet.write('A5', 'Use Menu->Tools->Protection->Unprotect Sheet')
1198
+ worksheet.write('A6', 'to remove the worksheet protection. ')
1199
+
1200
+ workbook.close
1201
+
1202
+ # do assertion
1203
+ compare_file("#{PERL_OUTDIR}/protection.xls", @file)
1204
+ end
1205
+
1206
+ def test_password_protection
1207
+ workbook = WriteExcel.new(@file)
1208
+ worksheet = workbook.add_worksheet
1209
+
1210
+ # Create some format objects
1211
+ locked = workbook.add_format(:locked => 1)
1212
+ unlocked = workbook.add_format(:locked => 0)
1213
+ hidden = workbook.add_format(:hidden => 1)
1214
+
1215
+ # Format the columns
1216
+ worksheet.set_column('A:A', 42)
1217
+ worksheet.set_selection('B3:B3')
1218
+
1219
+ # Protect the worksheet
1220
+ worksheet.protect('password')
1221
+
1222
+ # Examples of cell locking and hiding
1223
+ worksheet.write('A1', 'Cell B1 is locked. It cannot be edited.')
1224
+ worksheet.write('B1', '=1+2', locked)
1225
+
1226
+ worksheet.write('A2', 'Cell B2 is unlocked. It can be edited.')
1227
+ worksheet.write('B2', '=1+2', unlocked)
1228
+
1229
+ worksheet.write('A3', "Cell B3 is hidden. The formula isn't visible.")
1230
+ worksheet.write('B3', '=1+2', hidden)
1231
+
1232
+ worksheet.write('A5', 'Use Menu->Tools->Protection->Unprotect Sheet')
1233
+ worksheet.write('A6', 'to remove the worksheet protection. ')
1234
+ worksheet.write('A7', 'The password is "password". ')
1235
+
1236
+ workbook.close
1237
+
1238
+ # do assertion
1239
+ compare_file("#{PERL_OUTDIR}/password_protection.xls", @file)
1240
+ end
1241
+
1242
+ def test_date_time
1243
+ # Create a new workbook and add a worksheet
1244
+ workbook = WriteExcel.new(@file)
1245
+ worksheet = workbook.add_worksheet
1246
+ bold = workbook.add_format(:bold => 1)
1247
+
1248
+ # Expand the first column so that the date is visible.
1249
+ worksheet.set_column("A:B", 30)
1250
+
1251
+ # Write the column headers
1252
+ worksheet.write('A1', 'Formatted date', bold)
1253
+ worksheet.write('B1', 'Format', bold)
1254
+
1255
+ # Examples date and time formats. In the output file compare how changing
1256
+ # the format codes change the appearance of the date.
1257
+ #
1258
+ date_formats = [
1259
+ 'dd/mm/yy',
1260
+ 'mm/dd/yy',
1261
+ '',
1262
+ 'd mm yy',
1263
+ 'dd mm yy',
1264
+ '',
1265
+ 'dd m yy',
1266
+ 'dd mm yy',
1267
+ 'dd mmm yy',
1268
+ 'dd mmmm yy',
1269
+ '',
1270
+ 'dd mm y',
1271
+ 'dd mm yyy',
1272
+ 'dd mm yyyy',
1273
+ '',
1274
+ 'd mmmm yyyy',
1275
+ '',
1276
+ 'dd/mm/yy',
1277
+ 'dd/mm/yy hh:mm',
1278
+ 'dd/mm/yy hh:mm:ss',
1279
+ 'dd/mm/yy hh:mm:ss.000',
1280
+ '',
1281
+ 'hh:mm',
1282
+ 'hh:mm:ss',
1283
+ 'hh:mm:ss.000',
1284
+ ]
1285
+
1286
+ # Write the same date and time using each of the above formats. The empty
1287
+ # string formats create a blank line to make the example clearer.
1288
+ #
1289
+ row = 0
1290
+ date_formats.each do |date_format|
1291
+ row += 1
1292
+ next if date_format == ''
1293
+
1294
+ # Create a format for the date or time.
1295
+ format = workbook.add_format(
1296
+ :num_format => date_format,
1297
+ :align => 'left'
1298
+ )
1299
+
1300
+ # Write the same date using different formats.
1301
+ worksheet.write_date_time(row, 0, '2004-08-01T12:30:45.123', format)
1302
+ worksheet.write(row, 1, date_format)
1303
+ end
1304
+
1305
+ # The following is an example of an invalid date. It is written as a string instead
1306
+ # of a number. This is also Excel's default behaviour.
1307
+ #
1308
+ row += 2
1309
+ worksheet.write_date_time(row, 0, '2004-13-01T12:30:45.123')
1310
+ worksheet.write(row, 1, 'Invalid date. Written as string.', bold)
1311
+
1312
+ workbook.close
1313
+
1314
+ # do assertion
1315
+ compare_file("#{PERL_OUTDIR}/date_time.xls", @file)
1316
+ end
1317
+
1318
+ def test_diag_border
1319
+ workbook = WriteExcel.new(@file)
1320
+ worksheet = workbook.add_worksheet
1321
+
1322
+ format1 = workbook.add_format(:diag_type => 1)
1323
+ format2 = workbook.add_format(:diag_type => 2)
1324
+ format3 = workbook.add_format(:diag_type => 3)
1325
+ format4 = workbook.add_format(
1326
+ :diag_type => 3,
1327
+ :diag_border => 7,
1328
+ :diag_color => 'red'
1329
+ )
1330
+
1331
+ worksheet.write('B3', 'Text', format1)
1332
+ worksheet.write('B6', 'Text', format2)
1333
+ worksheet.write('B9', 'Text', format3)
1334
+ worksheet.write('B12', 'Text', format4)
1335
+
1336
+ workbook.close
1337
+
1338
+ # do assertion
1339
+ compare_file("#{PERL_OUTDIR}/diag_border.xls", @file)
1340
+ end
1341
+
1342
+ def test_headers
1343
+ workbook = WriteExcel.new(@file)
1344
+ preview = "Select Print Preview to see the header and footer"
1345
+
1346
+
1347
+ ######################################################################
1348
+ #
1349
+ # A simple example to start
1350
+ #
1351
+ worksheet1 = workbook.add_worksheet('Simple')
1352
+
1353
+ header1 = '&CHere is some centred text.'
1354
+
1355
+ footer1 = '&LHere is some left aligned text.'
1356
+
1357
+
1358
+ worksheet1.set_header(header1)
1359
+ worksheet1.set_footer(footer1)
1360
+
1361
+ worksheet1.set_column('A:A', 50)
1362
+ worksheet1.write('A1', preview)
1363
+
1364
+
1365
+ ######################################################################
1366
+ #
1367
+ # This is an example of some of the header/footer variables.
1368
+ #
1369
+ worksheet2 = workbook.add_worksheet('Variables')
1370
+
1371
+ header2 = '&LPage &P of &N'+
1372
+ '&CFilename: &F' +
1373
+ '&RSheetname: &A'
1374
+
1375
+ footer2 = '&LCurrent date: &D'+
1376
+ '&RCurrent time: &T'
1377
+
1378
+ worksheet2.set_header(header2)
1379
+ worksheet2.set_footer(footer2)
1380
+
1381
+
1382
+ worksheet2.set_column('A:A', 50)
1383
+ worksheet2.write('A1', preview)
1384
+ worksheet2.write('A21', "Next sheet")
1385
+ worksheet2.set_h_pagebreaks(20)
1386
+
1387
+
1388
+ ######################################################################
1389
+ #
1390
+ # This example shows how to use more than one font
1391
+ #
1392
+ worksheet3 = workbook.add_worksheet('Mixed fonts')
1393
+
1394
+ header3 = '&C' +
1395
+ '&"Courier New,Bold"Hello ' +
1396
+ '&"Arial,Italic"World'
1397
+
1398
+ footer3 = '&C' +
1399
+ '&"Symbol"e' +
1400
+ '&"Arial" = mc&X2'
1401
+
1402
+ worksheet3.set_header(header3)
1403
+ worksheet3.set_footer(footer3)
1404
+
1405
+ worksheet3.set_column('A:A', 50)
1406
+ worksheet3.write('A1', preview)
1407
+
1408
+
1409
+ ######################################################################
1410
+ #
1411
+ # Example of line wrapping
1412
+ #
1413
+ worksheet4 = workbook.add_worksheet('Word wrap')
1414
+
1415
+ header4 = "&CHeading 1\nHeading 2\nHeading 3"
1416
+
1417
+ worksheet4.set_header(header4)
1418
+
1419
+ worksheet4.set_column('A:A', 50)
1420
+ worksheet4.write('A1', preview)
1421
+
1422
+
1423
+ ######################################################################
1424
+ #
1425
+ # Example of inserting a literal ampersand &
1426
+ #
1427
+ worksheet5 = workbook.add_worksheet('Ampersand')
1428
+
1429
+ header5 = "&CCuriouser && Curiouser - Attorneys at Law"
1430
+
1431
+ worksheet5.set_header(header5)
1432
+
1433
+ worksheet5.set_column('A:A', 50)
1434
+ worksheet5.write('A1', preview)
1435
+
1436
+ workbook.close
1437
+
1438
+ # do assertion
1439
+ compare_file("#{PERL_OUTDIR}/headers.xls", @file)
1440
+ end
1441
+
1442
+ def test_demo
1443
+ workbook = WriteExcel.new(@file)
1444
+ worksheet = workbook.add_worksheet('Demo')
1445
+ worksheet2 = workbook.add_worksheet('Another sheet')
1446
+ worksheet3 = workbook.add_worksheet('And another')
1447
+
1448
+ bold = workbook.add_format(:bold => 1)
1449
+
1450
+ #######################################################################
1451
+ #
1452
+ # Write a general heading
1453
+ #
1454
+ worksheet.set_column('A:A', 36, bold)
1455
+ worksheet.set_column('B:B', 20 )
1456
+ worksheet.set_row(0, 40 )
1457
+
1458
+ heading = workbook.add_format(
1459
+ :bold => 1,
1460
+ :color => 'blue',
1461
+ :size => 16,
1462
+ :merge => 1,
1463
+ :align => 'vcenter'
1464
+ )
1465
+
1466
+ headings = ['Features of Spreadsheet::WriteExcel', '']
1467
+ worksheet.write_row('A1', headings, heading)
1468
+
1469
+
1470
+ #######################################################################
1471
+ #
1472
+ # Some text examples
1473
+ #
1474
+ text_format = workbook.add_format(
1475
+ :bold => 1,
1476
+ :italic => 1,
1477
+ :color => 'red',
1478
+ :size => 18,
1479
+ :font =>'Lucida Calligraphy'
1480
+ )
1481
+
1482
+ # A phrase in Cyrillic
1483
+ unicode = [
1484
+ "042d0442043e002004440440043004370430002004"+
1485
+ "3d043000200440044304410441043a043e043c0021"
1486
+ ].pack('H*')
1487
+
1488
+ worksheet.write('A2', "Text")
1489
+ worksheet.write('B2', "Hello Excel")
1490
+ worksheet.write('A3', "Formatted text")
1491
+ worksheet.write('B3', "Hello Excel", text_format)
1492
+ worksheet.write('A4', "Unicode text")
1493
+ worksheet.write_utf16be_string('B4', unicode)
1494
+
1495
+
1496
+ #######################################################################
1497
+ #
1498
+ # Some numeric examples
1499
+ #
1500
+ num1_format = workbook.add_format(:num_format => '$#,##0.00')
1501
+ num2_format = workbook.add_format(:num_format => ' d mmmm yyy')
1502
+
1503
+ worksheet.write('A5', "Numbers")
1504
+ worksheet.write('B5', 1234.56)
1505
+ worksheet.write('A6', "Formatted numbers")
1506
+ worksheet.write('B6', 1234.56, num1_format)
1507
+ worksheet.write('A7', "Formatted numbers")
1508
+ worksheet.write('B7', 37257, num2_format)
1509
+
1510
+
1511
+ #######################################################################
1512
+ #
1513
+ # Formulae
1514
+ #
1515
+ worksheet.set_selection('B8')
1516
+ worksheet.write('A8', 'Formulas and functions, "=SIN(PI()/4)"')
1517
+ worksheet.write('B8', '=SIN(PI()/4)')
1518
+
1519
+
1520
+ #######################################################################
1521
+ #
1522
+ # Hyperlinks
1523
+ #
1524
+ worksheet.write('A9', "Hyperlinks")
1525
+ worksheet.write('B9', 'http://www.perl.com/' )
1526
+
1527
+
1528
+ #######################################################################
1529
+ #
1530
+ # Images
1531
+ #
1532
+ worksheet.write('A10', "Images")
1533
+ worksheet.insert_image('B10', "#{TEST_DIR}/republic.png", 16, 8)
1534
+
1535
+
1536
+ #######################################################################
1537
+ #
1538
+ # Misc
1539
+ #
1540
+ worksheet.write('A18', "Page/printer setup")
1541
+ worksheet.write('A19', "Multiple worksheets")
1542
+
1543
+ workbook.close
1544
+
1545
+ # do assertion
1546
+ compare_file("#{PERL_OUTDIR}/demo.xls", @file)
1547
+ end
1548
+
1549
+ def test_unicode_cyrillic
1550
+ # Create a Russian worksheet name in utf8.
1551
+ sheet = [0x0421, 0x0442, 0x0440, 0x0430, 0x043D, 0x0438,
1552
+ 0x0446, 0x0430].pack("U*")
1553
+
1554
+ # Create a Russian string.
1555
+ str = [0x0417, 0x0434, 0x0440, 0x0430, 0x0432, 0x0441,
1556
+ 0x0442, 0x0432, 0x0443, 0x0439, 0x0020, 0x041C,
1557
+ 0x0438, 0x0440, 0x0021].pack("U*")
1558
+
1559
+ workbook = WriteExcel.new(@file)
1560
+ worksheet = workbook.add_worksheet(sheet + '1')
1561
+
1562
+ worksheet.set_column('A:A', 18)
1563
+ worksheet.write('A1', str)
1564
+
1565
+ workbook.close
1566
+
1567
+ # do assertion
1568
+ compare_file("#{PERL_OUTDIR}/unicode_cyrillic.xls", @file)
1569
+ end
1570
+
1571
+ def test_defined_name
1572
+ workbook = WriteExcel.new(@file)
1573
+ worksheet1 = workbook.add_worksheet
1574
+ worksheet2 = workbook.add_worksheet
1575
+
1576
+ workbook.define_name('Exchange_rate', '=0.96')
1577
+ workbook.define_name('Sales', '=Sheet1!$G$1:$H$10')
1578
+ workbook.define_name('Sheet2!Sales', '=Sheet2!$G$1:$G$10')
1579
+
1580
+ workbook.sheets.each do |worksheet|
1581
+ worksheet.set_column('A:A', 45)
1582
+ worksheet.write('A2', 'This worksheet contains some defined names,')
1583
+ worksheet.write('A3', 'See the Insert -> Name -> Define dialog.')
1584
+ end
1585
+
1586
+ worksheet1.write('A4', '=Exchange_rate')
1587
+
1588
+ workbook.close
1589
+
1590
+ # do assertion
1591
+ compare_file("#{PERL_OUTDIR}/defined_name.xls", @file)
1592
+ end
1593
+
1594
+ def test_chart_area
1595
+ workbook = WriteExcel.new(@file)
1596
+ worksheet = workbook.add_worksheet
1597
+ bold = workbook.add_format(:bold => 1)
1598
+
1599
+ # Add the data to the worksheet that the charts will refer to.
1600
+ headings = [ 'Category', 'Values 1', 'Values 2' ]
1601
+ data = [
1602
+ [ 2, 3, 4, 5, 6, 7 ],
1603
+ [ 1, 4, 5, 2, 1, 5 ],
1604
+ [ 3, 6, 7, 5, 4, 3 ]
1605
+ ]
1606
+
1607
+ worksheet.write('A1', headings, bold)
1608
+ worksheet.write('A2', data)
1609
+
1610
+
1611
+ ###############################################################################
1612
+ #
1613
+ # Example 1. A minimal chart.
1614
+ #
1615
+ chart1 = workbook.add_chart(:type => 'Chart::Area')
1616
+
1617
+ # Add values only. Use the default categories.
1618
+ chart1.add_series( :values => '=Sheet1!$B$2:$B$7' )
1619
+
1620
+ ###############################################################################
1621
+ #
1622
+ # Example 2. A minimal chart with user specified categories (X axis)
1623
+ # and a series name.
1624
+ #
1625
+ chart2 = workbook.add_chart(:type => 'Chart::Area')
1626
+
1627
+ # Configure the series.
1628
+ chart2.add_series(
1629
+ :categories => '=Sheet1!$A$2:$A$7',
1630
+ :values => '=Sheet1!$B$2:$B$7',
1631
+ :name => 'Test data series 1'
1632
+ )
1633
+
1634
+ ###############################################################################
1635
+ #
1636
+ # Example 3. Same as previous chart but with added title and axes labels.
1637
+ #
1638
+ chart3 = workbook.add_chart(:type => 'Chart::Area')
1639
+
1640
+ # Configure the series.
1641
+ chart3.add_series(
1642
+ :categories => '=Sheet1!$A$2:$A$7',
1643
+ :values => '=Sheet1!$B$2:$B$7',
1644
+ :name => 'Test data series 1'
1645
+ )
1646
+
1647
+ # Add some labels.
1648
+ chart3.set_title( :name => 'Results of sample analysis' )
1649
+ chart3.set_x_axis( :name => 'Sample number' )
1650
+ chart3.set_y_axis( :name => 'Sample length (cm)' )
1651
+
1652
+ ###############################################################################
1653
+ #
1654
+ # Example 4. Same as previous chart but with an added series
1655
+ #
1656
+ chart4 = workbook.add_chart(:name => 'Results Chart', :type => 'Chart::Area')
1657
+
1658
+ # Configure the series.
1659
+ chart4.add_series(
1660
+ :categories => '=Sheet1!$A$2:$A$7',
1661
+ :values => '=Sheet1!$B$2:$B$7',
1662
+ :name => 'Test data series 1'
1663
+ )
1664
+
1665
+ # Add another series.
1666
+ chart4.add_series(
1667
+ :categories => '=Sheet1!$A$2:$A$7',
1668
+ :values => '=Sheet1!$C$2:$C$7',
1669
+ :name => 'Test data series 2'
1670
+ )
1671
+
1672
+ # Add some labels.
1673
+ chart4.set_title( :name => 'Results of sample analysis' )
1674
+ chart4.set_x_axis( :name => 'Sample number' )
1675
+ chart4.set_y_axis( :name => 'Sample length (cm)' )
1676
+
1677
+ ###############################################################################
1678
+ #
1679
+ # Example 5. Same as Example 3 but as an embedded chart.
1680
+ #
1681
+ chart5 = workbook.add_chart(:type => 'Chart::Area', :embedded => 1)
1682
+
1683
+ # Configure the series.
1684
+ chart5.add_series(
1685
+ :categories => '=Sheet1!$A$2:$A$7',
1686
+ :values => '=Sheet1!$B$2:$B$7',
1687
+ :name => 'Test data series 1'
1688
+ )
1689
+
1690
+ # Add some labels.
1691
+ chart5.set_title(:name => 'Results of sample analysis' )
1692
+ chart5.set_x_axis(:name => 'Sample number')
1693
+ chart5.set_y_axis(:name => 'Sample length (cm)')
1694
+
1695
+ # Insert the chart into the main worksheet.
1696
+ worksheet.insert_chart('E2', chart5)
1697
+
1698
+ # File save
1699
+ workbook.close
1700
+
1701
+ # do assertion
1702
+ compare_file("#{PERL_OUTDIR}/chart_area.xls", @file)
1703
+ end
1704
+
1705
+ def test_chart_bar
1706
+ workbook = WriteExcel.new(@file)
1707
+ worksheet = workbook.add_worksheet
1708
+ bold = workbook.add_format(:bold => 1)
1709
+
1710
+ # Add the data to the worksheet that the charts will refer to.
1711
+ headings = [ 'Category', 'Values 1', 'Values 2' ]
1712
+ data = [
1713
+ [ 2, 3, 4, 5, 6, 7 ],
1714
+ [ 1, 4, 5, 2, 1, 5 ],
1715
+ [ 3, 6, 7, 5, 4, 3 ]
1716
+ ]
1717
+
1718
+ worksheet.write('A1', headings, bold)
1719
+ worksheet.write('A2', data)
1720
+
1721
+
1722
+ ###############################################################################
1723
+ #
1724
+ # Example 1. A minimal chart.
1725
+ #
1726
+ chart1 = workbook.add_chart(:type => 'Chart::Bar')
1727
+
1728
+ # Add values only. Use the default categories.
1729
+ chart1.add_series( :values => '=Sheet1!$B$2:$B$7' )
1730
+
1731
+ ###############################################################################
1732
+ #
1733
+ # Example 2. A minimal chart with user specified categories (X axis)
1734
+ # and a series name.
1735
+ #
1736
+ chart2 = workbook.add_chart(:type => 'Chart::Bar')
1737
+
1738
+ # Configure the series.
1739
+ chart2.add_series(
1740
+ :categories => '=Sheet1!$A$2:$A$7',
1741
+ :values => '=Sheet1!$B$2:$B$7',
1742
+ :name => 'Test data series 1'
1743
+ )
1744
+
1745
+ ###############################################################################
1746
+ #
1747
+ # Example 3. Same as previous chart but with added title and axes labels.
1748
+ #
1749
+ chart3 = workbook.add_chart(:type => 'Chart::Bar')
1750
+
1751
+ # Configure the series.
1752
+ chart3.add_series(
1753
+ :categories => '=Sheet1!$A$2:$A$7',
1754
+ :values => '=Sheet1!$B$2:$B$7',
1755
+ :name => 'Test data series 1'
1756
+ )
1757
+
1758
+ # Add some labels.
1759
+ chart3.set_title( :name => 'Results of sample analysis' )
1760
+ chart3.set_x_axis( :name => 'Sample number' )
1761
+ chart3.set_y_axis( :name => 'Sample length (cm)' )
1762
+
1763
+ ###############################################################################
1764
+ #
1765
+ # Example 4. Same as previous chart but with an added series
1766
+ #
1767
+ chart4 = workbook.add_chart(:name => 'Results Chart', :type => 'Chart::Bar')
1768
+
1769
+ # Configure the series.
1770
+ chart4.add_series(
1771
+ :categories => '=Sheet1!$A$2:$A$7',
1772
+ :values => '=Sheet1!$B$2:$B$7',
1773
+ :name => 'Test data series 1'
1774
+ )
1775
+
1776
+ # Add another series.
1777
+ chart4.add_series(
1778
+ :categories => '=Sheet1!$A$2:$A$7',
1779
+ :values => '=Sheet1!$C$2:$C$7',
1780
+ :name => 'Test data series 2'
1781
+ )
1782
+
1783
+ # Add some labels.
1784
+ chart4.set_title( :name => 'Results of sample analysis' )
1785
+ chart4.set_x_axis( :name => 'Sample number' )
1786
+ chart4.set_y_axis( :name => 'Sample length (cm)' )
1787
+
1788
+ ###############################################################################
1789
+ #
1790
+ # Example 5. Same as Example 3 but as an embedded chart.
1791
+ #
1792
+ chart5 = workbook.add_chart(:type => 'Chart::Bar', :embedded => 1)
1793
+
1794
+ # Configure the series.
1795
+ chart5.add_series(
1796
+ :categories => '=Sheet1!$A$2:$A$7',
1797
+ :values => '=Sheet1!$B$2:$B$7',
1798
+ :name => 'Test data series 1'
1799
+ )
1800
+
1801
+ # Add some labels.
1802
+ chart5.set_title(:name => 'Results of sample analysis' )
1803
+ chart5.set_x_axis(:name => 'Sample number')
1804
+ chart5.set_y_axis(:name => 'Sample length (cm)')
1805
+
1806
+ # Insert the chart into the main worksheet.
1807
+ worksheet.insert_chart('E2', chart5)
1808
+
1809
+ # File save
1810
+ workbook.close
1811
+
1812
+ # do assertion
1813
+ compare_file("#{PERL_OUTDIR}/chart_bar.xls", @file)
1814
+ end
1815
+
1816
+ def test_chart_column
1817
+ workbook = WriteExcel.new(@file)
1818
+ worksheet = workbook.add_worksheet
1819
+ bold = workbook.add_format(:bold => 1)
1820
+
1821
+ # Add the data to the worksheet that the charts will refer to.
1822
+ headings = [ 'Category', 'Values 1', 'Values 2' ]
1823
+ data = [
1824
+ [ 2, 3, 4, 5, 6, 7 ],
1825
+ [ 1, 4, 5, 2, 1, 5 ],
1826
+ [ 3, 6, 7, 5, 4, 3 ]
1827
+ ]
1828
+
1829
+ worksheet.write('A1', headings, bold)
1830
+ worksheet.write('A2', data)
1831
+
1832
+
1833
+ ###############################################################################
1834
+ #
1835
+ # Example 1. A minimal chart.
1836
+ #
1837
+ chart1 = workbook.add_chart(:type => 'Chart::Column')
1838
+
1839
+ # Add values only. Use the default categories.
1840
+ chart1.add_series( :values => '=Sheet1!$B$2:$B$7' )
1841
+
1842
+ ###############################################################################
1843
+ #
1844
+ # Example 2. A minimal chart with user specified categories (X axis)
1845
+ # and a series name.
1846
+ #
1847
+ chart2 = workbook.add_chart(:type => 'Chart::Column')
1848
+
1849
+ # Configure the series.
1850
+ chart2.add_series(
1851
+ :categories => '=Sheet1!$A$2:$A$7',
1852
+ :values => '=Sheet1!$B$2:$B$7',
1853
+ :name => 'Test data series 1'
1854
+ )
1855
+
1856
+ ###############################################################################
1857
+ #
1858
+ # Example 3. Same as previous chart but with added title and axes labels.
1859
+ #
1860
+ chart3 = workbook.add_chart(:type => 'Chart::Column')
1861
+
1862
+ # Configure the series.
1863
+ chart3.add_series(
1864
+ :categories => '=Sheet1!$A$2:$A$7',
1865
+ :values => '=Sheet1!$B$2:$B$7',
1866
+ :name => 'Test data series 1'
1867
+ )
1868
+
1869
+ # Add some labels.
1870
+ chart3.set_title( :name => 'Results of sample analysis' )
1871
+ chart3.set_x_axis( :name => 'Sample number' )
1872
+ chart3.set_y_axis( :name => 'Sample length (cm)' )
1873
+
1874
+ ###############################################################################
1875
+ #
1876
+ # Example 4. Same as previous chart but with an added series
1877
+ #
1878
+ chart4 = workbook.add_chart(:name => 'Results Chart', :type => 'Chart::Column')
1879
+
1880
+ # Configure the series.
1881
+ chart4.add_series(
1882
+ :categories => '=Sheet1!$A$2:$A$7',
1883
+ :values => '=Sheet1!$B$2:$B$7',
1884
+ :name => 'Test data series 1'
1885
+ )
1886
+
1887
+ # Add another series.
1888
+ chart4.add_series(
1889
+ :categories => '=Sheet1!$A$2:$A$7',
1890
+ :values => '=Sheet1!$C$2:$C$7',
1891
+ :name => 'Test data series 2'
1892
+ )
1893
+
1894
+ # Add some labels.
1895
+ chart4.set_title( :name => 'Results of sample analysis' )
1896
+ chart4.set_x_axis( :name => 'Sample number' )
1897
+ chart4.set_y_axis( :name => 'Sample length (cm)' )
1898
+
1899
+ ###############################################################################
1900
+ #
1901
+ # Example 5. Same as Example 3 but as an embedded chart.
1902
+ #
1903
+ chart5 = workbook.add_chart(:type => 'Chart::Column', :embedded => 1)
1904
+
1905
+ # Configure the series.
1906
+ chart5.add_series(
1907
+ :categories => '=Sheet1!$A$2:$A$7',
1908
+ :values => '=Sheet1!$B$2:$B$7',
1909
+ :name => 'Test data series 1'
1910
+ )
1911
+
1912
+ # Add some labels.
1913
+ chart5.set_title(:name => 'Results of sample analysis' )
1914
+ chart5.set_x_axis(:name => 'Sample number')
1915
+ chart5.set_y_axis(:name => 'Sample length (cm)')
1916
+
1917
+ # Insert the chart into the main worksheet.
1918
+ worksheet.insert_chart('E2', chart5)
1919
+
1920
+ # File save
1921
+ workbook.close
1922
+
1923
+ # do assertion
1924
+ compare_file("#{PERL_OUTDIR}/chart_column.xls", @file)
1925
+ end
1926
+
1927
+ def test_chart_line
1928
+ workbook = WriteExcel.new(@file)
1929
+ worksheet = workbook.add_worksheet
1930
+ bold = workbook.add_format(:bold => 1)
1931
+
1932
+ # Add the data to the worksheet that the charts will refer to.
1933
+ headings = [ 'Category', 'Values 1', 'Values 2' ]
1934
+ data = [
1935
+ [ 2, 3, 4, 5, 6, 7 ],
1936
+ [ 1, 4, 5, 2, 1, 5 ],
1937
+ [ 3, 6, 7, 5, 4, 3 ]
1938
+ ]
1939
+
1940
+ worksheet.write('A1', headings, bold)
1941
+ worksheet.write('A2', data)
1942
+
1943
+
1944
+ ###############################################################################
1945
+ #
1946
+ # Example 1. A minimal chart.
1947
+ #
1948
+ chart1 = workbook.add_chart(:type => 'Chart::Line')
1949
+
1950
+ # Add values only. Use the default categories.
1951
+ chart1.add_series( :values => '=Sheet1!$B$2:$B$7' )
1952
+
1953
+ ###############################################################################
1954
+ #
1955
+ # Example 2. A minimal chart with user specified categories (X axis)
1956
+ # and a series name.
1957
+ #
1958
+ chart2 = workbook.add_chart(:type => 'Chart::Line')
1959
+
1960
+ # Configure the series.
1961
+ chart2.add_series(
1962
+ :categories => '=Sheet1!$A$2:$A$7',
1963
+ :values => '=Sheet1!$B$2:$B$7',
1964
+ :name => 'Test data series 1'
1965
+ )
1966
+
1967
+ ###############################################################################
1968
+ #
1969
+ # Example 3. Same as previous chart but with added title and axes labels.
1970
+ #
1971
+ chart3 = workbook.add_chart(:type => 'Chart::Line')
1972
+
1973
+ # Configure the series.
1974
+ chart3.add_series(
1975
+ :categories => '=Sheet1!$A$2:$A$7',
1976
+ :values => '=Sheet1!$B$2:$B$7',
1977
+ :name => 'Test data series 1'
1978
+ )
1979
+
1980
+ # Add some labels.
1981
+ chart3.set_title( :name => 'Results of sample analysis' )
1982
+ chart3.set_x_axis( :name => 'Sample number' )
1983
+ chart3.set_y_axis( :name => 'Sample length (cm)' )
1984
+
1985
+ ###############################################################################
1986
+ #
1987
+ # Example 4. Same as previous chart but with an added series
1988
+ #
1989
+ chart4 = workbook.add_chart(:name => 'Results Chart', :type => 'Chart::Line')
1990
+
1991
+ # Configure the series.
1992
+ chart4.add_series(
1993
+ :categories => '=Sheet1!$A$2:$A$7',
1994
+ :values => '=Sheet1!$B$2:$B$7',
1995
+ :name => 'Test data series 1'
1996
+ )
1997
+
1998
+ # Add another series.
1999
+ chart4.add_series(
2000
+ :categories => '=Sheet1!$A$2:$A$7',
2001
+ :values => '=Sheet1!$C$2:$C$7',
2002
+ :name => 'Test data series 2'
2003
+ )
2004
+
2005
+ # Add some labels.
2006
+ chart4.set_title( :name => 'Results of sample analysis' )
2007
+ chart4.set_x_axis( :name => 'Sample number' )
2008
+ chart4.set_y_axis( :name => 'Sample length (cm)' )
2009
+
2010
+ ###############################################################################
2011
+ #
2012
+ # Example 5. Same as Example 3 but as an embedded chart.
2013
+ #
2014
+ chart5 = workbook.add_chart(:type => 'Chart::Line', :embedded => 1)
2015
+
2016
+ # Configure the series.
2017
+ chart5.add_series(
2018
+ :categories => '=Sheet1!$A$2:$A$7',
2019
+ :values => '=Sheet1!$B$2:$B$7',
2020
+ :name => 'Test data series 1'
2021
+ )
2022
+
2023
+ # Add some labels.
2024
+ chart5.set_title(:name => 'Results of sample analysis' )
2025
+ chart5.set_x_axis(:name => 'Sample number')
2026
+ chart5.set_y_axis(:name => 'Sample length (cm)')
2027
+
2028
+ # Insert the chart into the main worksheet.
2029
+ worksheet.insert_chart('E2', chart5)
2030
+
2031
+ # File save
2032
+ workbook.close
2033
+
2034
+ # do assertion
2035
+ compare_file("#{PERL_OUTDIR}/chart_line.xls", @file)
2036
+ end
2037
+
2038
+ def test_chess
2039
+ workbook = WriteExcel.new(@file)
2040
+ worksheet = workbook.add_worksheet()
2041
+
2042
+ # Some row and column formatting
2043
+ worksheet.set_column('B:I', 10)
2044
+
2045
+ (1..8).each { |i| worksheet.set_row(i, 50) }
2046
+
2047
+ # Define the property hashes
2048
+ #
2049
+ black = {
2050
+ 'fg_color' => 'black',
2051
+ 'pattern' => 1,
2052
+ }
2053
+
2054
+ top = { 'top' => 6 }
2055
+ bottom = { 'bottom' => 6 }
2056
+ left = { 'left' => 6 }
2057
+ right = { 'right' => 6 }
2058
+
2059
+ # Define the formats
2060
+ #
2061
+ format01 = workbook.add_format(top.merge(left))
2062
+ format02 = workbook.add_format(top.merge(black))
2063
+ format03 = workbook.add_format(top)
2064
+ format04 = workbook.add_format(top.merge(right).merge(black))
2065
+
2066
+ format05 = workbook.add_format(left)
2067
+ format06 = workbook.add_format(black)
2068
+ format07 = workbook.add_format
2069
+ format08 = workbook.add_format(right.merge(black))
2070
+ format09 = workbook.add_format(right)
2071
+ format10 = workbook.add_format(left.merge(black))
2072
+
2073
+ format11 = workbook.add_format(bottom.merge(left).merge(black))
2074
+ format12 = workbook.add_format(bottom)
2075
+ format13 = workbook.add_format(bottom.merge(black))
2076
+ format14 = workbook.add_format(bottom.merge(right))
2077
+
2078
+
2079
+ # Draw the pattern
2080
+ worksheet.write('B2', '', format01)
2081
+ worksheet.write('C2', '', format02)
2082
+ worksheet.write('D2', '', format03)
2083
+ worksheet.write('E2', '', format02)
2084
+ worksheet.write('F2', '', format03)
2085
+ worksheet.write('G2', '', format02)
2086
+ worksheet.write('H2', '', format03)
2087
+ worksheet.write('I2', '', format04)
2088
+
2089
+ worksheet.write('B3', '', format10)
2090
+ worksheet.write('C3', '', format07)
2091
+ worksheet.write('D3', '', format06)
2092
+ worksheet.write('E3', '', format07)
2093
+ worksheet.write('F3', '', format06)
2094
+ worksheet.write('G3', '', format07)
2095
+ worksheet.write('H3', '', format06)
2096
+ worksheet.write('I3', '', format09)
2097
+
2098
+ worksheet.write('B4', '', format05)
2099
+ worksheet.write('C4', '', format06)
2100
+ worksheet.write('D4', '', format07)
2101
+ worksheet.write('E4', '', format06)
2102
+ worksheet.write('F4', '', format07)
2103
+ worksheet.write('G4', '', format06)
2104
+ worksheet.write('H4', '', format07)
2105
+ worksheet.write('I4', '', format08)
2106
+
2107
+ worksheet.write('B5', '', format10)
2108
+ worksheet.write('C5', '', format07)
2109
+ worksheet.write('D5', '', format06)
2110
+ worksheet.write('E5', '', format07)
2111
+ worksheet.write('F5', '', format06)
2112
+ worksheet.write('G5', '', format07)
2113
+ worksheet.write('H5', '', format06)
2114
+ worksheet.write('I5', '', format09)
2115
+
2116
+ worksheet.write('B6', '', format05)
2117
+ worksheet.write('C6', '', format06)
2118
+ worksheet.write('D6', '', format07)
2119
+ worksheet.write('E6', '', format06)
2120
+ worksheet.write('F6', '', format07)
2121
+ worksheet.write('G6', '', format06)
2122
+ worksheet.write('H6', '', format07)
2123
+ worksheet.write('I6', '', format08)
2124
+
2125
+ worksheet.write('B7', '', format10)
2126
+ worksheet.write('C7', '', format07)
2127
+ worksheet.write('D7', '', format06)
2128
+ worksheet.write('E7', '', format07)
2129
+ worksheet.write('F7', '', format06)
2130
+ worksheet.write('G7', '', format07)
2131
+ worksheet.write('H7', '', format06)
2132
+ worksheet.write('I7', '', format09)
2133
+
2134
+ worksheet.write('B8', '', format05)
2135
+ worksheet.write('C8', '', format06)
2136
+ worksheet.write('D8', '', format07)
2137
+ worksheet.write('E8', '', format06)
2138
+ worksheet.write('F8', '', format07)
2139
+ worksheet.write('G8', '', format06)
2140
+ worksheet.write('H8', '', format07)
2141
+ worksheet.write('I8', '', format08)
2142
+
2143
+ worksheet.write('B9', '', format11)
2144
+ worksheet.write('C9', '', format12)
2145
+ worksheet.write('D9', '', format13)
2146
+ worksheet.write('E9', '', format12)
2147
+ worksheet.write('F9', '', format13)
2148
+ worksheet.write('G9', '', format12)
2149
+ worksheet.write('H9', '', format13)
2150
+ worksheet.write('I9', '', format14)
2151
+
2152
+ workbook.close
2153
+
2154
+ # do assertion
2155
+ compare_file("#{PERL_OUTDIR}/chess.xls", @file)
2156
+ end
2157
+
2158
+ def test_colors
2159
+ workbook = WriteExcel.new(@file)
2160
+
2161
+ # Some common formats
2162
+ center = workbook.add_format(:align => 'center')
2163
+ heading = workbook.add_format(:align => 'center', :bold => 1)
2164
+
2165
+ ######################################################################
2166
+ #
2167
+ # Demonstrate the named colors.
2168
+ #
2169
+
2170
+ order = [
2171
+ 0x21,
2172
+ 0x0B,
2173
+ 0x35,
2174
+ 0x11,
2175
+ 0x16,
2176
+ 0x12,
2177
+ 0x0D,
2178
+ 0x10,
2179
+ 0x17,
2180
+ 0x09,
2181
+ 0x0C,
2182
+ 0x0F,
2183
+ 0x0E,
2184
+ 0x14,
2185
+ 0x08,
2186
+ 0x0A
2187
+ ]
2188
+
2189
+ colors = {
2190
+ 0x08 => 'black',
2191
+ 0x0C => 'blue',
2192
+ 0x10 => 'brown',
2193
+ 0x0F => 'cyan',
2194
+ 0x17 => 'gray',
2195
+ 0x11 => 'green',
2196
+ 0x0B => 'lime',
2197
+ 0x0E => 'magenta',
2198
+ 0x12 => 'navy',
2199
+ 0x35 => 'orange',
2200
+ 0x21 => 'pink',
2201
+ 0x14 => 'purple',
2202
+ 0x0A => 'red',
2203
+ 0x16 => 'silver',
2204
+ 0x09 => 'white',
2205
+ 0x0D => 'yellow',
2206
+ }
2207
+
2208
+ worksheet1 = workbook.add_worksheet('Named colors')
2209
+
2210
+ worksheet1.set_column(0, 3, 15)
2211
+
2212
+ worksheet1.write(0, 0, "Index", heading)
2213
+ worksheet1.write(0, 1, "Index", heading)
2214
+ worksheet1.write(0, 2, "Name", heading)
2215
+ worksheet1.write(0, 3, "Color", heading)
2216
+
2217
+ i = 1
2218
+
2219
+ # original was colors.each....
2220
+ # order unmatch between perl and ruby (of cource, it's hash!)
2221
+ # so i use order array to match perl's xls order.
2222
+ #
2223
+ order.each do |index|
2224
+ format = workbook.add_format(
2225
+ :fg_color => colors[index],
2226
+ :pattern => 1,
2227
+ :border => 1
2228
+ )
2229
+
2230
+ worksheet1.write(i + 1, 0, index, center)
2231
+ worksheet1.write(i + 1, 1, sprintf("0x%02X", index), center)
2232
+ worksheet1.write(i + 1, 2, colors[index], center)
2233
+ worksheet1.write(i + 1, 3, '', format)
2234
+ i += 1
2235
+ end
2236
+
2237
+ ######################################################################
2238
+ #
2239
+ # Demonstrate the standard Excel colors in the range 8..63.
2240
+ #
2241
+
2242
+ worksheet2 = workbook.add_worksheet('Standard colors')
2243
+
2244
+ worksheet2.set_column(0, 3, 15)
2245
+
2246
+ worksheet2.write(0, 0, "Index", heading)
2247
+ worksheet2.write(0, 1, "Index", heading)
2248
+ worksheet2.write(0, 2, "Color", heading)
2249
+ worksheet2.write(0, 3, "Name", heading)
2250
+
2251
+ (8..63).each do |i|
2252
+ format = workbook.add_format(
2253
+ :fg_color => i,
2254
+ :pattern => 1,
2255
+ :border => 1
2256
+ )
2257
+
2258
+ worksheet2.write((i - 7), 0, i, center)
2259
+ worksheet2.write((i - 7), 1, sprintf("0x%02X", i), center)
2260
+ worksheet2.write((i - 7), 2, '', format)
2261
+
2262
+ # Add the color names
2263
+ if colors.has_key?(i)
2264
+ worksheet2.write((i - 7), 3, colors[i], center)
2265
+ end
2266
+ end
2267
+
2268
+ workbook.close
2269
+
2270
+ # do assertion
2271
+ compare_file("#{PERL_OUTDIR}/colors.xls", @file)
2272
+ end
2273
+
2274
+ def test_comments0
2275
+ workbook = WriteExcel.new(@file)
2276
+ worksheet = workbook.add_worksheet
2277
+
2278
+ worksheet.write(0, 0, 'Hello a1')
2279
+ worksheet.write(0, 1, 'Hello b1')
2280
+ worksheet.write(1, 0, 'Hello a2')
2281
+ worksheet.write(1, 1, 'Hello b2')
2282
+
2283
+ worksheet.write_comment('A1', 'This is a comment a1', :author=>'arr')
2284
+ worksheet.write_comment('A2', 'This is a comment a2', :author=>'arr')
2285
+ worksheet.write_comment('B1', 'This is a comment b1', :author=>'arr')
2286
+ worksheet.write_comment('B2', 'This is a comment b2', :author=>'arr')
2287
+
2288
+ workbook.close
2289
+
2290
+ # do assertion
2291
+ compare_file("#{PERL_OUTDIR}/comments0.xls", @file)
2292
+ end
2293
+
2294
+ def test_comments1
2295
+ workbook = WriteExcel.new(@file)
2296
+ worksheet = workbook.add_worksheet
2297
+
2298
+ worksheet.write('A1', 'Hello')
2299
+ worksheet.write_comment('A1', 'This is a comment')
2300
+
2301
+ workbook.close
2302
+
2303
+ # do assertion
2304
+ compare_file("#{PERL_OUTDIR}/comments1.xls", @file)
2305
+ end
2306
+
2307
+ def test_comments2
2308
+ workbook = WriteExcel.new(@file)
2309
+ text_wrap = workbook.add_format(:text_wrap => 1, :valign => 'top')
2310
+ worksheet1 = workbook.add_worksheet
2311
+ worksheet2 = workbook.add_worksheet
2312
+ worksheet3 = workbook.add_worksheet
2313
+ worksheet4 = workbook.add_worksheet
2314
+ worksheet5 = workbook.add_worksheet
2315
+ worksheet6 = workbook.add_worksheet
2316
+ worksheet7 = workbook.add_worksheet
2317
+ worksheet8 = workbook.add_worksheet
2318
+
2319
+ # Variables that we will use in each example.
2320
+ cell_text = ''
2321
+ comment = ''
2322
+
2323
+ ###############################################################################
2324
+ #
2325
+ # Example 1. Demonstrates a simple cell comment without formatting and Unicode
2326
+ # comments encoded as UTF-16 and as UTF-8.
2327
+ #
2328
+
2329
+ # Set up some formatting.
2330
+ worksheet1.set_column('C:C', 25)
2331
+ worksheet1.set_row(2, 50)
2332
+ worksheet1.set_row(5, 50)
2333
+
2334
+ # Simple ascii string.
2335
+ cell_text = 'Hold the mouse over this cell to see the comment.'
2336
+
2337
+ comment = 'This is a comment.'
2338
+
2339
+ worksheet1.write('C3', cell_text, text_wrap)
2340
+ worksheet1.write_comment('C3', comment)
2341
+
2342
+ # UTF-16 string.
2343
+ cell_text = 'This is a UTF-16 comment.'
2344
+
2345
+ comment = [0x263a].pack("n")
2346
+
2347
+ worksheet1.write('C6', cell_text, text_wrap)
2348
+ worksheet1.write_comment('C6', comment, :encoding => 1)
2349
+
2350
+ # UTF-8 string.
2351
+ worksheet1.set_row(8, 50)
2352
+ cell_text = 'This is a UTF-8 string.'
2353
+ comment = '☺' # chr 0x263a in perl.
2354
+
2355
+ worksheet1.write('C9', cell_text, text_wrap)
2356
+ worksheet1.write_comment('C9', comment)
2357
+
2358
+ ###############################################################################
2359
+ #
2360
+ # Example 2. Demonstrates visible and hidden comments.
2361
+ #
2362
+
2363
+ # Set up some formatting.
2364
+ worksheet2.set_column('C:C', 25)
2365
+ worksheet2.set_row(2, 50)
2366
+ worksheet2.set_row(5, 50)
2367
+
2368
+
2369
+ cell_text = 'This cell comment is visible.'
2370
+
2371
+ comment = 'Hello.'
2372
+
2373
+ worksheet2.write('C3', cell_text, text_wrap)
2374
+ worksheet2.write_comment('C3', comment, :visible => 1)
2375
+
2376
+
2377
+ cell_text = "This cell comment isn't visible (the default)."
2378
+
2379
+ comment = 'Hello.'
2380
+
2381
+ worksheet2.write('C6', cell_text, text_wrap)
2382
+ worksheet2.write_comment('C6', comment)
2383
+
2384
+ ###############################################################################
2385
+ #
2386
+ # Example 3. Demonstrates visible and hidden comments set at the worksheet
2387
+ # level.
2388
+ #
2389
+
2390
+ # Set up some formatting.
2391
+ worksheet3.set_column('C:C', 25)
2392
+ worksheet3.set_row(2, 50)
2393
+ worksheet3.set_row(5, 50)
2394
+ worksheet3.set_row(8, 50)
2395
+
2396
+ # Make all comments on the worksheet visible.
2397
+ worksheet3.show_comments
2398
+
2399
+ cell_text = 'This cell comment is visible, explicitly.'
2400
+
2401
+ comment = 'Hello.'
2402
+
2403
+ worksheet3.write('C3', cell_text, text_wrap)
2404
+ worksheet3.write_comment('C3', comment, :visible => 1)
2405
+
2406
+
2407
+ cell_text = 'This cell comment is also visible because ' +
2408
+ 'we used show_comments().'
2409
+
2410
+ comment = 'Hello.'
2411
+
2412
+ worksheet3.write('C6', cell_text, text_wrap)
2413
+ worksheet3.write_comment('C6', comment)
2414
+
2415
+
2416
+ cell_text = 'However, we can still override it locally.'
2417
+
2418
+ comment = 'Hello.'
2419
+
2420
+ worksheet3.write('C9', cell_text, text_wrap)
2421
+ worksheet3.write_comment('C9', comment, :visible => 0)
2422
+
2423
+ ###############################################################################
2424
+ #
2425
+ # Example 4. Demonstrates changes to the comment box dimensions.
2426
+ #
2427
+
2428
+ # Set up some formatting.
2429
+ worksheet4.set_column('C:C', 25)
2430
+ worksheet4.set_row(2, 50)
2431
+ worksheet4.set_row(5, 50)
2432
+ worksheet4.set_row(8, 50)
2433
+ worksheet4.set_row(15, 50)
2434
+
2435
+ worksheet4.show_comments
2436
+
2437
+ cell_text = 'This cell comment is default size.'
2438
+
2439
+ comment = 'Hello.'
2440
+
2441
+ worksheet4.write('C3', cell_text, text_wrap)
2442
+ worksheet4.write_comment('C3', comment)
2443
+
2444
+
2445
+ cell_text = 'This cell comment is twice as wide.'
2446
+
2447
+ comment = 'Hello.'
2448
+
2449
+ worksheet4.write('C6', cell_text, text_wrap)
2450
+ worksheet4.write_comment('C6', comment, :x_scale => 2)
2451
+
2452
+
2453
+ cell_text = 'This cell comment is twice as high.'
2454
+
2455
+ comment = 'Hello.'
2456
+
2457
+ worksheet4.write('C9', cell_text, text_wrap)
2458
+ worksheet4.write_comment('C9', comment, :y_scale => 2)
2459
+
2460
+
2461
+ cell_text = 'This cell comment is scaled in both directions.'
2462
+
2463
+ comment = 'Hello.'
2464
+
2465
+ worksheet4.write('C16', cell_text, text_wrap)
2466
+ worksheet4.write_comment('C16', comment, :x_scale => 1.2, :y_scale => 0.8)
2467
+
2468
+
2469
+ cell_text = 'This cell comment has width and height specified in pixels.'
2470
+
2471
+ comment = 'Hello.'
2472
+
2473
+ worksheet4.write('C19', cell_text, text_wrap)
2474
+ worksheet4.write_comment('C19', comment, :width => 200, :height => 20)
2475
+
2476
+ ###############################################################################
2477
+ #
2478
+ # Example 5. Demonstrates changes to the cell comment position.
2479
+ #
2480
+
2481
+ worksheet5.set_column('C:C', 25)
2482
+ worksheet5.set_row(2, 50)
2483
+ worksheet5.set_row(5, 50)
2484
+ worksheet5.set_row(8, 50)
2485
+ worksheet5.set_row(11, 50)
2486
+
2487
+ worksheet5.show_comments
2488
+
2489
+ cell_text = 'This cell comment is in the default position.'
2490
+
2491
+ comment = 'Hello.'
2492
+
2493
+ worksheet5.write('C3', cell_text, text_wrap)
2494
+ worksheet5.write_comment('C3', comment)
2495
+
2496
+
2497
+ cell_text = 'This cell comment has been moved to another cell.'
2498
+
2499
+ comment = 'Hello.'
2500
+
2501
+ worksheet5.write('C6', cell_text, text_wrap)
2502
+ worksheet5.write_comment('C6', comment, :start_cell => 'E4')
2503
+
2504
+
2505
+ cell_text = 'This cell comment has been moved to another cell.'
2506
+
2507
+ comment = 'Hello.'
2508
+
2509
+ worksheet5.write('C9', cell_text, text_wrap)
2510
+ worksheet5.write_comment('C9', comment, :start_row => 8, :start_col => 4)
2511
+
2512
+
2513
+ cell_text = 'This cell comment has been shifted within its default cell.'
2514
+
2515
+ comment = 'Hello.'
2516
+
2517
+ worksheet5.write('C12', cell_text, text_wrap)
2518
+ worksheet5.write_comment('C12', comment, :x_offset => 30, :y_offset => 12)
2519
+
2520
+ ###############################################################################
2521
+ #
2522
+ # Example 6. Demonstrates changes to the comment background colour.
2523
+ #
2524
+
2525
+ worksheet6.set_column('C:C', 25)
2526
+ worksheet6.set_row(2, 50)
2527
+ worksheet6.set_row(5, 50)
2528
+ worksheet6.set_row(8, 50)
2529
+
2530
+ worksheet6.show_comments
2531
+
2532
+ cell_text = 'This cell comment has a different colour.'
2533
+
2534
+ comment = 'Hello.'
2535
+
2536
+ worksheet6.write('C3', cell_text, text_wrap)
2537
+ worksheet6.write_comment('C3', comment, :color => 'green')
2538
+
2539
+
2540
+ cell_text = 'This cell comment has the default colour.'
2541
+
2542
+ comment = 'Hello.'
2543
+
2544
+ worksheet6.write('C6', cell_text, text_wrap)
2545
+ worksheet6.write_comment('C6', comment)
2546
+
2547
+ cell_text = 'This cell comment has a different colour.'
2548
+
2549
+ comment = 'Hello.'
2550
+
2551
+ worksheet6.write('C9', cell_text, text_wrap)
2552
+ worksheet6.write_comment('C9', comment, :color => 0x35)
2553
+
2554
+ ###############################################################################
2555
+ #
2556
+ # Example 7. Demonstrates how to set the cell comment author.
2557
+ #
2558
+
2559
+ worksheet7.set_column('C:C', 30)
2560
+ worksheet7.set_row(2, 50)
2561
+ worksheet7.set_row(5, 50)
2562
+ worksheet7.set_row(8, 50)
2563
+ worksheet7.set_row(11, 50)
2564
+
2565
+ author = ''
2566
+ cell = 'C3'
2567
+
2568
+ cell_text = "Move the mouse over this cell and you will see 'Cell commented "+
2569
+ "by #{author}' (blank) in the status bar at the bottom"
2570
+
2571
+ comment = 'Hello.'
2572
+
2573
+ worksheet7.write(cell, cell_text, text_wrap)
2574
+ worksheet7.write_comment(cell, comment)
2575
+
2576
+ author = 'Perl'
2577
+ cell = 'C6'
2578
+ cell_text = "Move the mouse over this cell and you will see 'Cell commented " +
2579
+ "by #{author}' in the status bar at the bottom"
2580
+
2581
+ comment = 'Hello.'
2582
+
2583
+ worksheet7.write(cell, cell_text, text_wrap)
2584
+ worksheet7.write_comment(cell, comment, :author => author)
2585
+
2586
+ author = [0x20AC].pack("n") # UTF-16 Euro
2587
+ cell = 'C9'
2588
+ cell_text = "Move the mouse over this cell and you will see 'Cell commented " +
2589
+ "by Euro' in the status bar at the bottom"
2590
+
2591
+ comment = 'Hello.'
2592
+
2593
+ worksheet7.write(cell, cell_text, text_wrap)
2594
+ worksheet7.write_comment(cell, comment, :author => author,
2595
+ :author_encoding => 1)
2596
+
2597
+ # UTF-8 string.
2598
+ author = '☺' # smiley
2599
+ cell = 'C12'
2600
+ cell_text = "Move the mouse over this cell and you will see 'Cell commented " +
2601
+ "by #{author}' in the status bar at the bottom"
2602
+ comment = 'Hello.'
2603
+
2604
+ worksheet7.write(cell, cell_text, text_wrap)
2605
+ worksheet7.write_comment(cell, comment, :author => author)
2606
+
2607
+ ###############################################################################
2608
+ #
2609
+ # Example 8. Demonstrates the need to explicitly set the row height.
2610
+ #
2611
+
2612
+ # Set up some formatting.
2613
+ worksheet8.set_column('C:C', 25)
2614
+ worksheet8.set_row(2, 80)
2615
+
2616
+ worksheet8.show_comments
2617
+
2618
+ cell_text = 'The height of this row has been adjusted explicitly using ' +
2619
+ 'set_row(). The size of the comment box is adjusted ' +
2620
+ 'accordingly by WriteExcel.'
2621
+
2622
+ comment = 'Hello.'
2623
+
2624
+ worksheet8.write('C3', cell_text, text_wrap)
2625
+ worksheet8.write_comment('C3', comment)
2626
+
2627
+ cell_text = 'The height of this row has been adjusted by Excel due to the ' +
2628
+ 'text wrap property being set. Unfortunately this means that ' +
2629
+ 'the height of the row is unknown to WriteExcel at run time ' +
2630
+ "and thus the comment box is stretched as well.\n\n" +
2631
+ 'Use set_row() to specify the row height explicitly to avoid ' +
2632
+ 'this problem.'
2633
+
2634
+ comment = 'Hello.'
2635
+
2636
+ worksheet8.write('C6', cell_text, text_wrap)
2637
+ worksheet8.write_comment('C6', comment)
2638
+
2639
+ workbook.close
2640
+
2641
+ # do assertion
2642
+ compare_file("#{PERL_OUTDIR}/comments2.xls", @file)
2643
+ end
2644
+
2645
+ def test_formula_result
2646
+ workbook = WriteExcel.new(@file)
2647
+ worksheet = workbook.add_worksheet()
2648
+ format = workbook.add_format(:color => 'blue')
2649
+
2650
+ worksheet.write('A1', '=1+2')
2651
+ worksheet.write('A2', '=1+2', format, 4)
2652
+ worksheet.write('A3', '="ABC"', nil, 'DEF')
2653
+ worksheet.write('A4', '=IF(A1 > 1, TRUE, FALSE)', nil, 'TRUE')
2654
+ worksheet.write('A5', '=1/0', nil, '#DIV/0!')
2655
+
2656
+ workbook.close
2657
+
2658
+ # do assertion
2659
+ compare_file("#{PERL_OUTDIR}/formula_result.xls", @file)
2660
+ end
2661
+
2662
+ def test_indent
2663
+ workbook = WriteExcel.new(@file)
2664
+
2665
+ worksheet = workbook.add_worksheet()
2666
+ indent1 = workbook.add_format(:indent => 1)
2667
+ indent2 = workbook.add_format(:indent => 2)
2668
+
2669
+ worksheet.set_column('A:A', 40)
2670
+
2671
+ worksheet.write('A1', "This text is indented 1 level", indent1)
2672
+ worksheet.write('A2', "This text is indented 2 levels", indent2)
2673
+
2674
+ workbook.close
2675
+
2676
+ # do assertion
2677
+ compare_file("#{PERL_OUTDIR}/indent.xls", @file)
2678
+ end
2679
+
2680
+ def test_outline
2681
+ # Create a new workbook and add some worksheets
2682
+ workbook = WriteExcel.new(@file)
2683
+ worksheet1 = workbook.add_worksheet('Outlined Rows')
2684
+ worksheet2 = workbook.add_worksheet('Collapsed Rows')
2685
+ worksheet3 = workbook.add_worksheet('Outline Columns')
2686
+ worksheet4 = workbook.add_worksheet('Outline levels')
2687
+
2688
+ # Add a general format
2689
+ bold = workbook.add_format(:bold => 1)
2690
+
2691
+
2692
+
2693
+ ###############################################################################
2694
+ #
2695
+ # Example 1: Create a worksheet with outlined rows. It also includes SUBTOTAL()
2696
+ # functions so that it looks like the type of automatic outlines that are
2697
+ # generated when you use the Excel Data->SubTotals menu item.
2698
+ #
2699
+
2700
+
2701
+ # For outlines the important parameters are $hidden and $level. Rows with the
2702
+ # same $level are grouped together. The group will be collapsed if $hidden is
2703
+ # non-zero. $height and $XF are assigned default values if they are undef.
2704
+ #
2705
+ # The syntax is: set_row($row, $height, $XF, $hidden, $level, $collapsed)
2706
+ #
2707
+ worksheet1.set_row(1, nil, nil, 0, 2)
2708
+ worksheet1.set_row(2, nil, nil, 0, 2)
2709
+ worksheet1.set_row(3, nil, nil, 0, 2)
2710
+ worksheet1.set_row(4, nil, nil, 0, 2)
2711
+ worksheet1.set_row(5, nil, nil, 0, 1)
2712
+
2713
+ worksheet1.set_row(6, nil, nil, 0, 2)
2714
+ worksheet1.set_row(7, nil, nil, 0, 2)
2715
+ worksheet1.set_row(8, nil, nil, 0, 2)
2716
+ worksheet1.set_row(9, nil, nil, 0, 2)
2717
+ worksheet1.set_row(10, nil, nil, 0, 1)
2718
+
2719
+
2720
+ # Add a column format for clarity
2721
+ worksheet1.set_column('A:A', 20)
2722
+
2723
+ # Add the data, labels and formulas
2724
+ worksheet1.write('A1', 'Region', bold)
2725
+ worksheet1.write('A2', 'North')
2726
+ worksheet1.write('A3', 'North')
2727
+ worksheet1.write('A4', 'North')
2728
+ worksheet1.write('A5', 'North')
2729
+ worksheet1.write('A6', 'North Total', bold)
2730
+
2731
+ worksheet1.write('B1', 'Sales', bold)
2732
+ worksheet1.write('B2', 1000)
2733
+ worksheet1.write('B3', 1200)
2734
+ worksheet1.write('B4', 900)
2735
+ worksheet1.write('B5', 1200)
2736
+ worksheet1.write('B6', '=SUBTOTAL(9,B2:B5)', bold)
2737
+
2738
+ worksheet1.write('A7', 'South')
2739
+ worksheet1.write('A8', 'South')
2740
+ worksheet1.write('A9', 'South')
2741
+ worksheet1.write('A10', 'South')
2742
+ worksheet1.write('A11', 'South Total', bold)
2743
+
2744
+ worksheet1.write('B7', 400)
2745
+ worksheet1.write('B8', 600)
2746
+ worksheet1.write('B9', 500)
2747
+ worksheet1.write('B10', 600)
2748
+ worksheet1.write('B11', '=SUBTOTAL(9,B7:B10)', bold)
2749
+
2750
+ worksheet1.write('A12', 'Grand Total', bold)
2751
+ worksheet1.write('B12', '=SUBTOTAL(9,B2:B10)', bold)
2752
+
2753
+
2754
+ ###############################################################################
2755
+ #
2756
+ # Example 2: Create a worksheet with outlined rows. This is the same as the
2757
+ # previous example except that the rows are collapsed.
2758
+ # Note: We need to indicate the row that contains the collapsed symbol '+'
2759
+ # with the optional parameter, $collapsed.
2760
+
2761
+ # The group will be collapsed if $hidden is non-zero.
2762
+ # The syntax is: set_row($row, $height, $XF, $hidden, $level, $collapsed)
2763
+ #
2764
+ worksheet2.set_row(1, nil, nil, 1, 2)
2765
+ worksheet2.set_row(2, nil, nil, 1, 2)
2766
+ worksheet2.set_row(3, nil, nil, 1, 2)
2767
+ worksheet2.set_row(4, nil, nil, 1, 2)
2768
+ worksheet2.set_row(5, nil, nil, 1, 1)
2769
+
2770
+ worksheet2.set_row(6, nil, nil, 1, 2)
2771
+ worksheet2.set_row(7, nil, nil, 1, 2)
2772
+ worksheet2.set_row(8, nil, nil, 1, 2)
2773
+ worksheet2.set_row(9, nil, nil, 1, 2)
2774
+ worksheet2.set_row(10, nil, nil, 1, 1)
2775
+ worksheet2.set_row(11, nil, nil, 0, 0, 1)
2776
+
2777
+
2778
+ # Add a column format for clarity
2779
+ worksheet2.set_column('A:A', 20)
2780
+
2781
+ # Add the data, labels and formulas
2782
+ worksheet2.write('A1', 'Region', bold)
2783
+ worksheet2.write('A2', 'North')
2784
+ worksheet2.write('A3', 'North')
2785
+ worksheet2.write('A4', 'North')
2786
+ worksheet2.write('A5', 'North')
2787
+ worksheet2.write('A6', 'North Total', bold)
2788
+
2789
+ worksheet2.write('B1', 'Sales', bold)
2790
+ worksheet2.write('B2', 1000)
2791
+ worksheet2.write('B3', 1200)
2792
+ worksheet2.write('B4', 900)
2793
+ worksheet2.write('B5', 1200)
2794
+ worksheet2.write('B6', '=SUBTOTAL(9,B2:B5)', bold)
2795
+
2796
+ worksheet2.write('A7', 'South')
2797
+ worksheet2.write('A8', 'South')
2798
+ worksheet2.write('A9', 'South')
2799
+ worksheet2.write('A10', 'South')
2800
+ worksheet2.write('A11', 'South Total', bold)
2801
+
2802
+ worksheet2.write('B7', 400)
2803
+ worksheet2.write('B8', 600)
2804
+ worksheet2.write('B9', 500)
2805
+ worksheet2.write('B10', 600)
2806
+ worksheet2.write('B11', '=SUBTOTAL(9,B7:B10)', bold)
2807
+
2808
+ worksheet2.write('A12', 'Grand Total', bold)
2809
+ worksheet2.write('B12', '=SUBTOTAL(9,B2:B10)', bold)
2810
+
2811
+
2812
+
2813
+ ###############################################################################
2814
+ #
2815
+ # Example 3: Create a worksheet with outlined columns.
2816
+ #
2817
+ data = [
2818
+ ['Month', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', ' Total'],
2819
+ ['North', 50, 20, 15, 25, 65, 80, '=SUM(B2:G2)'],
2820
+ ['South', 10, 20, 30, 50, 50, 50, '=SUM(B3:G3)'],
2821
+ ['East', 45, 75, 50, 15, 75, 100, '=SUM(B4:G4)'],
2822
+ ['West', 15, 15, 55, 35, 20, 50, '=SUM(B5:G6)']
2823
+ ]
2824
+
2825
+ # Add bold format to the first row
2826
+ worksheet3.set_row(0, nil, bold)
2827
+
2828
+ # Syntax: set_column(col1, col2, width, XF, hidden, level, collapsed)
2829
+ worksheet3.set_column('A:A', 10, bold )
2830
+ worksheet3.set_column('B:G', 5, nil, 0, 1)
2831
+ worksheet3.set_column('H:H', 10)
2832
+
2833
+ # Write the data and a formula
2834
+ worksheet3.write_col('A1', data)
2835
+ worksheet3.write('H6', '=SUM(H2:H5)', bold)
2836
+
2837
+
2838
+
2839
+ ###############################################################################
2840
+ #
2841
+ # Example 4: Show all possible outline levels.
2842
+ #
2843
+ levels = [
2844
+ "Level 1", "Level 2", "Level 3", "Level 4",
2845
+ "Level 5", "Level 6", "Level 7", "Level 6",
2846
+ "Level 5", "Level 4", "Level 3", "Level 2", "Level 1"
2847
+ ]
2848
+
2849
+ worksheet4.write_col('A1', levels)
2850
+
2851
+ worksheet4.set_row(0, nil, nil, nil, 1)
2852
+ worksheet4.set_row(1, nil, nil, nil, 2)
2853
+ worksheet4.set_row(2, nil, nil, nil, 3)
2854
+ worksheet4.set_row(3, nil, nil, nil, 4)
2855
+ worksheet4.set_row(4, nil, nil, nil, 5)
2856
+ worksheet4.set_row(5, nil, nil, nil, 6)
2857
+ worksheet4.set_row(6, nil, nil, nil, 7)
2858
+ worksheet4.set_row(7, nil, nil, nil, 6)
2859
+ worksheet4.set_row(8, nil, nil, nil, 5)
2860
+ worksheet4.set_row(9, nil, nil, nil, 4)
2861
+ worksheet4.set_row(10, nil, nil, nil, 3)
2862
+ worksheet4.set_row(11, nil, nil, nil, 2)
2863
+ worksheet4.set_row(12, nil, nil, nil, 1)
2864
+
2865
+ workbook.close
2866
+
2867
+ # do assertion
2868
+ compare_file("#{PERL_OUTDIR}/outline.xls", @file)
2869
+ end
2870
+
2871
+ def test_outline_collapsed
2872
+ # Create a new workbook and add some worksheets
2873
+ workbook = WriteExcel.new(@file)
2874
+ worksheet1 = workbook.add_worksheet('Outlined Rows')
2875
+ worksheet2 = workbook.add_worksheet('Collapsed Rows 1')
2876
+ worksheet3 = workbook.add_worksheet('Collapsed Rows 2')
2877
+ worksheet4 = workbook.add_worksheet('Collapsed Rows 3')
2878
+ worksheet5 = workbook.add_worksheet('Outline Columns')
2879
+ worksheet6 = workbook.add_worksheet('Collapsed Columns')
2880
+
2881
+ # Add a general format
2882
+ bold = workbook.add_format(:bold => 1)
2883
+
2884
+ #
2885
+ # This function will generate the same data and sub-totals on each worksheet.
2886
+ #
2887
+ def create_sub_totals(worksheet, bold)
2888
+ # Add a column format for clarity
2889
+ worksheet.set_column('A:A', 20)
2890
+
2891
+ # Add the data, labels and formulas
2892
+ worksheet.write('A1', 'Region', bold)
2893
+ worksheet.write('A2', 'North')
2894
+ worksheet.write('A3', 'North')
2895
+ worksheet.write('A4', 'North')
2896
+ worksheet.write('A5', 'North')
2897
+ worksheet.write('A6', 'North Total', bold)
2898
+
2899
+ worksheet.write('B1', 'Sales', bold)
2900
+ worksheet.write('B2', 1000)
2901
+ worksheet.write('B3', 1200)
2902
+ worksheet.write('B4', 900)
2903
+ worksheet.write('B5', 1200)
2904
+ worksheet.write('B6', '=SUBTOTAL(9,B2:B5)', bold)
2905
+
2906
+ worksheet.write('A7', 'South')
2907
+ worksheet.write('A8', 'South')
2908
+ worksheet.write('A9', 'South')
2909
+ worksheet.write('A10', 'South')
2910
+ worksheet.write('A11', 'South Total', bold)
2911
+
2912
+ worksheet.write('B7', 400)
2913
+ worksheet.write('B8', 600)
2914
+ worksheet.write('B9', 500)
2915
+ worksheet.write('B10', 600)
2916
+ worksheet.write('B11', '=SUBTOTAL(9,B7:B10)', bold)
2917
+
2918
+ worksheet.write('A12', 'Grand Total', bold)
2919
+ worksheet.write('B12', '=SUBTOTAL(9,B2:B10)', bold)
2920
+
2921
+ end
2922
+
2923
+
2924
+ ###############################################################################
2925
+ #
2926
+ # Example 1: Create a worksheet with outlined rows. It also includes SUBTOTAL()
2927
+ # functions so that it looks like the type of automatic outlines that are
2928
+ # generated when you use the Excel Data.SubTotals menu item.
2929
+ #
2930
+
2931
+ # The syntax is: set_row(row, height, XF, hidden, level, collapsed)
2932
+ worksheet1.set_row(1, nil, nil, 0, 2)
2933
+ worksheet1.set_row(2, nil, nil, 0, 2)
2934
+ worksheet1.set_row(3, nil, nil, 0, 2)
2935
+ worksheet1.set_row(4, nil, nil, 0, 2)
2936
+ worksheet1.set_row(5, nil, nil, 0, 1)
2937
+
2938
+ worksheet1.set_row(6, nil, nil, 0, 2)
2939
+ worksheet1.set_row(7, nil, nil, 0, 2)
2940
+ worksheet1.set_row(8, nil, nil, 0, 2)
2941
+ worksheet1.set_row(9, nil, nil, 0, 2)
2942
+ worksheet1.set_row(10, nil, nil, 0, 1)
2943
+
2944
+ # Write the sub-total data that is common to the row examples.
2945
+ create_sub_totals(worksheet1, bold)
2946
+
2947
+
2948
+ ###############################################################################
2949
+ #
2950
+ # Example 2: Create a worksheet with collapsed outlined rows.
2951
+ # This is the same as the example 1 except that the all rows are collapsed.
2952
+ # Note: We need to indicate the row that contains the collapsed symbol '+' with
2953
+ # the optional parameter, collapsed.
2954
+
2955
+ worksheet2.set_row(1, nil, nil, 1, 2)
2956
+ worksheet2.set_row(2, nil, nil, 1, 2)
2957
+ worksheet2.set_row(3, nil, nil, 1, 2)
2958
+ worksheet2.set_row(4, nil, nil, 1, 2)
2959
+ worksheet2.set_row(5, nil, nil, 1, 1)
2960
+
2961
+ worksheet2.set_row(6, nil, nil, 1, 2)
2962
+ worksheet2.set_row(7, nil, nil, 1, 2)
2963
+ worksheet2.set_row(8, nil, nil, 1, 2)
2964
+ worksheet2.set_row(9, nil, nil, 1, 2)
2965
+ worksheet2.set_row(10, nil, nil, 1, 1)
2966
+
2967
+ worksheet2.set_row(11, nil, nil, 0, 0, 1)
2968
+
2969
+ # Write the sub-total data that is common to the row examples.
2970
+ create_sub_totals(worksheet2, bold)
2971
+
2972
+
2973
+ ###############################################################################
2974
+ #
2975
+ # Example 3: Create a worksheet with collapsed outlined rows.
2976
+ # Same as the example 1 except that the two sub-totals are collapsed.
2977
+
2978
+ worksheet3.set_row(1, nil, nil, 1, 2)
2979
+ worksheet3.set_row(2, nil, nil, 1, 2)
2980
+ worksheet3.set_row(3, nil, nil, 1, 2)
2981
+ worksheet3.set_row(4, nil, nil, 1, 2)
2982
+ worksheet3.set_row(5, nil, nil, 0, 1, 1)
2983
+
2984
+ worksheet3.set_row(6, nil, nil, 1, 2)
2985
+ worksheet3.set_row(7, nil, nil, 1, 2)
2986
+ worksheet3.set_row(8, nil, nil, 1, 2)
2987
+ worksheet3.set_row(9, nil, nil, 1, 2)
2988
+ worksheet3.set_row(10, nil, nil, 0, 1, 1)
2989
+
2990
+
2991
+ # Write the sub-total data that is common to the row examples.
2992
+ create_sub_totals(worksheet3, bold)
2993
+
2994
+
2995
+ ###############################################################################
2996
+ #
2997
+ # Example 4: Create a worksheet with outlined rows.
2998
+ # Same as the example 1 except that the two sub-totals are collapsed.
2999
+
3000
+ worksheet4.set_row(1, nil, nil, 1, 2)
3001
+ worksheet4.set_row(2, nil, nil, 1, 2)
3002
+ worksheet4.set_row(3, nil, nil, 1, 2)
3003
+ worksheet4.set_row(4, nil, nil, 1, 2)
3004
+ worksheet4.set_row(5, nil, nil, 1, 1, 1)
3005
+
3006
+ worksheet4.set_row(6, nil, nil, 1, 2)
3007
+ worksheet4.set_row(7, nil, nil, 1, 2)
3008
+ worksheet4.set_row(8, nil, nil, 1, 2)
3009
+ worksheet4.set_row(9, nil, nil, 1, 2)
3010
+ worksheet4.set_row(10, nil, nil, 1, 1, 1)
3011
+
3012
+ worksheet4.set_row(11, nil, nil, 0, 0, 1)
3013
+
3014
+ # Write the sub-total data that is common to the row examples.
3015
+ create_sub_totals(worksheet4, bold)
3016
+
3017
+
3018
+
3019
+ ###############################################################################
3020
+ #
3021
+ # Example 5: Create a worksheet with outlined columns.
3022
+ #
3023
+ data = [
3024
+ ['Month', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',' Total'],
3025
+ ['North', 50, 20, 15, 25, 65, 80, '=SUM(B2:G2)'],
3026
+ ['South', 10, 20, 30, 50, 50, 50, '=SUM(B3:G3)'],
3027
+ ['East', 45, 75, 50, 15, 75, 100, '=SUM(B4:G4)'],
3028
+ ['West', 15, 15, 55, 35, 20, 50, '=SUM(B5:G6)']
3029
+ ]
3030
+
3031
+ # Add bold format to the first row
3032
+ worksheet5.set_row(0, nil, bold)
3033
+
3034
+ # Syntax: set_column(col1, col2, width, XF, hidden, level, collapsed)
3035
+ worksheet5.set_column('A:A', 10, bold )
3036
+ worksheet5.set_column('B:G', 5, nil, 0, 1)
3037
+ worksheet5.set_column('H:H', 10 )
3038
+
3039
+ # Write the data and a formula
3040
+ worksheet5.write_col('A1', data)
3041
+ worksheet5.write('H6', '=SUM(H2:H5)', bold)
3042
+
3043
+
3044
+ ###############################################################################
3045
+ #
3046
+ # Example 6: Create a worksheet with collapsed outlined columns.
3047
+ # This is the same as the previous example except collapsed columns.
3048
+
3049
+ # Add bold format to the first row
3050
+ worksheet6.set_row(0, nil, bold)
3051
+
3052
+ # Syntax: set_column(col1, col2, width, XF, hidden, level, collapsed)
3053
+ worksheet6.set_column('A:A', 10, bold )
3054
+ worksheet6.set_column('B:G', 5, nil, 1, 1 )
3055
+ worksheet6.set_column('H:H', 10, nil, 0, 0, 1)
3056
+
3057
+ # Write the data and a formula
3058
+ worksheet6.write_col('A1', data)
3059
+ worksheet6.write('H6', '=SUM(H2:H5)', bold)
3060
+
3061
+ workbook.close
3062
+
3063
+ # do assertion
3064
+ compare_file("#{PERL_OUTDIR}/outline_collapsed.xls", @file)
3065
+ end
3066
+
3067
+ def test_panes
3068
+ workbook = WriteExcel.new(@file)
3069
+
3070
+ worksheet1 = workbook.add_worksheet('Panes 1')
3071
+ worksheet2 = workbook.add_worksheet('Panes 2')
3072
+ worksheet3 = workbook.add_worksheet('Panes 3')
3073
+ worksheet4 = workbook.add_worksheet('Panes 4')
3074
+
3075
+ # Freeze panes
3076
+ worksheet1.freeze_panes(1, 0) # 1 row
3077
+
3078
+ worksheet2.freeze_panes(0, 1) # 1 column
3079
+ worksheet3.freeze_panes(1, 1) # 1 row and column
3080
+
3081
+ # Split panes.
3082
+ # The divisions must be specified in terms of row and column dimensions.
3083
+ # The default row height is 12.75 and the default column width is 8.43
3084
+ #
3085
+ worksheet4.split_panes(12.75, 8.43, 1, 1) # 1 row and column
3086
+
3087
+
3088
+ #######################################################################
3089
+ #
3090
+ # Set up some formatting and text to highlight the panes
3091
+ #
3092
+
3093
+ header = workbook.add_format
3094
+ header.set_color('white')
3095
+ header.set_align('center')
3096
+ header.set_align('vcenter')
3097
+ header.set_pattern
3098
+ header.set_fg_color('green')
3099
+
3100
+ center = workbook.add_format
3101
+ center.set_align('center')
3102
+
3103
+ #######################################################################
3104
+ #
3105
+ # Sheet 1
3106
+ #
3107
+
3108
+ worksheet1.set_column('A:I', 16)
3109
+ worksheet1.set_row(0, 20)
3110
+ worksheet1.set_selection('C3')
3111
+
3112
+ (0..8).each { |i| worksheet1.write(0, i, 'Scroll down', header) }
3113
+ (1..100).each do |i|
3114
+ (0..8).each { |j| worksheet1.write(i, j, i + 1, center) }
3115
+ end
3116
+
3117
+ #######################################################################
3118
+ #
3119
+ # Sheet 2
3120
+ #
3121
+
3122
+ worksheet2.set_column('A:A', 16)
3123
+ worksheet2.set_selection('C3')
3124
+
3125
+ (0..49).each do |i|
3126
+ worksheet2.set_row(i, 15)
3127
+ worksheet2.write(i, 0, 'Scroll right', header)
3128
+ end
3129
+
3130
+ (0..49).each do |i|
3131
+ (1..25).each { |j| worksheet2.write(i, j, j, center) }
3132
+ end
3133
+
3134
+ #######################################################################
3135
+ #
3136
+ # Sheet 3
3137
+ #
3138
+
3139
+ worksheet3.set_column('A:Z', 16)
3140
+ worksheet3.set_selection('C3')
3141
+
3142
+ (1..25).each { |i| worksheet3.write(0, i, 'Scroll down', header) }
3143
+
3144
+ (1..49).each { |i| worksheet3.write(i, 0, 'Scroll right', header) }
3145
+
3146
+ (1..49).each do |i|
3147
+ (1..25).each { |j| worksheet3.write(i, j, j, center) }
3148
+ end
3149
+
3150
+ #######################################################################
3151
+ #
3152
+ # Sheet 4
3153
+ #
3154
+
3155
+ worksheet4.set_selection('C3')
3156
+
3157
+ (1..25).each { |i| worksheet4.write(0, i, 'Scroll', center) }
3158
+
3159
+ (1..49).each { |i| worksheet4.write(i, 0, 'Scroll', center) }
3160
+
3161
+ (1..49).each do |i|
3162
+ (1..25).each { |j| worksheet4.write(i, j, j, center) }
3163
+ end
3164
+
3165
+ workbook.close
3166
+
3167
+ # do assertion
3168
+ compare_file("#{PERL_OUTDIR}/panes.xls", @file)
3169
+ end
3170
+
3171
+ def test_right_to_left
3172
+ workbook = WriteExcel.new(@file)
3173
+ worksheet1 = workbook.add_worksheet
3174
+ worksheet2 = workbook.add_worksheet
3175
+
3176
+ worksheet2.right_to_left
3177
+
3178
+ worksheet1.write(0, 0, 'Hello') # A1, B1, C1, ...
3179
+ worksheet2.write(0, 0, 'Hello') # ..., C1, B1, A1
3180
+
3181
+ workbook.close
3182
+
3183
+ # do assertion
3184
+ compare_file("#{PERL_OUTDIR}/right_to_left.xls", @file)
3185
+ end
3186
+
3187
+ def test_utf8
3188
+ workbook = WriteExcel.new(@file)
3189
+ worksheet = workbook.add_worksheet('シート1')
3190
+ format = workbook.add_format(:font => 'MS 明朝')
3191
+ worksheet.set_footer('フッター')
3192
+ worksheet.set_header('ヘッダー')
3193
+ worksheet.write('A1', 'UTF8文字列', format)
3194
+ worksheet.write('A2', '=CONCATENATE(A1,"の連結")', format)
3195
+ workbook.close
3196
+
3197
+ # do assertion
3198
+ compare_file("#{PERL_OUTDIR}/utf8.xls", @file)
3199
+ end
3200
+
3201
+ def test_hide_zero
3202
+ workbook = WriteExcel.new(@file)
3203
+ worksheet = workbook.add_worksheet
3204
+ worksheet.write(0, 0, 'C2, E2 value is zero, not displayed')
3205
+ worksheet.write(1, 0, 1)
3206
+ worksheet.write(1, 1, 2)
3207
+ worksheet.write(1, 2, 0)
3208
+ worksheet.write(1, 3, 4)
3209
+ worksheet.write(1, 4, 0)
3210
+
3211
+ worksheet.hide_zero
3212
+ workbook.close
3213
+
3214
+ # do assertion
3215
+ compare_file("#{PERL_OUTDIR}/hide_zero.xls", @file)
3216
+ end
3217
+
3218
+ def test_set_first_sheet
3219
+ workbook = WriteExcel.new(@file)
3220
+ 20.times { workbook.add_worksheet }
3221
+ worksheet21 = workbook.add_worksheet
3222
+ worksheet22 = workbook.add_worksheet
3223
+
3224
+ worksheet21.set_first_sheet
3225
+ worksheet22.activate
3226
+ workbook.close
3227
+
3228
+ # do assertion
3229
+ compare_file("#{PERL_OUTDIR}/set_first_sheet.xls", @file)
3230
+ end
3231
+
3232
+ def test_store_formula
3233
+ workbook = WriteExcel.new(@file)
3234
+ worksheet = workbook.add_worksheet()
3235
+
3236
+ formula = worksheet.store_formula('=A1 * 3 + 50')
3237
+ (0 .. 999).each do |row|
3238
+ worksheet.repeat_formula(row, 1, formula, nil, 'A1', "A#{row + 1}")
3239
+ end
3240
+
3241
+ workbook.close
3242
+
3243
+ # do assertion
3244
+ compare_file("#{PERL_OUTDIR}/store_formula.xls", @file)
3245
+ end
3246
+ end