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,30 @@
1
+ #!/usr/bin/ruby -w
2
+
3
+ #######################################################################
4
+ #
5
+ # Example of how to set Excel worksheet tab colours.
6
+ #
7
+ # reverse('©'), May 2006, 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('tab_colors.xls')
17
+
18
+ worksheet1 = workbook.add_worksheet
19
+ worksheet2 = workbook.add_worksheet
20
+ worksheet3 = workbook.add_worksheet
21
+ worksheet4 = workbook.add_worksheet
22
+
23
+ # Worsheet1 will have the default tab colour.
24
+ worksheet2.set_tab_color('red')
25
+ worksheet3.set_tab_color('green')
26
+ worksheet4.set_tab_color(0x35) # Orange
27
+
28
+ workbook.close
29
+
30
+ workbook.close
@@ -0,0 +1,260 @@
1
+ #
2
+ # BIFFwriter - An abstract base class for Excel workbooks and worksheets.
3
+ #
4
+ #
5
+ # Used in conjunction with Spreadsheet::WriteExcel
6
+ #
7
+ # Copyright 2000-2008, 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
+
14
+ require 'tempfile'
15
+
16
+ class BIFFWriter
17
+
18
+ BIFF_Version = 0x0600
19
+ BigEndian = [1].pack("I") == [1].pack("N")
20
+
21
+ attr_reader :byte_order, :data, :datasize
22
+
23
+ ######################################################################
24
+ # The args here aren't used by BIFFWriter, but they are needed by its
25
+ # subclasses. I don't feel like creating multiple constructors.
26
+ ######################################################################
27
+
28
+ def initialize
29
+ set_byte_order
30
+ @data = ''
31
+ @datasize = 0
32
+ @limit = 8224
33
+ @ignore_continue = 0
34
+
35
+ # Open a tmp file to store the majority of the Worksheet data. If this fails,
36
+ # for example due to write permissions, store the data in memory. This can be
37
+ # slow for large files.
38
+ @filehandle = Tempfile.new('spreadsheetwriteexcel')
39
+ @filehandle.binmode
40
+
41
+ # failed. store temporary data in memory.
42
+ @using_tmpfile = @filehandle ? true : false
43
+
44
+ end
45
+
46
+ ###############################################################################
47
+ #
48
+ # _set_byte_order()
49
+ #
50
+ # Determine the byte order and store it as class data to avoid
51
+ # recalculating it for each call to new().
52
+ #
53
+ def set_byte_order
54
+ # Check if "pack" gives the required IEEE 64bit float
55
+ teststr = [1.2345].pack("d")
56
+ hexdata = [0x8D, 0x97, 0x6E, 0x12, 0x83, 0xC0, 0xF3, 0x3F]
57
+ number = hexdata.pack("C8")
58
+
59
+ if number == teststr
60
+ @byte_order = 0 # Little Endian
61
+ elsif number == teststr.reverse
62
+ @byte_order = 1 # Big Endian
63
+ else
64
+ # Give up. I'll fix this in a later version.
65
+ raise( "Required floating point format not supported " +
66
+ "on this platform. See the portability section " +
67
+ "of the documentation."
68
+ )
69
+ end
70
+ end
71
+
72
+ ###############################################################################
73
+ #
74
+ # _prepend($data)
75
+ #
76
+ # General storage function
77
+ #
78
+ def prepend(*args)
79
+ d = args.join
80
+ d = add_continue(d) if d.length > @limit
81
+
82
+ @datasize += d.length
83
+ @data = d + @data
84
+
85
+ #print "prepend\n"
86
+ #print d.unpack('C*').map! {|c| sprintf("%02X", c) }.join(' ') + "\n\n"
87
+ return d
88
+ end
89
+
90
+ ###############################################################################
91
+ #
92
+ # _append($data)
93
+ #
94
+ # General storage function
95
+ #
96
+ def append(*args)
97
+ d = args.join
98
+ # Add CONTINUE records if necessary
99
+ d = add_continue(d) if d.length > @limit
100
+ if @using_tmpfile
101
+ @filehandle.write d
102
+ @datasize += d.length
103
+ else
104
+ @datasize += d.length
105
+ @data = @data + d
106
+ end
107
+ #print "apend\n"
108
+ #print d.unpack('C*').map! {|c| sprintf("%02X", c) }.join(' ') + "\n\n"
109
+ return d
110
+ end
111
+
112
+ ###############################################################################
113
+ #
114
+ # get_data().
115
+ #
116
+ # Retrieves data from memory in one chunk, or from disk in $buffer
117
+ # sized chunks.
118
+ #
119
+ def get_data
120
+ buflen = 4096
121
+
122
+ # Return data stored in memory
123
+ unless @data.nil?
124
+ tmp = @data
125
+ @data = nil
126
+ if @using_tmpfile
127
+ @filehandle.open
128
+ @filehandle.binmode
129
+ end
130
+ return tmp
131
+ end
132
+
133
+ # Return data stored on disk
134
+ if @using_tmpfile
135
+ return @filehandle.read(buflen)
136
+ end
137
+
138
+ # No data to return
139
+ return nil
140
+ end
141
+
142
+ ###############################################################################
143
+ #
144
+ # _store_bof($type)
145
+ #
146
+ # $type = 0x0005, Workbook
147
+ # $type = 0x0010, Worksheet
148
+ #
149
+ # Writes Excel BOF record to indicate the beginning of a stream or
150
+ # sub-stream in the BIFF file.
151
+ #
152
+ def store_bof(type = 0x0005)
153
+ record = 0x0809 # Record identifier
154
+ length = 0x0010 # Number of bytes to follow
155
+
156
+ # According to the SDK $build and $year should be set to zero.
157
+ # However, this throws a warning in Excel 5. So, use these
158
+ # magic numbers.
159
+ build = 0x0DBB
160
+ year = 0x07CC
161
+
162
+ bfh = 0x00000041
163
+ sfo = 0x00000006
164
+
165
+ header = [record,length].pack("vv")
166
+ data = [BIFF_Version,type,build,year,bfh,sfo].pack("vvvvVV")
167
+
168
+ prepend(header, data)
169
+ end
170
+
171
+ ###############################################################################
172
+ #
173
+ # _store_eof()
174
+ #
175
+ # Writes Excel EOF record to indicate the end of a BIFF stream.
176
+ #
177
+ def store_eof
178
+ record = 0x000A
179
+ length = 0x0000
180
+ header = [record,length].pack("vv")
181
+
182
+ append(header)
183
+ end
184
+
185
+ ###############################################################################
186
+ #
187
+ # _add_continue()
188
+ #
189
+ # Excel limits the size of BIFF records. In Excel 5 the limit is 2084 bytes. In
190
+ # Excel 97 the limit is 8228 bytes. Records that are longer than these limits
191
+ # must be split up into CONTINUE blocks.
192
+ #
193
+ # This function take a long BIFF record and inserts CONTINUE records as
194
+ # necessary.
195
+ #
196
+ # Some records have their own specialised Continue blocks so there is also an
197
+ # option to bypass this function.
198
+ #
199
+ def add_continue(data)
200
+ record = 0x003C # Record identifier
201
+
202
+ # Skip this if another method handles the continue blocks.
203
+ return data if @ignore_continue != 0
204
+
205
+ # The first 2080/8224 bytes remain intact. However, we have to change
206
+ # the length field of the record.
207
+ #
208
+
209
+ # in perl
210
+ # $tmp = substr($data, 0, $limit, "");
211
+ if data.length > @limit
212
+ tmp = data[0, @limit]
213
+ data[0, @limit] = ''
214
+ else
215
+ tmp = data.dup
216
+ data = ''
217
+ end
218
+
219
+ tmp[2, 2] = [@limit-4].pack('v')
220
+
221
+ # Strip out chunks of 2080/8224 bytes +4 for the header.
222
+ while (data.length > @limit)
223
+ header = [record, @limit].pack("vv")
224
+ tmp = tmp + header + data[0, @limit]
225
+ data[0, @limit] = ''
226
+ end
227
+
228
+ # Mop up the last of the data
229
+ header = [record, data.length].pack("vv")
230
+ tmp = tmp + header + data
231
+
232
+ return tmp
233
+ end
234
+
235
+ ###############################################################################
236
+ #
237
+ # _add_mso_generic()
238
+ # my $type = $_[0];
239
+ # my $version = $_[1];
240
+ # my $instance = $_[2];
241
+ # my $data = $_[3];
242
+ #
243
+ # Create a mso structure that is part of an Escher drawing object. These are
244
+ # are used for images, comments and filters. This generic method is used by
245
+ # other methods to create specific mso records.
246
+ #
247
+ # Returns the packed record.
248
+ #
249
+ def add_mso_generic(type, version, instance, data, length = nil)
250
+ length = length.nil? ? data.length : length
251
+
252
+ # The header contains version and instance info packed into 2 bytes.
253
+ header = version | (instance << 4)
254
+
255
+ record = [header, type, length].pack('vvV') + data
256
+
257
+ return record
258
+ end
259
+
260
+ end
@@ -0,0 +1,217 @@
1
+ ###############################################################################
2
+ #
3
+ # Chart - A writer class for Excel Charts.
4
+ #
5
+ #
6
+ # Used in conjunction with Spreadsheet::WriteExcel
7
+ #
8
+ # Copyright 2000-2008, 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 'writeexcel/biffwriter'
15
+
16
+ class Chart
17
+
18
+
19
+ attr_reader :name
20
+
21
+
22
+ ###############################################################################
23
+ #
24
+ # new()
25
+ #
26
+ # Constructor. Creates a new Chart object from a BIFFwriter object
27
+ #
28
+ def initialize(filename, name, index, encoding, activesheet, firstsheet)
29
+ @filename = filename
30
+ @name = name
31
+ @index = index
32
+ @encoding = encoding
33
+ @activesheet = activesheet
34
+ @firstsheet = firstsheet
35
+
36
+ @type = 0x0200
37
+ @ext_sheets = []
38
+ @using_tmpfile = 1
39
+ @filehandle = ""
40
+ @fileclosed = false
41
+ @offset = 0
42
+ @xls_rowmax = 0
43
+ @xls_colmax = 0
44
+ @xls_strmax = 0
45
+ @dim_rowmin = 0
46
+ @dim_rowmax = 0
47
+ @dim_colmin = 0
48
+ @dim_colmax = 0
49
+ @dim_changed = 0
50
+ @colinfo = []
51
+ @selection = [0, 0]
52
+ @panes = []
53
+ @active_pane = 3
54
+ @frozen = 0
55
+ @selected = 0
56
+ @hidden = 0
57
+
58
+ @paper_size = 0x0
59
+ @orientation = 0x1
60
+ @header = ''
61
+ @footer = ''
62
+ @hcenter = 0
63
+ @vcenter = 0
64
+ @margin_head = 0.50
65
+ @margin_foot = 0.50
66
+ @margin_left = 0.75
67
+ @margin_right = 0.75
68
+ @margin_top = 1.00
69
+ @margin_bottom = 1.00
70
+
71
+ @title_rowmin = nil
72
+ @title_rowmax = nil
73
+ @title_colmin = nil
74
+ @title_colmax = nil
75
+ @print_rowmin = nil
76
+ @print_rowmax = nil
77
+ @print_colmin = nil
78
+ @print_colmax = nil
79
+
80
+ @print_gridlines = 1
81
+ @screen_gridlines = 1
82
+ @print_headers = 0
83
+
84
+ @fit_page = 0
85
+ @fit_width = 0
86
+ @fit_height = 0
87
+
88
+ @hbreaks = []
89
+ @vbreaks = []
90
+
91
+ @protect = 0
92
+ @password = nil
93
+
94
+ @col_sizes = {}
95
+ @row_sizes = {}
96
+
97
+ @col_formats = {}
98
+ @row_formats = {}
99
+
100
+ @zoom = 100
101
+ @print_scale = 100
102
+
103
+ @leading_zeros = 0
104
+
105
+ @outline_row_level = 0
106
+ @outline_style = 0
107
+ @outline_below = 1
108
+ @outline_right = 1
109
+ @outline_on = 1
110
+
111
+ _initialize
112
+ end
113
+
114
+ ###############################################################################
115
+ #
116
+ # get_data().
117
+ #
118
+ # Retrieves data from memory in one chunk, or from disk in $buffer
119
+ # sized chunks.
120
+ #
121
+ def get_data
122
+ buffer = 4096
123
+
124
+ return tmp if read(@filehandle, tmp, buffer)
125
+
126
+ # No data to return
127
+ return nil
128
+ end
129
+
130
+
131
+ ###############################################################################
132
+ #
133
+ # select()
134
+ #
135
+ # Set this worksheet as a selected worksheet, i.e. the worksheet has its tab
136
+ # highlighted.
137
+ #
138
+ def select
139
+ @hidden = 0 # Selected worksheet can't be hidden.
140
+ @selected = 1
141
+ end
142
+
143
+
144
+ ###############################################################################
145
+ #
146
+ # activate()
147
+ #
148
+ # Set this worksheet as the active worksheet, i.e. the worksheet that is
149
+ # displayed when the workbook is opened. Also set it as selected.
150
+ #
151
+ def activate
152
+ @hidden = 0 # Active worksheet can't be hidden.
153
+ @selected = 1
154
+ @activesheet = @index
155
+ end
156
+
157
+
158
+ ###############################################################################
159
+ #
160
+ # hide()
161
+ #
162
+ # Hide this worksheet.
163
+ #
164
+ def hide
165
+ @hidd = 1
166
+
167
+ # A hidden worksheet shouldn't be active or selected.
168
+ @selecte = 0
169
+ @activesheet = 0
170
+ @firstsheet = 0
171
+ end
172
+
173
+
174
+ ###############################################################################
175
+ #
176
+ # set_first_sheet()
177
+ #
178
+ # Set this worksheet as the first visible sheet. This is necessary
179
+ # when there are a large number of worksheets and the activated
180
+ # worksheet is not visible on the screen.
181
+ #
182
+ def set_first_sheet
183
+ hidden = 0 # Active worksheet can't be hidden.
184
+ firstsheet = index
185
+ end
186
+
187
+
188
+
189
+ ###############################################################################
190
+
191
+ private
192
+
193
+ ###############################################################################
194
+
195
+
196
+ ###############################################################################
197
+ #
198
+ # _initialize()
199
+ #
200
+ def _initialize
201
+ filehandle = open(@filename, "rb") or
202
+ die "Couldn't open #{@filename} in add_chart_ext(): $!.\n"
203
+ @filehandle = filehandle
204
+ @datasize = File.Stat.size(@filename)
205
+ end
206
+
207
+ ###############################################################################
208
+ #
209
+ # _close()
210
+ #
211
+ # Add data to the beginning of the workbook (note the reverse order)
212
+ # and to the end of the workbook.
213
+ #
214
+ def _close
215
+ end
216
+
217
+ end