writeexcel 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (109) hide show
  1. data/README +103 -0
  2. data/examples/a_simple.rb +42 -0
  3. data/examples/autofilters.rb +266 -0
  4. data/examples/copyformat.rb +51 -0
  5. data/examples/data_validate.rb +278 -0
  6. data/examples/date_time.rb +86 -0
  7. data/examples/demo.rb +118 -0
  8. data/examples/diag_border.rb +35 -0
  9. data/examples/formats.rb +489 -0
  10. data/examples/header.rb +136 -0
  11. data/examples/hidden.rb +28 -0
  12. data/examples/hyperlink.rb +42 -0
  13. data/examples/images.rb +52 -0
  14. data/examples/merge1.rb +39 -0
  15. data/examples/merge2.rb +44 -0
  16. data/examples/merge3.rb +65 -0
  17. data/examples/merge4.rb +82 -0
  18. data/examples/merge5.rb +79 -0
  19. data/examples/protection.rb +46 -0
  20. data/examples/regions.rb +52 -0
  21. data/examples/repeat.rb +42 -0
  22. data/examples/republic.png +0 -0
  23. data/examples/stats.rb +75 -0
  24. data/examples/stocks.rb +80 -0
  25. data/examples/tab_colors.rb +30 -0
  26. data/lib/writeexcel/biffwriter.rb +260 -0
  27. data/lib/writeexcel/chart.rb +217 -0
  28. data/lib/writeexcel/excelformulaparser.rb +573 -0
  29. data/lib/writeexcel/format.rb +1108 -0
  30. data/lib/writeexcel/formula.rb +986 -0
  31. data/lib/writeexcel/olewriter.rb +322 -0
  32. data/lib/writeexcel/properties.rb +250 -0
  33. data/lib/writeexcel/workbook.rb +2630 -0
  34. data/lib/writeexcel/worksheet.rb +6377 -0
  35. data/lib/writeexcel.rb +18 -0
  36. data/test/perl_output/README +31 -0
  37. data/test/perl_output/a_simple.xls +0 -0
  38. data/test/perl_output/biff_add_continue_testdata +0 -0
  39. data/test/perl_output/data_validate.xls +0 -0
  40. data/test/perl_output/date_time.xls +0 -0
  41. data/test/perl_output/demo.xls +0 -0
  42. data/test/perl_output/diag_border.xls +0 -0
  43. data/test/perl_output/f_font_biff +0 -0
  44. data/test/perl_output/f_font_key +1 -0
  45. data/test/perl_output/f_xf_biff +0 -0
  46. data/test/perl_output/file_font_biff +0 -0
  47. data/test/perl_output/file_font_key +1 -0
  48. data/test/perl_output/file_xf_biff +0 -0
  49. data/test/perl_output/headers.xls +0 -0
  50. data/test/perl_output/hidden.xls +0 -0
  51. data/test/perl_output/hyperlink.xls +0 -0
  52. data/test/perl_output/images.xls +0 -0
  53. data/test/perl_output/merge1.xls +0 -0
  54. data/test/perl_output/merge2.xls +0 -0
  55. data/test/perl_output/merge3.xls +0 -0
  56. data/test/perl_output/merge4.xls +0 -0
  57. data/test/perl_output/merge5.xls +0 -0
  58. data/test/perl_output/ole_write_header +0 -0
  59. data/test/perl_output/protection.xls +0 -0
  60. data/test/perl_output/regions.xls +0 -0
  61. data/test/perl_output/stats.xls +0 -0
  62. data/test/perl_output/stocks.xls +0 -0
  63. data/test/perl_output/tab_colors.xls +0 -0
  64. data/test/perl_output/unicode_cyrillic.xls +0 -0
  65. data/test/perl_output/workbook1.xls +0 -0
  66. data/test/perl_output/workbook2.xls +0 -0
  67. data/test/perl_output/ws_colinfo +1 -0
  68. data/test/perl_output/ws_store_colinfo +0 -0
  69. data/test/perl_output/ws_store_dimensions +0 -0
  70. data/test/perl_output/ws_store_filtermode +0 -0
  71. data/test/perl_output/ws_store_filtermode_off +0 -0
  72. data/test/perl_output/ws_store_filtermode_on +0 -0
  73. data/test/perl_output/ws_store_selection +0 -0
  74. data/test/perl_output/ws_store_window2 +1 -0
  75. data/test/republic.png +0 -0
  76. data/test/tc_all.rb +31 -0
  77. data/test/tc_biff.rb +104 -0
  78. data/test/tc_chart.rb +22 -0
  79. data/test/tc_example_match.rb +1280 -0
  80. data/test/tc_format.rb +1267 -0
  81. data/test/tc_formula.rb +63 -0
  82. data/test/tc_ole.rb +110 -0
  83. data/test/tc_workbook.rb +115 -0
  84. data/test/tc_worksheet.rb +115 -0
  85. data/test/test_00_IEEE_double.rb +14 -0
  86. data/test/test_01_add_worksheet.rb +12 -0
  87. data/test/test_02_merge_formats.rb +58 -0
  88. data/test/test_04_dimensions.rb +397 -0
  89. data/test/test_05_rows.rb +182 -0
  90. data/test/test_06_extsst.rb +80 -0
  91. data/test/test_11_date_time.rb +484 -0
  92. data/test/test_12_date_only.rb +506 -0
  93. data/test/test_13_date_seconds.rb +486 -0
  94. data/test/test_21_escher.rb +629 -0
  95. data/test/test_22_mso_drawing_group.rb +739 -0
  96. data/test/test_23_note.rb +78 -0
  97. data/test/test_24_txo.rb +80 -0
  98. data/test/test_26_autofilter.rb +327 -0
  99. data/test/test_27_autofilter.rb +144 -0
  100. data/test/test_28_autofilter.rb +174 -0
  101. data/test/test_29_process_jpg.rb +131 -0
  102. data/test/test_30_validation_dval.rb +82 -0
  103. data/test/test_31_validation_dv_strings.rb +131 -0
  104. data/test/test_32_validation_dv_formula.rb +211 -0
  105. data/test/test_40_property_types.rb +191 -0
  106. data/test/test_41_properties.rb +238 -0
  107. data/test/test_42_set_properties.rb +419 -0
  108. data/test/ts_all.rb +34 -0
  109. metadata +170 -0
@@ -0,0 +1,52 @@
1
+ #!/usr/bin/ruby -w
2
+
3
+ #######################################################################
4
+ #
5
+ # Example of how to insert images into an Excel worksheet using the
6
+ # Spreadsheet::WriteExcel insert_image() method.
7
+ #
8
+ # reverse('©'), October 2001, John McNamara, jmcnamara@cpan.org
9
+ #
10
+ # original written in Perl by John McNamara
11
+ # converted to Ruby by Hideo Nakamura, cxn03651@msj.biglobe.ne.jp
12
+ #
13
+
14
+ require 'rubygems'
15
+ require 'writeexcel'
16
+
17
+ # Create a new workbook called simple.xls and add a worksheet
18
+ workbook = Spreadsheet::WriteExcel.new("images.xls")
19
+ worksheet1 = workbook.add_worksheet('Image 1')
20
+ worksheet2 = workbook.add_worksheet('Image 2')
21
+ worksheet3 = workbook.add_worksheet('Image 3')
22
+ worksheet4 = workbook.add_worksheet('Image 4')
23
+ bp=1
24
+
25
+ # Insert a basic image
26
+ worksheet1.write('A10', "Image inserted into worksheet.")
27
+ worksheet1.insert_image('A1', 'republic.png')
28
+
29
+
30
+ # Insert an image with an offset
31
+ worksheet2.write('A10', "Image inserted with an offset.")
32
+ worksheet2.insert_image('A1', 'republic.png', 32, 10)
33
+
34
+ # Insert a scaled image
35
+ worksheet3.write('A10', "Image scaled: width x 2, height x 0.8.")
36
+ worksheet3.insert_image('A1', 'republic.png', 0, 0, 2, 0.8)
37
+
38
+ # Insert an image over varied column and row sizes
39
+ # This does not require any additional work
40
+
41
+ # Set the cols and row sizes
42
+ # NOTE: you must do this before you call insert_image()
43
+ worksheet4.set_column('A:A', 5)
44
+ worksheet4.set_column('B:B', nil, nil, 1) # Hidden
45
+ worksheet4.set_column('C:D', 10)
46
+ worksheet4.set_row(0, 30)
47
+ worksheet4.set_row(3, 5)
48
+
49
+ worksheet4.write('A10', "Image inserted over scaled rows and columns.")
50
+ worksheet4.insert_image('A1', 'republic.png')
51
+
52
+ workbook.close
@@ -0,0 +1,39 @@
1
+ #!/usr/bin/ruby -w
2
+
3
+ ###############################################################################
4
+ #
5
+ # Simple example of merging cells using the Spreadsheet::WriteExcel module.
6
+ #
7
+ # This merges three cells using the "Centre Across Selection" alignment.
8
+ # This was the Excel 5 method of achieving a merge. Use the merge_range()
9
+ # worksheet method instead. See merge3.pl - merge6.pl.
10
+ #
11
+ # reverse('©'), August 2002, John McNamara, jmcnamara@cpan.org
12
+ #
13
+ # original written in Perl by John McNamara
14
+ # converted to Ruby by Hideo Nakamura, cxn03651@msj.biglobe.ne.jp
15
+ #
16
+
17
+ require 'rubygems'
18
+ require 'writeexcel'
19
+
20
+ # Create a new workbook and add a worksheet
21
+ workbook = Spreadsheet::WriteExcel.new('merge1.xls')
22
+ worksheet = workbook.add_worksheet
23
+
24
+
25
+ # Increase the cell size of the merged cells to highlight the formatting.
26
+ worksheet.set_column('B:D', 20)
27
+ worksheet.set_row(2, 30)
28
+
29
+
30
+ # Create a merge format
31
+ format = workbook.add_format(:center_across => 1)
32
+
33
+
34
+ # Only one cell should contain text, the others should be blank.
35
+ worksheet.write(2, 1, "Center across selection", format)
36
+ worksheet.write_blank(2, 2, format)
37
+ worksheet.write_blank(2, 3, format)
38
+
39
+ workbook.close
@@ -0,0 +1,44 @@
1
+ #!/usr/bin/ruby -w
2
+
3
+ ###############################################################################
4
+ #
5
+ # Simple example of merging cells using the Spreadsheet::WriteExcel module
6
+ #
7
+ # This merges two formatted cells using the "Centre Across Selection" alignment.
8
+ # This was the Excel 5 method of achieving a merge. Use the merge_range()
9
+ # worksheet method instead. See merge3.pl - merge6.pl.
10
+ #
11
+ # reverse('©'), August 2002, John McNamara, jmcnamara@cpan.org
12
+ #
13
+ # original written in Perl by John McNamara
14
+ # converted to Ruby by Hideo Nakamura, cxn03651@msj.biglobe.ne.jp
15
+ #
16
+
17
+ require 'rubygems'
18
+ require 'writeexcel'
19
+
20
+ # Create a new workbook and add a worksheet
21
+ workbook = Spreadsheet::WriteExcel.new("merge2.xls")
22
+ worksheet = workbook.add_worksheet
23
+
24
+ # Increase the cell size of the merged cells to highlight the formatting.
25
+ worksheet.set_column(1, 2, 30)
26
+ worksheet.set_row(2, 40)
27
+
28
+ # Create a merged format
29
+ format = workbook.add_format(
30
+ :center_across => 1,
31
+ :bold => 1,
32
+ :size => 15,
33
+ :pattern => 1,
34
+ :border => 6,
35
+ :color => 'white',
36
+ :fg_color => 'green',
37
+ :border_color => 'yellow',
38
+ :align => 'vcenter'
39
+ )
40
+
41
+ # Only one cell should contain text, the others should be blank.
42
+ worksheet.write(2, 1, "Center across selection", format)
43
+ worksheet.write_blank(2, 2, format)
44
+ workbook.close
@@ -0,0 +1,65 @@
1
+ #!/usr/bin/ruby -w
2
+
3
+ ###############################################################################
4
+ #
5
+ # Example of how to use Spreadsheet::WriteExcel to write a hyperlink in a
6
+ # merged cell. There are two options write_url_range() with a standard merge
7
+ # format or merge_range().
8
+ #
9
+ # reverse('©'), September 2002, John McNamara, jmcnamara@cpan.org
10
+ #
11
+ # original written in Perl by John McNamara
12
+ # converted to Ruby by Hideo Nakamura, cxn03651@msj.biglobe.ne.jp
13
+ #
14
+
15
+ require 'rubygems'
16
+ require 'writeexcel'
17
+
18
+ # Create a new workbook and add a worksheet
19
+ workbook = Spreadsheet::WriteExcel.new("merge3.xls")
20
+ worksheet = workbook.add_worksheet()
21
+
22
+ # Increase the cell size of the merged cells to highlight the formatting.
23
+ [1, 3,6,7].each { |row| worksheet.set_row(row, 30) }
24
+ worksheet.set_column('B:D', 20)
25
+
26
+ bp=1
27
+ ###############################################################################
28
+ #
29
+ # Example 1: Merge cells containing a hyperlink using write_url_range()
30
+ # and the standard Excel 5+ merge property.
31
+ #
32
+ format1 = workbook.add_format(
33
+ :center_across => 1,
34
+ :border => 1,
35
+ :underline => 1,
36
+ :color => 'blue'
37
+ )
38
+
39
+ # Write the cells to be merged
40
+ worksheet.write_url_range('B2:D2', 'http://www.perl.com', format1)
41
+ worksheet.write_blank('C2', format1)
42
+ worksheet.write_blank('D2', format1)
43
+
44
+
45
+
46
+ ###############################################################################
47
+ #
48
+ # Example 2: Merge cells containing a hyperlink using merge_range().
49
+ #
50
+ format2 = workbook.add_format(
51
+ :border => 1,
52
+ :underline => 1,
53
+ :color => 'blue',
54
+ :align => 'center',
55
+ :valign => 'vcenter'
56
+ )
57
+
58
+ # Merge 3 cells
59
+ worksheet.merge_range('B4:D4', 'http://www.perl.com', format2)
60
+
61
+
62
+ # Merge 3 cells over two rows
63
+ worksheet.merge_range('B7:D8', 'http://www.perl.com', format2)
64
+
65
+ workbook.close
@@ -0,0 +1,82 @@
1
+ #!/usr/bin/ruby -w
2
+
3
+ ###############################################################################
4
+ #
5
+ # Example of how to use the Spreadsheet::WriteExcel merge_range() workbook
6
+ # method with complex formatting.
7
+ #
8
+ # reverse('©'), September 2002, John McNamara, jmcnamara@cpan.org
9
+ #
10
+ # original written in Perl by John McNamara
11
+ # converted to Ruby by Hideo Nakamura, cxn03651@msj.biglobe.ne.jp
12
+ #
13
+
14
+ require 'rubygems'
15
+ require 'writeexcel'
16
+
17
+ # Create a new workbook and add a worksheet
18
+ workbook = Spreadsheet::WriteExcel.new('merge4.xls')
19
+ worksheet = workbook.add_worksheet
20
+
21
+ # Increase the cell size of the merged cells to highlight the formatting.
22
+ (1..11).each { |row| worksheet.set_row(row, 30) }
23
+ worksheet.set_column('B:D', 20)
24
+
25
+ ###############################################################################
26
+ #
27
+ # Example 1: Text centered vertically and horizontally
28
+ #
29
+ format1 = workbook.add_format(
30
+ :border => 6,
31
+ :bold => 1,
32
+ :color => 'red',
33
+ :valign => 'vcenter',
34
+ :align => 'center'
35
+ )
36
+
37
+ worksheet.merge_range('B2:D3', 'Vertical and horizontal', format1)
38
+
39
+
40
+ ###############################################################################
41
+ #
42
+ # Example 2: Text aligned to the top and left
43
+ #
44
+ format2 = workbook.add_format(
45
+ :border => 6,
46
+ :bold => 1,
47
+ :color => 'red',
48
+ :valign => 'top',
49
+ :align => 'left'
50
+ )
51
+
52
+ worksheet.merge_range('B5:D6', 'Aligned to the top and left', format2)
53
+
54
+ ###############################################################################
55
+ #
56
+ # Example 3: Text aligned to the bottom and right
57
+ #
58
+ format3 = workbook.add_format(
59
+ :border => 6,
60
+ :bold => 1,
61
+ :color => 'red',
62
+ :valign => 'bottom',
63
+ :align => 'right'
64
+ )
65
+
66
+ worksheet.merge_range('B8:D9', 'Aligned to the bottom and right', format3)
67
+
68
+ ###############################################################################
69
+ #
70
+ # Example 4: Text justified (i.e. wrapped) in the cell
71
+ #
72
+ format4 = workbook.add_format(
73
+ :border => 6,
74
+ :bold => 1,
75
+ :color => 'red',
76
+ :valign => 'top',
77
+ :align => 'justify'
78
+ )
79
+
80
+ worksheet.merge_range('B11:D12', 'Justified: '+'so on and '*18, format4)
81
+
82
+ workbook.close
@@ -0,0 +1,79 @@
1
+ #!/usr/bin/ruby -w
2
+
3
+ ###############################################################################
4
+ #
5
+ # Example of how to use the Spreadsheet::WriteExcel merge_cells() workbook
6
+ # method with complex formatting and rotation.
7
+ #
8
+ #
9
+ # reverse('©'), September 2002, John McNamara, jmcnamara@cpan.org
10
+ #
11
+ # original written in Perl by John McNamara
12
+ # converted to Ruby by Hideo Nakamura, cxn03651@msj.biglobe.ne.jp
13
+ #
14
+
15
+ require 'rubygems'
16
+ require 'writeexcel'
17
+
18
+ # Create a new workbook and add a worksheet
19
+ workbook = Spreadsheet::WriteExcel.new('merge5.xls')
20
+ worksheet = workbook.add_worksheet
21
+
22
+
23
+ # Increase the cell size of the merged cells to highlight the formatting.
24
+ (3..8).each { |col| worksheet.set_row(col, 36) }
25
+ [1, 3, 5].each { |n| worksheet.set_column(n, n, 15) }
26
+
27
+
28
+ ###############################################################################
29
+ #
30
+ # Rotation 1, letters run from top to bottom
31
+ #
32
+ format1 = workbook.add_format(
33
+ :border => 6,
34
+ :bold => 1,
35
+ :color => 'red',
36
+ :valign => 'vcentre',
37
+ :align => 'centre',
38
+ :rotation => 270
39
+ )
40
+
41
+
42
+ worksheet.merge_range('B4:B9', 'Rotation 270', format1)
43
+
44
+
45
+ ###############################################################################
46
+ #
47
+ # Rotation 2, 90° anticlockwise
48
+ #
49
+ format2 = workbook.add_format(
50
+ :border => 6,
51
+ :bold => 1,
52
+ :color => 'red',
53
+ :valign => 'vcentre',
54
+ :align => 'centre',
55
+ :rotation => 90
56
+ )
57
+
58
+
59
+ worksheet.merge_range('D4:D9', 'Rotation 90°', format2)
60
+
61
+
62
+
63
+ ###############################################################################
64
+ #
65
+ # Rotation 3, 90° clockwise
66
+ #
67
+ format3 = workbook.add_format(
68
+ :border => 6,
69
+ :bold => 1,
70
+ :color => 'red',
71
+ :valign => 'vcentre',
72
+ :align => 'centre',
73
+ :rotation => -90
74
+ )
75
+
76
+
77
+ worksheet.merge_range('F4:F9', 'Rotation -90°', format3)
78
+
79
+ workbook.close
@@ -0,0 +1,46 @@
1
+ #!/usr/bin/ruby -w
2
+
3
+ ########################################################################
4
+ #
5
+ # Example of cell locking and formula hiding in an Excel worksheet via
6
+ # the Spreadsheet::WriteExcel module.
7
+ #
8
+ # reverse('©'), August 2001, John McNamara, jmcnamara@cpan.org
9
+ #
10
+ # original written in Perl by John McNamara
11
+ # converted to Ruby by Hideo Nakamura, cxn03651@msj.biglobe.ne.jp
12
+ #
13
+
14
+ require 'rubygems'
15
+ require 'writeexcel'
16
+
17
+ workbook = Spreadsheet::WriteExcel.new("protection.xls")
18
+ worksheet = workbook.add_worksheet
19
+
20
+ # Create some format objects
21
+ locked = workbook.add_format(:locked => 1)
22
+ unlocked = workbook.add_format(:locked => 0)
23
+ hidden = workbook.add_format(:hidden => 1)
24
+
25
+ # Format the columns
26
+ worksheet.set_column('A:A', 42)
27
+ worksheet.set_selection('B3:B3')
28
+
29
+ # Protect the worksheet
30
+ worksheet.protect
31
+
32
+ # Examples of cell locking and hiding
33
+ worksheet.write('A1', 'Cell B1 is locked. It cannot be edited.')
34
+ worksheet.write('B1', '=1+2', locked)
35
+
36
+ worksheet.write('A2', 'Cell B2 is unlocked. It can be edited.')
37
+ worksheet.write('B2', '=1+2', unlocked)
38
+
39
+ worksheet.write('A3', "Cell B3 is hidden. The formula isn't visible.")
40
+ worksheet.write('B3', '=1+2', hidden)
41
+
42
+ worksheet.write('A5', 'Use Menu->Tools->Protection->Unprotect Sheet')
43
+ worksheet.write('A6', 'to remove the worksheet protection. ')
44
+
45
+ workbook.close
46
+
@@ -0,0 +1,52 @@
1
+ #!/usr/bin/ruby -w
2
+
3
+ ###############################################################################
4
+ #
5
+ # Example of how to use the WriteExcel module to write a basic multiple
6
+ # worksheet Excel file.
7
+ #
8
+ # reverse('©'), March 2001, John McNamara, jmcnamara@cpan.org
9
+ #
10
+ # original written in Perl by John McNamara
11
+ # converted to Ruby by Hideo Nakamura, cxn03651@msj.biglobe.ne.jp
12
+ #
13
+
14
+ require 'rubygems'
15
+ require 'writeexcel'
16
+
17
+ workbook = Spreadsheet::WriteExcel.new("regions.xls")
18
+
19
+ # Add some worksheets
20
+ north = workbook.add_worksheet("North")
21
+ south = workbook.add_worksheet("South")
22
+ east = workbook.add_worksheet("East")
23
+ west = workbook.add_worksheet("West")
24
+
25
+ # Add a Format
26
+ format = workbook.add_format()
27
+ format.set_bold()
28
+ format.set_color('blue')
29
+
30
+ # Add a caption to each worksheet
31
+ workbook.sheets.each do |worksheet|
32
+ worksheet.write(0, 0, "Sales", format)
33
+ end
34
+
35
+ # Write some data
36
+ north.write(0, 1, 200000)
37
+ south.write(0, 1, 100000)
38
+ east.write(0, 1, 150000)
39
+ west.write(0, 1, 100000)
40
+
41
+ # Set the active worksheet
42
+ bp=1
43
+ south.activate()
44
+
45
+ # Set the width of the first column
46
+ south.set_column(0, 0, 20)
47
+
48
+ # Set the active cell
49
+ south.set_selection(0, 1)
50
+
51
+ workbook.close
52
+
@@ -0,0 +1,42 @@
1
+ #!/usr/bin/ruby -w
2
+
3
+ ######################################################################
4
+ #
5
+ # Example of writing repeated formulas.
6
+ #
7
+ # reverse('©'), August 2002, John McNamara, jmcnamara@cpan.org
8
+ #
9
+ # original written in Perl by John McNamara
10
+ # converted to Ruby by Hideo Nakamura, cxn03651@msj.biglobe.ne.jp
11
+ #
12
+
13
+ require 'rubygems'
14
+ require 'writeexcel'
15
+
16
+ workbook = Spreadsheet::WriteExcel.new("repeat.xls")
17
+ worksheet = workbook.add_worksheet
18
+
19
+ limit = 1000
20
+
21
+ # Write a column of numbers
22
+ 0.upto(limit) do |row|
23
+ worksheet.write(row, 0, row)
24
+ end
25
+
26
+ # Store a formula
27
+ formula = worksheet.store_formula('=A1*5+4')
28
+
29
+ # Write a column of formulas based on the stored formula
30
+ 0.upto(limit) do |row|
31
+ worksheet.repeat_formula(row, 1, formula, nil,
32
+ /A1/, 'A'+(row+1).to_s)
33
+ end
34
+
35
+ # Direct formula writing. As a speed comparison uncomment the
36
+ # following and run the program again
37
+
38
+ #for row (0..limit) {
39
+ # worksheet.write_formula(row, 2, '=A'.(row+1).'*5+4')
40
+ #}
41
+
42
+ workbook.close
Binary file
data/examples/stats.rb ADDED
@@ -0,0 +1,75 @@
1
+ #!/usr/bin/ruby -w
2
+
3
+ ###############################################################################
4
+ #
5
+ # This is a simple example of how to use functions with the
6
+ # Spreadsheet::WriteExcel module.
7
+ #
8
+ # reverse('©'), March 2001, John McNamara, jmcnamara@cpan.org
9
+ #
10
+ # original written in Perl by John McNamara
11
+ # converted to Ruby by Hideo Nakamura, cxn03651@msj.biglobe.ne.jp
12
+ #
13
+
14
+ require 'rubygems'
15
+ require 'writeexcel'
16
+ include Spreadsheet
17
+ xlsfile = 'stats.xls'
18
+
19
+ workbook = Spreadsheet::WriteExcel.new(xlsfile)
20
+ worksheet = workbook.add_worksheet('Test data')
21
+
22
+ # Set the column width for columns 1
23
+ worksheet.set_column(0, 0, 20)
24
+
25
+
26
+ # Create a format for the headings
27
+ format = workbook.add_format
28
+ format.set_bold
29
+
30
+
31
+ # Write the sample data
32
+ worksheet.write(0, 0, 'Sample', format)
33
+ worksheet.write(0, 1, 1)
34
+ worksheet.write(0, 2, 2)
35
+ worksheet.write(0, 3, 3)
36
+ worksheet.write(0, 4, 4)
37
+ worksheet.write(0, 5, 5)
38
+ worksheet.write(0, 6, 6)
39
+ worksheet.write(0, 7, 7)
40
+ worksheet.write(0, 8, 8)
41
+
42
+ worksheet.write(1, 0, 'Length', format)
43
+ worksheet.write(1, 1, 25.4)
44
+ worksheet.write(1, 2, 25.4)
45
+ worksheet.write(1, 3, 24.8)
46
+ worksheet.write(1, 4, 25.0)
47
+ worksheet.write(1, 5, 25.3)
48
+ worksheet.write(1, 6, 24.9)
49
+ worksheet.write(1, 7, 25.2)
50
+ worksheet.write(1, 8, 24.8)
51
+
52
+ # Write some statistical functions
53
+ worksheet.write(4, 0, 'Count', format)
54
+ worksheet.write(4, 1, '=COUNT(B1:I1)')
55
+
56
+ worksheet.write(5, 0, 'Sum', format)
57
+ worksheet.write(5, 1, '=SUM(B2:I2)')
58
+
59
+ worksheet.write(6, 0, 'Average', format)
60
+ worksheet.write(6, 1, '=AVERAGE(B2:I2)')
61
+
62
+ worksheet.write(7, 0, 'Min', format)
63
+ worksheet.write(7, 1, '=MIN(B2:I2)')
64
+
65
+ worksheet.write(8, 0, 'Max', format)
66
+ worksheet.write(8, 1, '=MAX(B2:I2)')
67
+
68
+ worksheet.write(9, 0, 'Standard Deviation', format)
69
+ worksheet.write(9, 1, '=STDEV(B2:I2)')
70
+
71
+ worksheet.write(10, 0, 'Kurtosis', format)
72
+ worksheet.write(10, 1, '=KURT(B2:I2)')
73
+
74
+ workbook.close
75
+
@@ -0,0 +1,80 @@
1
+ #!/usr/bin/ruby -w
2
+
3
+ ###############################################################################
4
+ #
5
+ # Example of formatting using the Spreadsheet::WriteExcel module
6
+ #
7
+ # This example shows how to use a conditional numerical format
8
+ # with colours to indicate if a share price has gone up or down.
9
+ #
10
+ # reverse('©'), March 2001, John McNamara, jmcnamara@cpan.org
11
+ #
12
+ # original written in Perl by John McNamara
13
+ # converted to Ruby by Hideo Nakamura, cxn03651@msj.biglobe.ne.jp
14
+ #
15
+
16
+ require 'rubygems'
17
+ require 'writeexcel'
18
+
19
+ # Create a new workbook and add a worksheet
20
+ workbook = Spreadsheet::WriteExcel.new("stocks.xls")
21
+ worksheet = workbook.add_worksheet
22
+
23
+ # Set the column width for columns 1, 2, 3 and 4
24
+ worksheet.set_column(0, 3, 15)
25
+
26
+
27
+ # Create a format for the column headings
28
+ header = workbook.add_format
29
+ header.set_bold
30
+ header.set_size(12)
31
+ header.set_color('blue')
32
+
33
+
34
+ # Create a format for the stock price
35
+ f_price = workbook.add_format
36
+ f_price.set_align('left')
37
+ f_price.set_num_format('$0.00')
38
+
39
+
40
+ # Create a format for the stock volume
41
+ f_volume = workbook.add_format
42
+ f_volume.set_align('left')
43
+ f_volume.set_num_format('#,##0')
44
+
45
+
46
+ # Create a format for the price change. This is an example of a conditional
47
+ # format. The number is formatted as a percentage. If it is positive it is
48
+ # formatted in green, if it is negative it is formatted in red and if it is
49
+ # zero it is formatted as the default font colour (in this case black).
50
+ # Note: the [Green] format produces an unappealing lime green. Try
51
+ # [Color 10] instead for a dark green.
52
+ #
53
+ f_change = workbook.add_format
54
+ f_change.set_align('left')
55
+ f_change.set_num_format('[Green]0.0%;[Red]-0.0%;0.0%')
56
+
57
+
58
+ # Write out the data
59
+ worksheet.write(0, 0, 'Company', header)
60
+ worksheet.write(0, 1, 'Price', header)
61
+ worksheet.write(0, 2, 'Volume', header)
62
+ worksheet.write(0, 3, 'Change', header)
63
+
64
+ worksheet.write(1, 0, 'Damage Inc.' )
65
+ worksheet.write(1, 1, 30.25, f_price) # $30.25
66
+ worksheet.write(1, 2, 1234567, f_volume) # 1,234,567
67
+ worksheet.write(1, 3, 0.085, f_change) # 8.5% in green
68
+
69
+ worksheet.write(2, 0, 'Dump Corp.' )
70
+ worksheet.write(2, 1, 1.56, f_price) # $1.56
71
+ worksheet.write(2, 2, 7564, f_volume) # 7,564
72
+ worksheet.write(2, 3, -0.015, f_change) # -1.5% in red
73
+
74
+ worksheet.write(3, 0, 'Rev Ltd.' )
75
+ worksheet.write(3, 1, 0.13, f_price) # $0.13
76
+ worksheet.write(3, 2, 321, f_volume) # 321
77
+ worksheet.write(3, 3, 0, f_change) # 0 in the font color (black)
78
+
79
+
80
+ workbook.close