writeexcel 0.1.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README +26 -31
- data/examples/a_simple.rb +42 -42
- data/examples/{autofilters.rb → autofilter.rb} +264 -266
- data/examples/bigfile.rb +29 -0
- data/examples/chart_area.rb +120 -0
- data/examples/chart_bar.rb +119 -0
- data/examples/chart_column.rb +119 -0
- data/examples/chart_line.rb +119 -0
- data/examples/chart_pie.rb +107 -0
- data/examples/chart_scatter.rb +120 -0
- data/examples/chart_stock.rb +147 -0
- data/examples/copyformat.rb +51 -51
- data/examples/data_validate.rb +278 -278
- data/examples/date_time.rb +86 -86
- data/examples/defined_name.rb +31 -0
- data/examples/demo.rb +120 -118
- data/examples/diag_border.rb +35 -35
- data/examples/formats.rb +489 -489
- data/examples/header.rb +136 -136
- data/examples/hidden.rb +28 -28
- data/examples/hyperlink.rb +42 -42
- data/examples/images.rb +52 -52
- data/examples/merge1.rb +39 -39
- data/examples/merge2.rb +44 -44
- data/examples/merge3.rb +65 -65
- data/examples/merge4.rb +82 -82
- data/examples/merge5.rb +79 -79
- data/examples/properties.rb +33 -0
- data/examples/properties_jp.rb +32 -0
- data/examples/protection.rb +46 -46
- data/examples/regions.rb +52 -52
- data/examples/repeat.rb +42 -42
- data/examples/stats.rb +75 -75
- data/examples/stocks.rb +80 -80
- data/examples/tab_colors.rb +30 -30
- data/examples/write_arrays.rb +82 -0
- data/lib/writeexcel.rb +1134 -18
- data/lib/writeexcel/biffwriter.rb +273 -260
- data/lib/writeexcel/chart.rb +2306 -217
- data/lib/writeexcel/charts/area.rb +152 -0
- data/lib/writeexcel/charts/bar.rb +177 -0
- data/lib/writeexcel/charts/column.rb +156 -0
- data/lib/writeexcel/charts/external.rb +61 -0
- data/lib/writeexcel/charts/line.rb +152 -0
- data/lib/writeexcel/charts/pie.rb +169 -0
- data/lib/writeexcel/charts/scatter.rb +192 -0
- data/lib/writeexcel/charts/stock.rb +211 -0
- data/lib/writeexcel/excelformulaparser.rb +208 -195
- data/lib/writeexcel/format.rb +1697 -1108
- data/lib/writeexcel/formula.rb +1050 -986
- data/lib/writeexcel/olewriter.rb +322 -322
- data/lib/writeexcel/properties.rb +251 -250
- data/lib/writeexcel/storage_lite.rb +968 -0
- data/lib/writeexcel/workbook.rb +3294 -2630
- data/lib/writeexcel/worksheet.rb +9012 -6377
- data/test/excelfile/Chart1.xls +0 -0
- data/test/excelfile/Chart2.xls +0 -0
- data/test/excelfile/Chart3.xls +0 -0
- data/test/excelfile/Chart4.xls +0 -0
- data/test/excelfile/Chart5.xls +0 -0
- data/test/perl_output/Chart1.xls.data +0 -0
- data/test/perl_output/Chart2.xls.data +0 -0
- data/test/perl_output/Chart3.xls.data +0 -0
- data/test/perl_output/Chart4.xls.data +0 -0
- data/test/perl_output/Chart5.xls.data +0 -0
- data/test/perl_output/a_simple.xls +0 -0
- data/test/perl_output/autofilter.xls +0 -0
- data/test/perl_output/chart_area.xls +0 -0
- data/test/perl_output/chart_bar.xls +0 -0
- data/test/perl_output/chart_column.xls +0 -0
- data/test/perl_output/chart_line.xls +0 -0
- data/test/perl_output/data_validate.xls +0 -0
- data/test/perl_output/date_time.xls +0 -0
- data/test/perl_output/demo.xls +0 -0
- data/test/perl_output/demo101.bin +0 -0
- data/test/perl_output/demo201.bin +0 -0
- data/test/perl_output/demo301.bin +0 -0
- data/test/perl_output/demo401.bin +0 -0
- data/test/perl_output/demo501.bin +0 -0
- data/test/perl_output/diag_border.xls +0 -0
- data/test/perl_output/headers.xls +0 -0
- data/test/perl_output/hyperlink.xls +0 -0
- data/test/perl_output/images.xls +0 -0
- data/test/perl_output/merge1.xls +0 -0
- data/test/perl_output/merge2.xls +0 -0
- data/test/perl_output/merge3.xls +0 -0
- data/test/perl_output/merge4.xls +0 -0
- data/test/perl_output/merge5.xls +0 -0
- data/test/perl_output/protection.xls +0 -0
- data/test/perl_output/regions.xls +0 -0
- data/test/perl_output/stats.xls +0 -0
- data/test/perl_output/stocks.xls +0 -0
- data/test/perl_output/tab_colors.xls +0 -0
- data/test/perl_output/unicode_cyrillic.xls +0 -0
- data/test/perl_output/workbook1.xls +0 -0
- data/test/perl_output/workbook2.xls +0 -0
- data/test/tc_all.rb +32 -31
- data/test/tc_biff.rb +104 -104
- data/test/tc_chart.rb +22 -22
- data/test/tc_example_match.rb +1944 -1280
- data/test/tc_format.rb +1254 -1267
- data/test/tc_formula.rb +63 -63
- data/test/tc_ole.rb +110 -110
- data/test/tc_storage_lite.rb +149 -0
- data/test/tc_workbook.rb +140 -115
- data/test/tc_worksheet.rb +115 -115
- data/test/test_00_IEEE_double.rb +14 -14
- data/test/test_01_add_worksheet.rb +12 -12
- data/test/test_02_merge_formats.rb +58 -58
- data/test/test_04_dimensions.rb +397 -397
- data/test/test_05_rows.rb +182 -182
- data/test/test_06_extsst.rb +80 -80
- data/test/test_11_date_time.rb +484 -484
- data/test/test_12_date_only.rb +506 -506
- data/test/test_13_date_seconds.rb +486 -486
- data/test/test_21_escher.rb +642 -629
- data/test/test_22_mso_drawing_group.rb +750 -739
- data/test/test_23_note.rb +78 -78
- data/test/test_24_txo.rb +80 -80
- data/test/test_25_position_object.rb +82 -0
- data/test/test_26_autofilter.rb +327 -327
- data/test/test_27_autofilter.rb +144 -144
- data/test/test_28_autofilter.rb +174 -174
- data/test/test_29_process_jpg.rb +681 -131
- data/test/test_30_validation_dval.rb +82 -82
- data/test/test_31_validation_dv_strings.rb +131 -131
- data/test/test_32_validation_dv_formula.rb +211 -211
- data/test/test_40_property_types.rb +191 -191
- data/test/test_41_properties.rb +238 -238
- data/test/test_42_set_properties.rb +442 -419
- data/test/test_50_name_stored.rb +305 -0
- data/test/test_51_name_print_area.rb +363 -0
- data/test/test_52_name_print_titles.rb +460 -0
- data/test/test_53_autofilter.rb +209 -0
- data/test/test_60_chart_generic.rb +576 -0
- data/test/test_61_chart_subclasses.rb +97 -0
- data/test/test_62_chart_formats.rb +270 -0
- data/test/test_63_chart_area_formats.rb +647 -0
- data/test/test_chartex.rb +35 -0
- data/test/ts_all.rb +46 -34
- data/writeexcel.gemspec +18 -0
- data/writeexcel.rdoc +583 -0
- metadata +162 -108
data/lib/writeexcel.rb
CHANGED
@@ -1,18 +1,1134 @@
|
|
1
|
-
###############################################################################
|
2
|
-
#
|
3
|
-
# WriteExcel.
|
4
|
-
#
|
5
|
-
#
|
6
|
-
#
|
7
|
-
# Copyright 2000-
|
8
|
-
#
|
9
|
-
# original written in Perl by John McNamara
|
10
|
-
# converted to Ruby by Hideo Nakamura, cxn03651@msj.biglobe.ne.jp
|
11
|
-
#
|
12
|
-
require "writeexcel/workbook"
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
1
|
+
###############################################################################
|
2
|
+
#
|
3
|
+
# WriteExcel.
|
4
|
+
#
|
5
|
+
# WriteExcel - Write to a cross-platform Excel binary file.
|
6
|
+
#
|
7
|
+
# Copyright 2000-2010, 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
|
+
require "writeexcel/workbook"
|
13
|
+
#
|
14
|
+
# = WriteExcel - Write to a cross-platform Excel binary file.
|
15
|
+
#
|
16
|
+
# == Contents
|
17
|
+
# SYSNOPSYS
|
18
|
+
# DESCRIPTION
|
19
|
+
# QUICK START
|
20
|
+
# WORKBOOK METHODS
|
21
|
+
# WORKSHEET METHODS
|
22
|
+
# PAGE SET-UP METHODS
|
23
|
+
# CELL FORMATTING
|
24
|
+
# FORMAT METHODS
|
25
|
+
# COLOURS IN EXCEL
|
26
|
+
# DATE AND TIME IN EXCEL
|
27
|
+
# OUTLINES AND GROUPING IN EXCEL
|
28
|
+
# DATA VALIDATION IN EXCEL
|
29
|
+
# FORMULAS AND FUNCTIONS IN EXCEL
|
30
|
+
# CHART
|
31
|
+
#
|
32
|
+
# == Synopsis
|
33
|
+
#
|
34
|
+
# To write a string, a formatted string, a number and a formula to the first
|
35
|
+
# worksheet in an Excel workbook called ruby.xls:
|
36
|
+
#
|
37
|
+
# require 'WriteExcel'
|
38
|
+
#
|
39
|
+
# # Create a new Excel workbook
|
40
|
+
# workbook = WriteExcel.new('ruby.xls')
|
41
|
+
#
|
42
|
+
# # Add a worksheet
|
43
|
+
# worksheet = workbook.add_worksheet
|
44
|
+
#
|
45
|
+
# # Add and define a format
|
46
|
+
# format = workbook.add_format # Add a format
|
47
|
+
# format.set_bold()
|
48
|
+
# format.set_color('red')
|
49
|
+
# format.set_align('center')
|
50
|
+
#
|
51
|
+
# # Write a formatted and unformatted string, row and column notation.
|
52
|
+
# col = row = 0
|
53
|
+
# worksheet.write(row, col, 'Hi Excel!', format)
|
54
|
+
# worksheet.write(1, col, 'Hi Excel!')
|
55
|
+
#
|
56
|
+
# # Write a number and a formula using A1 notation
|
57
|
+
# worksheet.write('A3', 1.2345)
|
58
|
+
# worksheet.write('A4', '=SIN(PI()/4)')
|
59
|
+
#
|
60
|
+
# == Description
|
61
|
+
#
|
62
|
+
# WriteExcel can be used to create a cross-platform Excel binary file.
|
63
|
+
# Multiple worksheets can be added to a workbook and formatting can be applied
|
64
|
+
# to cells. Text, numbers, formulas, hyperlinks and images can be written to
|
65
|
+
# the cells.
|
66
|
+
#
|
67
|
+
# The Excel file produced by this gem is compatible with 97, 2000, 2002, 2003
|
68
|
+
# and 2007.
|
69
|
+
#
|
70
|
+
# WriteExcel will work on the majority of Windows, UNIX and Mac platforms.
|
71
|
+
# Generated files are also compatible with the Linux/UNIX spreadsheet
|
72
|
+
# applications Gnumeric and OpenOffice.org.
|
73
|
+
#
|
74
|
+
# This module cannot be used to write to an existing Excel file
|
75
|
+
#
|
76
|
+
# This library is converted from Spreadsheet::WriteExcel module of Perl.
|
77
|
+
# http://search.cpan.org/~jmcnamara/Spreadsheet-WriteExcel-2.37/
|
78
|
+
#
|
79
|
+
# == Quick Start
|
80
|
+
#
|
81
|
+
# WriteExcel tries to provide an interface to as many of Excel's features as
|
82
|
+
# possible. As a result there is a lot of documentation to accompany the
|
83
|
+
# interface and it can be difficult at first glance to see what it important
|
84
|
+
# and what is not. So for those of you who prefer to assemble Ikea furniture
|
85
|
+
# first and then read the instructions, here are three easy steps:
|
86
|
+
#
|
87
|
+
# 1. Create a new Excel workbook (i.e. file) using new().
|
88
|
+
#
|
89
|
+
# 2. Add a worksheet to the new workbook using add_worksheet().
|
90
|
+
#
|
91
|
+
# 3. Write to the worksheet using write().
|
92
|
+
#
|
93
|
+
# Like this:
|
94
|
+
#
|
95
|
+
# require 'WriteExcel' # Step 0
|
96
|
+
#
|
97
|
+
# workbook = WriteExcel.new('ruby.xls') # Step 1
|
98
|
+
# worksheet = workbook.add_worksheet # Step 2
|
99
|
+
# worksheet.write('A1', 'Hi Excel!') # Step 3
|
100
|
+
#
|
101
|
+
# This will create an Excel file called ruby.xls with a single worksheet and the
|
102
|
+
# text 'Hi Excel!' in the relevant cell. And that's it. Okay, so there is
|
103
|
+
# actually a zeroth step as well, but use WriteExcel goes without saying. There
|
104
|
+
# are also many examples that come with the distribution and which you can
|
105
|
+
# use to get you started. See EXAMPLES.
|
106
|
+
#
|
107
|
+
# = Workbook methods
|
108
|
+
#
|
109
|
+
# The WriteExcel module provides an object oriented interface
|
110
|
+
# to a new Excel workbook. The following methods are available through
|
111
|
+
# a new workbook.
|
112
|
+
#
|
113
|
+
# new()
|
114
|
+
# add_worksheet()
|
115
|
+
# add_format()
|
116
|
+
# add_chart()
|
117
|
+
# add_chart_ext()
|
118
|
+
# close()
|
119
|
+
# compatibility_mode()
|
120
|
+
# set_properties()
|
121
|
+
# define_name()
|
122
|
+
# set_tempdir()
|
123
|
+
# set_custom_color()
|
124
|
+
# sheets()
|
125
|
+
# set_1904()
|
126
|
+
# set_codepage()
|
127
|
+
#
|
128
|
+
# = Worksheet methods
|
129
|
+
#
|
130
|
+
# A new worksheet is created by calling the add_worksheet() method from
|
131
|
+
# a workbook object:
|
132
|
+
#
|
133
|
+
# worksheet1 = workbook.add_worksheet
|
134
|
+
# worksheet2 = workbook.add_worksheet
|
135
|
+
#
|
136
|
+
# The following methods are available through a new worksheet:
|
137
|
+
#
|
138
|
+
# write()
|
139
|
+
# write_number()
|
140
|
+
# write_string()
|
141
|
+
# write_utf16be_string()
|
142
|
+
# write_utf16le_string()
|
143
|
+
# keep_leading_zeros()
|
144
|
+
# write_blank()
|
145
|
+
# write_row()
|
146
|
+
# write_col()
|
147
|
+
# write_date_time()
|
148
|
+
# write_url()
|
149
|
+
# write_url_range()
|
150
|
+
# write_formula()
|
151
|
+
# store_formula()
|
152
|
+
# repeat_formula()
|
153
|
+
# write_comment()
|
154
|
+
# show_comments()
|
155
|
+
# add_write_handler() (* not implemented yet)
|
156
|
+
# insert_image()
|
157
|
+
# insert_chart()
|
158
|
+
# data_validation()
|
159
|
+
# get_name()
|
160
|
+
# activate()
|
161
|
+
# select()
|
162
|
+
# hide()
|
163
|
+
# set_first_sheet()
|
164
|
+
# protect()
|
165
|
+
# set_selection()
|
166
|
+
# set_row()
|
167
|
+
# set_column()
|
168
|
+
# outline_settings()
|
169
|
+
# freeze_panes()
|
170
|
+
# split_panes()
|
171
|
+
# merge_range()
|
172
|
+
# set_zoom()
|
173
|
+
# right_to_left()
|
174
|
+
# hide_zero()
|
175
|
+
# set_tab_color()
|
176
|
+
# autofilter()
|
177
|
+
#
|
178
|
+
# == Cell notation
|
179
|
+
# WriteExcel supports two forms of notation to designate the position of cells:
|
180
|
+
# Row-column notation and A1 notation.
|
181
|
+
#
|
182
|
+
# Row-column notation uses a zero based index for both row and column while A1
|
183
|
+
# notation uses the standard Excel alphanumeric sequence of column letter and
|
184
|
+
# 1-based row. For example:
|
185
|
+
#
|
186
|
+
# (0, 0) # The top left cell in row-column notation.
|
187
|
+
# ('A1') # The top left cell in A1 notation.
|
188
|
+
#
|
189
|
+
# (1999, 29) # Row-column notation.
|
190
|
+
# ('AD2000') # The same cell in A1 notation.
|
191
|
+
#
|
192
|
+
# Row-column notation is useful if you are referring to cells
|
193
|
+
# programmatically:
|
194
|
+
#
|
195
|
+
# (0 .. 10).each do |i|
|
196
|
+
# worksheet.write(i, 0, 'Hello') # Cells A1 to A10
|
197
|
+
# end
|
198
|
+
#
|
199
|
+
# A1 notation is useful for setting up a worksheet manually and for working
|
200
|
+
# with formulas:
|
201
|
+
#
|
202
|
+
# worksheet.write('H1', 200)
|
203
|
+
# worksheet.write('H2', '=H1+1')
|
204
|
+
#
|
205
|
+
# In formulas and applicable methods you can also use the A:A column notation:
|
206
|
+
#
|
207
|
+
# worksheet.write('A1', '=SUM(B:B)')
|
208
|
+
#
|
209
|
+
# For simplicity, the parameter lists for the worksheet method calls in the
|
210
|
+
# following sections are given in terms of row-column notation. In all cases
|
211
|
+
# it is also possible to use A1 notation.
|
212
|
+
#
|
213
|
+
# Note: in Excel it is also possible to use a R1C1 notation. This is not
|
214
|
+
# supported by WriteExcel.
|
215
|
+
#
|
216
|
+
# ==PAGE SET-UP METHODS
|
217
|
+
#
|
218
|
+
# Page set-up methods affect the way that a worksheet looks when it is printed.
|
219
|
+
# They control features such as page headers and footers and margins. These
|
220
|
+
# methods are really just standard worksheet methods. They are documented
|
221
|
+
# here in a separate section for the sake of clarity.
|
222
|
+
#
|
223
|
+
# The following methods are available for page set-up:
|
224
|
+
#
|
225
|
+
# set_landscape()
|
226
|
+
# set_portrait()
|
227
|
+
# set_page_view()
|
228
|
+
# set_paper()
|
229
|
+
# center_horizontally()
|
230
|
+
# center_vertically()
|
231
|
+
# set_margins()
|
232
|
+
# set_header()
|
233
|
+
# set_footer()
|
234
|
+
# repeat_rows()
|
235
|
+
# repeat_columns()
|
236
|
+
# hide_gridlines()
|
237
|
+
# print_row_col_headers()
|
238
|
+
# print_area()
|
239
|
+
# print_across()
|
240
|
+
# fit_to_pages()
|
241
|
+
# set_start_page()
|
242
|
+
# set_print_scale()
|
243
|
+
# set_h_pagebreaks()
|
244
|
+
# set_v_pagebreaks()
|
245
|
+
#
|
246
|
+
# A common requirement when working with WriteExcel is to apply the same page
|
247
|
+
# set-up features to all of the worksheets in a workbook. To do this you can use
|
248
|
+
# the sheets() method of the workbook class to access the array of worksheets
|
249
|
+
# in a workbook:
|
250
|
+
#
|
251
|
+
# workbook.sheets.each do |worksheet|
|
252
|
+
# worksheet.set_landscape
|
253
|
+
# end
|
254
|
+
#
|
255
|
+
# ==CELL FORMATTING
|
256
|
+
#
|
257
|
+
# This section describes the methods and properties that are available for
|
258
|
+
# formatting cells in Excel. The properties of a cell that can be formatted
|
259
|
+
# include: fonts, colours, patterns, borders, alignment and number formatting.
|
260
|
+
#
|
261
|
+
# ===Creating and using a Format object
|
262
|
+
#
|
263
|
+
# Cell formatting is defined through a Format object. Format objects are
|
264
|
+
# created by calling the workbook add_format() method as follows:
|
265
|
+
#
|
266
|
+
# format1 = workbook.add_format # Set properties later
|
267
|
+
# format2 = workbook.add_format(property hash..) # Set at creation
|
268
|
+
#
|
269
|
+
# The format object holds all the formatting properties that can be applied
|
270
|
+
# to a cell, a row or a column. The process of setting these properties is
|
271
|
+
# discussed in the next section.
|
272
|
+
#
|
273
|
+
# Once a Format object has been constructed and it properties have been set
|
274
|
+
# it can be passed as an argument to the worksheet write methods as follows:
|
275
|
+
#
|
276
|
+
# worksheet.write(0, 0, 'One', format)
|
277
|
+
# worksheet.write_string(1, 0, 'Two', format)
|
278
|
+
# worksheet.write_number(2, 0, 3, format)
|
279
|
+
# worksheet.write_blank(3, 0, format)
|
280
|
+
#
|
281
|
+
# Formats can also be passed to the worksheet set_row() and set_column()
|
282
|
+
# methods to define the default property for a row or column.
|
283
|
+
#
|
284
|
+
# worksheet.set_row(0, 15, format)
|
285
|
+
# worksheet.set_column(0, 0, 15, format)
|
286
|
+
#
|
287
|
+
# ===Format methods and Format properties
|
288
|
+
#
|
289
|
+
# The following table shows the Excel format categories, the formatting
|
290
|
+
# properties that can be applied and the equivalent object method:
|
291
|
+
#
|
292
|
+
# Category Description Property Method Name
|
293
|
+
# -------- ----------- -------- -----------
|
294
|
+
# Font Font type font set_font()
|
295
|
+
# Font size size set_size()
|
296
|
+
# Font color color set_color()
|
297
|
+
# Bold bold set_bold()
|
298
|
+
# Italic italic set_italic()
|
299
|
+
# Underline underline set_underline()
|
300
|
+
# Strikeout font_strikeout set_font_strikeout()
|
301
|
+
# Super/Subscript font_script set_font_script()
|
302
|
+
# Outline font_outline set_font_outline()
|
303
|
+
# Shadow font_shadow set_font_shadow()
|
304
|
+
#
|
305
|
+
# Number Numeric format num_format set_num_format()
|
306
|
+
#
|
307
|
+
# Protection Lock cells locked set_locked()
|
308
|
+
# Hide formulas hidden set_hidden()
|
309
|
+
#
|
310
|
+
# Alignment Horizontal align align set_align()
|
311
|
+
# Vertical align valign set_align()
|
312
|
+
# Rotation rotation set_rotation()
|
313
|
+
# Text wrap text_wrap set_text_wrap()
|
314
|
+
# Justify last text_justlast set_text_justlast()
|
315
|
+
# Center across center_across set_center_across()
|
316
|
+
# Indentation indent set_indent()
|
317
|
+
# Shrink to fit shrink set_shrink()
|
318
|
+
#
|
319
|
+
# Pattern Cell pattern pattern set_pattern()
|
320
|
+
# Background color bg_color set_bg_color()
|
321
|
+
# Foreground color fg_color set_fg_color()
|
322
|
+
#
|
323
|
+
# Border Cell border border set_border()
|
324
|
+
# Bottom border bottom set_bottom()
|
325
|
+
# Top border top set_top()
|
326
|
+
# Left border left set_left()
|
327
|
+
# Right border right set_right()
|
328
|
+
# Border color border_color set_border_color()
|
329
|
+
# Bottom color bottom_color set_bottom_color()
|
330
|
+
# Top color top_color set_top_color()
|
331
|
+
# Left color left_color set_left_color()
|
332
|
+
# Right color right_color set_right_color()
|
333
|
+
#
|
334
|
+
# There are two ways of setting Format properties: by using the object method
|
335
|
+
# interface or by setting the property directly. For example, a typical use of
|
336
|
+
# the method interface would be as follows:
|
337
|
+
#
|
338
|
+
# format = workbook.add_format
|
339
|
+
# format.set_bold
|
340
|
+
# format.set_color('red')
|
341
|
+
#
|
342
|
+
# By comparison the properties can be set directly by passing a hash of
|
343
|
+
# properties to the Format constructor:
|
344
|
+
#
|
345
|
+
# format = workbook.add_format(:bold => 1, :color => 'red')
|
346
|
+
#
|
347
|
+
# or after the Format has been constructed by means of the
|
348
|
+
# set_format_properties() method as follows:
|
349
|
+
#
|
350
|
+
# format = workbook.add_format
|
351
|
+
# format.set_format_properties(:bold => 1, :color => 'red')
|
352
|
+
#
|
353
|
+
# You can also store the properties in one or more named hashes and pass them
|
354
|
+
# to the required method:
|
355
|
+
#
|
356
|
+
# font = {
|
357
|
+
# :font => 'Arial',
|
358
|
+
# :size => 12,
|
359
|
+
# :color => 'blue',
|
360
|
+
# :bold => 1
|
361
|
+
# }
|
362
|
+
#
|
363
|
+
# shading = {
|
364
|
+
# :bg_color => 'green',
|
365
|
+
# :pattern => 1
|
366
|
+
# }
|
367
|
+
#
|
368
|
+
# format1 = workbook.add_format(font) # Font only
|
369
|
+
# format2 = workbook.add_format(font, shading) # Font and shading
|
370
|
+
#
|
371
|
+
# The provision of two ways of setting properties might lead you to wonder
|
372
|
+
# which is the best way. The method mechanism may be better is you prefer
|
373
|
+
# setting properties via method calls (which the author did when they were
|
374
|
+
# code was first written) otherwise passing properties to the constructor has
|
375
|
+
# proved to be a little more flexible and self documenting in practice. An
|
376
|
+
# additional advantage of working with property hashes is that it allows you to
|
377
|
+
# share formatting between workbook objects as shown in the example above.
|
378
|
+
#
|
379
|
+
#--
|
380
|
+
#
|
381
|
+
# did not converted ???
|
382
|
+
#
|
383
|
+
# The Perl/Tk style of adding properties is also supported:
|
384
|
+
#
|
385
|
+
# %font = (
|
386
|
+
# -font => 'Arial',
|
387
|
+
# -size => 12,
|
388
|
+
# -color => 'blue',
|
389
|
+
# -bold => 1,
|
390
|
+
# )
|
391
|
+
#++
|
392
|
+
#
|
393
|
+
# ===Working with formats
|
394
|
+
#
|
395
|
+
# The default format is Arial 10 with all other properties off.
|
396
|
+
#
|
397
|
+
# Each unique format in WriteExcel must have a corresponding
|
398
|
+
# Format object. It isn't possible to use a Format with a write() method and
|
399
|
+
# then redefine the Format for use at a later stage. This is because a Format
|
400
|
+
# is applied to a cell not in its current state but in its final state.
|
401
|
+
# Consider the following example:
|
402
|
+
#
|
403
|
+
# format = workbook.add_format
|
404
|
+
# format.set_bold
|
405
|
+
# format.set_color('red')
|
406
|
+
# worksheet.write('A1', 'Cell A1', format)
|
407
|
+
# format.set_color('green')
|
408
|
+
# worksheet.write('B1', 'Cell B1', format)
|
409
|
+
#
|
410
|
+
# Cell A1 is assigned the Format _format_ which is initially set to the colour
|
411
|
+
# red. However, the colour is subsequently set to green. When Excel displays
|
412
|
+
# Cell A1 it will display the final state of the Format which in this case
|
413
|
+
# will be the colour green.
|
414
|
+
#
|
415
|
+
# In general a method call without an argument will turn a property on,
|
416
|
+
# for example:
|
417
|
+
#
|
418
|
+
# format1 = workbook.add_format
|
419
|
+
# format1.set_bold # Turns bold on
|
420
|
+
# format1.set_bold(1) # Also turns bold on
|
421
|
+
# format1.set_bold(0) # Turns bold off
|
422
|
+
#
|
423
|
+
# ==FORMAT METHODS
|
424
|
+
#
|
425
|
+
# The Format object methods are described in more detail in the following
|
426
|
+
# sections. In addition, there is a Ruby program called formats.rb in the
|
427
|
+
# examples directory of the WriteExcel distribution. This program creates an
|
428
|
+
# Excel workbook called formats.xls which contains examples of almost all
|
429
|
+
# the format types.
|
430
|
+
#
|
431
|
+
# The following Format methods are available:
|
432
|
+
#
|
433
|
+
# set_font()
|
434
|
+
# set_size()
|
435
|
+
# set_color()
|
436
|
+
# set_bold()
|
437
|
+
# set_italic()
|
438
|
+
# set_underline()
|
439
|
+
# set_font_strikeout()
|
440
|
+
# set_font_script()
|
441
|
+
# set_font_outline()
|
442
|
+
# set_font_shadow()
|
443
|
+
# set_num_format()
|
444
|
+
# set_locked()
|
445
|
+
# set_hidden()
|
446
|
+
# set_align()
|
447
|
+
# set_rotation()
|
448
|
+
# set_text_wrap()
|
449
|
+
# set_text_justlast()
|
450
|
+
# set_center_across()
|
451
|
+
# set_indent()
|
452
|
+
# set_shrink()
|
453
|
+
# set_pattern()
|
454
|
+
# set_bg_color()
|
455
|
+
# set_fg_color()
|
456
|
+
# set_border()
|
457
|
+
# set_bottom()
|
458
|
+
# set_top()
|
459
|
+
# set_left()
|
460
|
+
# set_right()
|
461
|
+
# set_border_color()
|
462
|
+
# set_bottom_color()
|
463
|
+
# set_top_color()
|
464
|
+
# set_left_color()
|
465
|
+
# set_right_color()
|
466
|
+
#
|
467
|
+
# The above methods can also be applied directly as properties. For example
|
468
|
+
# format.set_bold is equivalent to workbook.add_format(:bold => 1).
|
469
|
+
#
|
470
|
+
# ==COLOURS IN EXCEL
|
471
|
+
#
|
472
|
+
# Excel provides a colour palette of 56 colours. In WriteExcel these colours
|
473
|
+
# are accessed via their palette index in the range 8..63. This index is used
|
474
|
+
# to set the colour of fonts, cell patterns and cell borders. For example:
|
475
|
+
#
|
476
|
+
# format = workbook.add_format(
|
477
|
+
# :color => 12, # index for blue
|
478
|
+
# :font => 'Arial',
|
479
|
+
# :size => 12,
|
480
|
+
# :bold => 1
|
481
|
+
# )
|
482
|
+
#
|
483
|
+
# The most commonly used colours can also be accessed by name. The name acts
|
484
|
+
# as a simple alias for the colour index:
|
485
|
+
#
|
486
|
+
# black => 8
|
487
|
+
# blue => 12
|
488
|
+
# brown => 16
|
489
|
+
# cyan => 15
|
490
|
+
# gray => 23
|
491
|
+
# green => 17
|
492
|
+
# lime => 11
|
493
|
+
# magenta => 14
|
494
|
+
# navy => 18
|
495
|
+
# orange => 53
|
496
|
+
# pink => 33
|
497
|
+
# purple => 20
|
498
|
+
# red => 10
|
499
|
+
# silver => 22
|
500
|
+
# white => 9
|
501
|
+
# yellow => 13
|
502
|
+
#
|
503
|
+
# For example:
|
504
|
+
#
|
505
|
+
# font = workbook.add_format(:color => 'red')
|
506
|
+
#
|
507
|
+
# Users of VBA in Excel should note that the equivalent colour indices are in
|
508
|
+
# the range 1..56 instead of 8..63.
|
509
|
+
#
|
510
|
+
# If the default palette does not provide a required colour you can override
|
511
|
+
# one of the built-in values. This is achieved by using the set_custom_color()
|
512
|
+
# workbook method to adjust the RGB (red green blue) components of the colour:
|
513
|
+
#
|
514
|
+
# ferrari = workbook.set_custom_color(40, 216, 12, 12)
|
515
|
+
#
|
516
|
+
# format = workbook.add_format(
|
517
|
+
# :bg_color => ferrari,
|
518
|
+
# :pattern => 1,
|
519
|
+
# :border => 1
|
520
|
+
# )
|
521
|
+
#
|
522
|
+
# worksheet.write_blank('A1', format)
|
523
|
+
#
|
524
|
+
# You may also find the following links helpful:
|
525
|
+
#
|
526
|
+
# A detailed look at Excel's colour palette:
|
527
|
+
# http://www.mvps.org/dmcritchie/excel/colors.htm
|
528
|
+
#
|
529
|
+
# A decimal RGB chart: http://www.hypersolutions.org/pages/rgbdec.html
|
530
|
+
#
|
531
|
+
# A hex RGB chart: : http://www.hypersolutions.org/pages/rgbhex.html
|
532
|
+
#
|
533
|
+
# ==DATES AND TIME IN EXCEL
|
534
|
+
#
|
535
|
+
# There are two important things to understand about dates and times in Excel:
|
536
|
+
#
|
537
|
+
# 1. A date/time in Excel is a real number plus an Excel number format.
|
538
|
+
#
|
539
|
+
# 2. WriteExcel doesn't automatically convert date/time strings in write() to
|
540
|
+
# an Excel date/time.
|
541
|
+
#
|
542
|
+
# These two points are explained in more detail below along with some
|
543
|
+
# suggestions on how to convert times and dates to the required format.
|
544
|
+
#
|
545
|
+
# ===An Excel date/time is a number plus a format
|
546
|
+
#
|
547
|
+
# If you write a date string with write() then all you will get is a string:
|
548
|
+
#
|
549
|
+
# worksheet.write('A1', '02/03/04') # !! Writes a string not a date. !!
|
550
|
+
#
|
551
|
+
# Dates and times in Excel are represented by real numbers, for example
|
552
|
+
# "Jan 1 2001 12:30 AM" is represented by the number 36892.521.
|
553
|
+
#
|
554
|
+
# The integer part of the number stores the number of days since the epoch
|
555
|
+
# and the fractional part stores the percentage of the day.
|
556
|
+
#
|
557
|
+
# A date or time in Excel is just like any other number. To have the number
|
558
|
+
# display as a date you must apply an Excel number format to it. Here are
|
559
|
+
# some examples.
|
560
|
+
#
|
561
|
+
# #!/usr/bin/ruby -w
|
562
|
+
#
|
563
|
+
# require 'writeexcel'
|
564
|
+
#
|
565
|
+
# workbook = WriteExcel.new('date_examples.xls')
|
566
|
+
# worksheet = workbook.add_worksheet
|
567
|
+
#
|
568
|
+
# worksheet.set_column('A:A', 30) # For extra visibility.
|
569
|
+
#
|
570
|
+
# number = 39506.5
|
571
|
+
#
|
572
|
+
# worksheet.write('A1', number) # 39506.5
|
573
|
+
#
|
574
|
+
# format2 = workbook.add_format(:num_format => 'dd/mm/yy')
|
575
|
+
# worksheet.write('A2', number , format2); # 28/02/08
|
576
|
+
#
|
577
|
+
# format3 = workbook.add_format(:num_format => 'mm/dd/yy')
|
578
|
+
# worksheet.write('A3', number , format3); # 02/28/08
|
579
|
+
#
|
580
|
+
# format4 = workbook.add_format(:num_format => 'd-m-yyyy')
|
581
|
+
# worksheet.write('A4', .number , format4) # 28-2-2008
|
582
|
+
#
|
583
|
+
# format5 = workbook.add_format(:num_format => 'dd/mm/yy hh:mm')
|
584
|
+
# worksheet.write('A5', number , format5) # 28/02/08 12:00
|
585
|
+
#
|
586
|
+
# format6 = workbook.add_format(:num_format => 'd mmm yyyy')
|
587
|
+
# worksheet.write('A6', number , format6) # 28 Feb 2008
|
588
|
+
#
|
589
|
+
# format7 = workbook.add_format(:num_format => 'mmm d yyyy hh:mm AM/PM')
|
590
|
+
# worksheet.write('A7', number , format7) # Feb 28 2008 12:00 PM
|
591
|
+
#
|
592
|
+
# ===WriteExcel doesn't automatically convert date/time strings
|
593
|
+
#
|
594
|
+
# WriteExcel doesn't automatically convert input date strings into Excel's
|
595
|
+
# formatted date numbers due to the large number of possible date formats
|
596
|
+
# and also due to the possibility of misinterpretation.
|
597
|
+
#
|
598
|
+
# For example, does 02/03/04 mean March 2 2004, February 3 2004 or even March
|
599
|
+
# 4 2002.
|
600
|
+
#
|
601
|
+
# Therefore, in order to handle dates you will have to convert them to numbers
|
602
|
+
# and apply an Excel format. Some methods for converting dates are listed in
|
603
|
+
# the next section.
|
604
|
+
#
|
605
|
+
# The most direct way is to convert your dates to the ISO8601
|
606
|
+
# yyyy-mm-ddThh:mm:ss.sss date format and use the write_date_time() worksheet
|
607
|
+
# method:
|
608
|
+
#
|
609
|
+
# worksheet.write_date_time('A2', '2001-01-01T12:20', format)
|
610
|
+
#
|
611
|
+
# See the write_date_time() section of the documentation for more details.
|
612
|
+
#
|
613
|
+
# A general methodology for handling date strings with write_date_time() is:
|
614
|
+
#
|
615
|
+
# 1. Identify incoming date/time strings with a regex.
|
616
|
+
# 2. Extract the component parts of the date/time using the same regex.
|
617
|
+
# 3. Convert the date/time to the ISO8601 format.
|
618
|
+
# 4. Write the date/time using write_date_time() and a number format.
|
619
|
+
#
|
620
|
+
# Here is an example:
|
621
|
+
#
|
622
|
+
# #!/usr/bin/ruby -w
|
623
|
+
#
|
624
|
+
# require 'writeexcel'
|
625
|
+
#
|
626
|
+
# workbook = WriteExcel.new('example.xls')
|
627
|
+
# worksheet = workbook.add_worksheet
|
628
|
+
#
|
629
|
+
# # Set the default format for dates.
|
630
|
+
# date_format = workbook.add_format(:num_format => 'mmm d yyyy')
|
631
|
+
#
|
632
|
+
# # Increase column width to improve visibility of data.
|
633
|
+
# worksheet.set_column('A:C', 20)
|
634
|
+
#
|
635
|
+
# data = [
|
636
|
+
# %w(Item Cost Date),
|
637
|
+
# %w(Book 10 1/9/2007),
|
638
|
+
# %w(Beer 4 12/9/2007),
|
639
|
+
# %w(Bed 500 5/10/2007)
|
640
|
+
# ]
|
641
|
+
#
|
642
|
+
# # Simulate reading from a data source.
|
643
|
+
# row = 0
|
644
|
+
#
|
645
|
+
# data.each do |row_data|
|
646
|
+
# col = 0
|
647
|
+
# row_data.each do |item|
|
648
|
+
#
|
649
|
+
# # Match dates in the following formats: d/m/yy, d/m/yyyy
|
650
|
+
# if item =~ %r[^(\d{1,2})/(\d{1,2})/(\d{4})$]
|
651
|
+
# # Change to the date format required by write_date_time().
|
652
|
+
# date = sprintf "%4d-%02d-%02dT", $3, $2, $1
|
653
|
+
# worksheet.write_date_time(row, col, date, date_format)
|
654
|
+
# else
|
655
|
+
# # Just plain data
|
656
|
+
# worksheet.write(row, col, item)
|
657
|
+
# end
|
658
|
+
# col += 1
|
659
|
+
# end
|
660
|
+
# row += 1
|
661
|
+
# end
|
662
|
+
#
|
663
|
+
#--
|
664
|
+
# For a slightly more advanced solution you can modify the write() method to
|
665
|
+
# handle date formats of your choice via the add_write_handler() method. See
|
666
|
+
# the add_write_handler() section of the docs and the write_handler3.rb and
|
667
|
+
# write_handler4.rb programs in the examples directory of the distro.
|
668
|
+
#++
|
669
|
+
#
|
670
|
+
# ==OUTLINES AND GROUPING IN EXCEL
|
671
|
+
#
|
672
|
+
# Excel allows you to group rows or columns so that they can be hidden or
|
673
|
+
# displayed with a single mouse click. This feature is referred to as outlines.
|
674
|
+
#
|
675
|
+
# Outlines can reduce complex data down to a few salient sub-totals or
|
676
|
+
# summaries.
|
677
|
+
#
|
678
|
+
# This feature is best viewed in Excel but the following is an ASCII
|
679
|
+
# representation of what a worksheet with three outlines might look like. Rows
|
680
|
+
# 3-4 and rows 7-8 are grouped at level 2. Rows 2-9 are grouped at level 1.
|
681
|
+
# The lines at the left hand side are called outline level bars.
|
682
|
+
#
|
683
|
+
# ------------------------------------------
|
684
|
+
# 1 2 3 | | A | B | C | D | ...
|
685
|
+
# ------------------------------------------
|
686
|
+
# _ | 1 | A | | | | ...
|
687
|
+
# | _ | 2 | B | | | | ...
|
688
|
+
# | | | 3 | (C) | | | | ...
|
689
|
+
# | | | 4 | (D) | | | | ...
|
690
|
+
# | - | 5 | E | | | | ...
|
691
|
+
# | _ | 6 | F | | | | ...
|
692
|
+
# | | | 7 | (G) | | | | ...
|
693
|
+
# | | | 8 | (H) | | | | ...
|
694
|
+
# | - | 9 | I | | | | ...
|
695
|
+
# - | . | ... | ... | ... | ... | ...
|
696
|
+
#
|
697
|
+
# Clicking the minus sign on each of the level 2 outlines will collapse and
|
698
|
+
# hide the data as shown in the next figure. The minus sign changes to a plus
|
699
|
+
# sign to indicate that the data in the outline is hidden.
|
700
|
+
#
|
701
|
+
# ------------------------------------------
|
702
|
+
# 1 2 3 | | A | B | C | D | ...
|
703
|
+
# ------------------------------------------
|
704
|
+
# _ | 1 | A | | | | ...
|
705
|
+
# | | 2 | B | | | | ...
|
706
|
+
# | + | 5 | E | | | | ...
|
707
|
+
# | | 6 | F | | | | ...
|
708
|
+
# | + | 9 | I | | | | ...
|
709
|
+
# - | . | ... | ... | ... | ... | ...
|
710
|
+
#
|
711
|
+
# Clicking on the minus sign on the level 1 outline will collapse the
|
712
|
+
# remaining rows as follows:
|
713
|
+
#
|
714
|
+
# ------------------------------------------
|
715
|
+
# 1 2 3 | | A | B | C | D | ...
|
716
|
+
# ------------------------------------------
|
717
|
+
# | 1 | A | | | | ...
|
718
|
+
# + | . | ... | ... | ... | ... | ...
|
719
|
+
#
|
720
|
+
# Grouping in WriteExcel is achieved by setting the outline level via the
|
721
|
+
# set_row() and set_column() worksheet methods:
|
722
|
+
#
|
723
|
+
# set_row(row, height, format, hidden, level, collapsed)
|
724
|
+
# set_column(first_col, last_col, width, format, hidden, level, collapsed)
|
725
|
+
#
|
726
|
+
# The following example sets an outline level of 1 for rows 1 and 2
|
727
|
+
# (zero-indexed) and columns B to G. The parameters _height_ and _XF_ are
|
728
|
+
# assigned default values since they are undefined:
|
729
|
+
#
|
730
|
+
# worksheet.set_row(1, nil, nil, 0, 1)
|
731
|
+
# worksheet.set_row(2, nil, nil, 0, 1)
|
732
|
+
# worksheet.set_column('B:G', nil, nil, 0, 1)
|
733
|
+
#
|
734
|
+
# Excel allows up to 7 outline levels. Therefore the _level_ parameter should
|
735
|
+
# be in the range 0 <= _level_ <= 7.
|
736
|
+
#
|
737
|
+
# Rows and columns can be collapsed by setting the _hidden_ flag for the hidden
|
738
|
+
# rows/columns and setting the _collapsed_ flag for the row/column that has
|
739
|
+
# the collapsed + symbol:
|
740
|
+
#
|
741
|
+
# worksheet.set_row(1, nil, nil, 1, 1)
|
742
|
+
# worksheet.set_row(2, nil, nil, 1, 1)
|
743
|
+
# worksheet.set_row(3, nil, nil, 0, 0, 1) # Collapsed flag.
|
744
|
+
#
|
745
|
+
# worksheet.set_column('B:G', nil, nil, 1, 1)
|
746
|
+
# worksheet.set_column('H:H', nil, nil, 0, 0, 1) # Collapsed flag.
|
747
|
+
#
|
748
|
+
# Note: Setting the _collapsed_ flag is particularly important for
|
749
|
+
# compatibility with OpenOffice.org and Gnumeric.
|
750
|
+
#
|
751
|
+
# For a more complete example see the outline.rb
|
752
|
+
#--
|
753
|
+
# and outline_collapsed.rb
|
754
|
+
#++
|
755
|
+
# programs in the examples directory of the distro.
|
756
|
+
#
|
757
|
+
# Some additional outline properties can be set via the outline_settings()
|
758
|
+
# worksheet method, see above.
|
759
|
+
#
|
760
|
+
# ==DATA VALIDATION IN EXCEL
|
761
|
+
#
|
762
|
+
# Data validation is a feature of Excel which allows you to restrict the data
|
763
|
+
# that a users enters in a cell and to display help and warning messages. It
|
764
|
+
# also allows you to restrict input to values in a drop down list.
|
765
|
+
#
|
766
|
+
# A typical use case might be to restrict data in a cell to integer values in
|
767
|
+
# a certain range, to provide a help message to indicate the required value and
|
768
|
+
# to issue a warning if the input data doesn't meet the stated criteria.
|
769
|
+
# In WriteExcel we could do that as follows:
|
770
|
+
#
|
771
|
+
# worksheet.data_validation('B3',
|
772
|
+
# {
|
773
|
+
# :validate => 'integer',
|
774
|
+
# :criteria => 'between',
|
775
|
+
# :minimum => 1,
|
776
|
+
# :maximum => 100,
|
777
|
+
# :input_title => 'Input an integer:',
|
778
|
+
# :input_message => 'Between 1 and 100',
|
779
|
+
# :error_message => 'Sorry, try again.'
|
780
|
+
# })
|
781
|
+
#
|
782
|
+
# The above example would look like this in Excel:
|
783
|
+
# http://homepage.eircom.net/~jmcnamara/perl/data_validation.jpg.
|
784
|
+
#
|
785
|
+
# For more information on data validation see the following Microsoft
|
786
|
+
# support article "Description and examples of data validation in Excel":
|
787
|
+
# http://support.microsoft.com/kb/211485.
|
788
|
+
#
|
789
|
+
# ==FORMULAS AND FUNCTIONS IN EXCEL
|
790
|
+
#
|
791
|
+
# ===Caveats
|
792
|
+
#
|
793
|
+
# The first thing to note is that there are still some outstanding issues
|
794
|
+
# with the implementation of formulas and functions:
|
795
|
+
#
|
796
|
+
# 1. Writing a formula is much slower than writing the equivalent string.
|
797
|
+
# 2. You cannot use array constants, i.e. {1;2;3}, in functions.
|
798
|
+
# 3. Unary minus isn't supported.
|
799
|
+
# 4. Whitespace is not preserved around operators.
|
800
|
+
# 5. Named ranges are not supported.
|
801
|
+
# 6. Array formulas are not supported.
|
802
|
+
#
|
803
|
+
# However, these constraints will be removed in future versions. They are
|
804
|
+
# here because of a trade-off between features and time. Also, it is possible
|
805
|
+
# to work around issue 1 using the store_formula() and repeat_formula()
|
806
|
+
# methods as described later in this section.
|
807
|
+
#
|
808
|
+
# ===Introduction
|
809
|
+
#
|
810
|
+
# The following is a brief introduction to formulas and functions in Excel
|
811
|
+
# and WriteExcel.
|
812
|
+
#
|
813
|
+
# A formula is a string that begins with an equals sign:
|
814
|
+
#
|
815
|
+
# '=A1+B1'
|
816
|
+
# '=AVERAGE(1, 2, 3)'
|
817
|
+
#
|
818
|
+
# The formula can contain numbers, strings, boolean values, cell references,
|
819
|
+
# cell ranges and functions. Named ranges are not supported. Formulas should
|
820
|
+
# be written as they appear in Excel, that is cells and functions must be
|
821
|
+
# in uppercase.
|
822
|
+
#
|
823
|
+
# Cells in Excel are referenced using the A1 notation system where the
|
824
|
+
# column is designated by a letter and the row by a number. Columns
|
825
|
+
# range from A to IV i.e. 0 to 255, rows range from 1 to 65536.
|
826
|
+
#--
|
827
|
+
# The Spreadsheet::WriteExcel::Utility module that is included in the distro
|
828
|
+
# contains helper functions for dealing with A1 notation, for example:
|
829
|
+
#
|
830
|
+
# use Spreadsheet::WriteExcel::Utility;
|
831
|
+
#
|
832
|
+
# ($row, $col) = xl_cell_to_rowcol('C2'); # (1, 2)
|
833
|
+
# $str = xl_rowcol_to_cell(1, 2); # C2
|
834
|
+
#++
|
835
|
+
#
|
836
|
+
# The Excel $ notation in cell references is also supported. This allows you
|
837
|
+
# to specify whether a row or column is relative or absolute. This only has
|
838
|
+
# an effect if the cell is copied. The following examples show relative and
|
839
|
+
# absolute values.
|
840
|
+
#
|
841
|
+
# '=A1' # Column and row are relative
|
842
|
+
# '=$A1' # Column is absolute and row is relative
|
843
|
+
# '=A$1' # Column is relative and row is absolute
|
844
|
+
# '=$A$1' # Column and row are absolute
|
845
|
+
#
|
846
|
+
# Formulas can also refer to cells in other worksheets of the current
|
847
|
+
# workbook. For example:
|
848
|
+
#
|
849
|
+
# '=Sheet2!A1'
|
850
|
+
# '=Sheet2!A1:A5'
|
851
|
+
# '=Sheet2:Sheet3!A1'
|
852
|
+
# '=Sheet2:Sheet3!A1:A5'
|
853
|
+
# q{='Test Data'!A1}
|
854
|
+
# q{='Test Data1:Test Data2'!A1}
|
855
|
+
#
|
856
|
+
# The sheet reference and the cell reference are separated by ! the exclamation
|
857
|
+
# mark symbol. If worksheet names contain spaces, commas o parentheses then Excel
|
858
|
+
# requires that the name is enclosed in single quotes as shown in the last two
|
859
|
+
# examples above. In order to avoid using a lot of escape characters you can
|
860
|
+
# use the quote operator %q{} to protect the quotes. Only valid sheet names that
|
861
|
+
# have been added using the add_worksheet() method can be used in formulas.
|
862
|
+
# You cannot reference external workbooks.
|
863
|
+
#
|
864
|
+
# The following table lists the operators that are available in Excel's formulas.
|
865
|
+
# The majority of the operators are the same as Perl's, differences are indicated:
|
866
|
+
#
|
867
|
+
# Arithmetic operators:
|
868
|
+
# =====================
|
869
|
+
# Operator Meaning Example
|
870
|
+
# + Addition 1+2
|
871
|
+
# - Subtraction 2-1
|
872
|
+
# * Multiplication 2*3
|
873
|
+
# / Division 1/4
|
874
|
+
# ^ Exponentiation 2^3 # Equivalent to **
|
875
|
+
# - Unary minus -(1+2) # Not yet supported
|
876
|
+
# % Percent (Not modulus) 13% # Not supported, [1]
|
877
|
+
#
|
878
|
+
# Comparison operators:
|
879
|
+
# =====================
|
880
|
+
# Operator Meaning Example
|
881
|
+
# = Equal to A1 = B1 # Equivalent to ==
|
882
|
+
# <> Not equal to A1 <> B1 # Equivalent to !=
|
883
|
+
# > Greater than A1 > B1
|
884
|
+
# < Less than A1 < B1
|
885
|
+
# >= Greater than or equal to A1 >= B1
|
886
|
+
# <= Less than or equal to A1 <= B1
|
887
|
+
#
|
888
|
+
# String operator:
|
889
|
+
# ================
|
890
|
+
# Operator Meaning Example
|
891
|
+
# & Concatenation "Hello " & "World!" # [2]
|
892
|
+
#
|
893
|
+
# Reference operators:
|
894
|
+
# ====================
|
895
|
+
# Operator Meaning Example
|
896
|
+
# : Range operator A1:A4 # [3]
|
897
|
+
# , Union operator SUM(1, 2+2, B3) # [4]
|
898
|
+
#
|
899
|
+
# Notes:
|
900
|
+
# [1]: You can get a percentage with formatting and modulus with MOD().
|
901
|
+
# [2]: Equivalent to ("Hello " . "World!") in Perl.
|
902
|
+
# [3]: This range is equivalent to cells A1, A2, A3 and A4.
|
903
|
+
# [4]: The comma behaves like the list separator in Perl.
|
904
|
+
#
|
905
|
+
# The range and comma operators can have different symbols in non-English
|
906
|
+
# versions of Excel. These will be supported in a later version of WriteExcel.
|
907
|
+
# European users of Excel take note:
|
908
|
+
#
|
909
|
+
# worksheet.write('A1', '=SUM(1; 2; 3)') # Wrong!!
|
910
|
+
# worksheet.write('A1', '=SUM(1, 2, 3)') # Okay
|
911
|
+
#
|
912
|
+
# The following table lists all of the core functions supported by
|
913
|
+
# Excel 5 and WriteExcel. Any additional functions that are available through
|
914
|
+
# the "Analysis ToolPak" or other add-ins are not supported. These functions
|
915
|
+
# have all been tested to verify that they work.
|
916
|
+
#
|
917
|
+
# ABS DB INDIRECT NORMINV SLN
|
918
|
+
# ACOS DCOUNT INFO NORMSDIST SLOPE
|
919
|
+
# ACOSH DCOUNTA INT NORMSINV SMALL
|
920
|
+
# ADDRESS DDB INTERCEPT NOT SQRT
|
921
|
+
# AND DEGREES IPMT NOW STANDARDIZE
|
922
|
+
# AREAS DEVSQ IRR NPER STDEV
|
923
|
+
# ASIN DGET ISBLANK NPV STDEVP
|
924
|
+
# ASINH DMAX ISERR ODD STEYX
|
925
|
+
# ATAN DMIN ISERROR OFFSET SUBSTITUTE
|
926
|
+
# ATAN2 DOLLAR ISLOGICAL OR SUBTOTAL
|
927
|
+
# ATANH DPRODUCT ISNA PEARSON SUM
|
928
|
+
# AVEDEV DSTDEV ISNONTEXT PERCENTILE SUMIF
|
929
|
+
# AVERAGE DSTDEVP ISNUMBER PERCENTRANK SUMPRODUCT
|
930
|
+
# BETADIST DSUM ISREF PERMUT SUMSQ
|
931
|
+
# BETAINV DVAR ISTEXT PI SUMX2MY2
|
932
|
+
# BINOMDIST DVARP KURT PMT SUMX2PY2
|
933
|
+
# CALL ERROR.TYPE LARGE POISSON SUMXMY2
|
934
|
+
# CEILING EVEN LEFT POWER SYD
|
935
|
+
# CELL EXACT LEN PPMT T
|
936
|
+
# CHAR EXP LINEST PROB TAN
|
937
|
+
# CHIDIST EXPONDIST LN PRODUCT TANH
|
938
|
+
# CHIINV FACT LOG PROPER TDIST
|
939
|
+
# CHITEST FALSE LOG10 PV TEXT
|
940
|
+
# CHOOSE FDIST LOGEST QUARTILE TIME
|
941
|
+
# CLEAN FIND LOGINV RADIANS TIMEVALUE
|
942
|
+
# CODE FINV LOGNORMDIST RAND TINV
|
943
|
+
# COLUMN FISHER LOOKUP RANK TODAY
|
944
|
+
# COLUMNS FISHERINV LOWER RATE TRANSPOSE
|
945
|
+
# COMBIN FIXED MATCH REGISTER.ID TREND
|
946
|
+
# CONCATENATE FLOOR MAX REPLACE TRIM
|
947
|
+
# CONFIDENCE FORECAST MDETERM REPT TRIMMEAN
|
948
|
+
# CORREL FREQUENCY MEDIAN RIGHT TRUE
|
949
|
+
# COS FTEST MID ROMAN TRUNC
|
950
|
+
# COSH FV MIN ROUND TTEST
|
951
|
+
# COUNT GAMMADIST MINUTE ROUNDDOWN TYPE
|
952
|
+
# COUNTA GAMMAINV MINVERSE ROUNDUP UPPER
|
953
|
+
# COUNTBLANK GAMMALN MIRR ROW VALUE
|
954
|
+
# COUNTIF GEOMEAN MMULT ROWS VAR
|
955
|
+
# COVAR GROWTH MOD RSQ VARP
|
956
|
+
# CRITBINOM HARMEAN MODE SEARCH VDB
|
957
|
+
# DATE HLOOKUP MONTH SECOND VLOOKUP
|
958
|
+
# DATEVALUE HOUR N SIGN WEEKDAY
|
959
|
+
# DAVERAGE HYPGEOMDIST NA SIN WEIBULL
|
960
|
+
# DAY IF NEGBINOMDIST SINH YEAR
|
961
|
+
# DAYS360 INDEX NORMDIST SKEW ZTEST
|
962
|
+
#
|
963
|
+
#--
|
964
|
+
# You can also modify the module to support function names in the following
|
965
|
+
# languages: German, French, Spanish, Portuguese, Dutch, Finnish, Italian and
|
966
|
+
# Swedish. See the function_locale.pl program in the examples directory of the distro.
|
967
|
+
#++
|
968
|
+
#
|
969
|
+
# For a general introduction to Excel's formulas and an explanation of the
|
970
|
+
# syntax of the function refer to the Excel help files or the following:
|
971
|
+
# http://office.microsoft.com/en-us/assistance/CH062528031033.aspx.
|
972
|
+
#
|
973
|
+
# If your formula doesn't work in Spreadsheet::WriteExcel try the following:
|
974
|
+
#
|
975
|
+
# 1. Verify that the formula works in Excel (or Gnumeric or OpenOffice.org).
|
976
|
+
# 2. Ensure that it isn't on the Caveats list shown above.
|
977
|
+
# 3. Ensure that cell references and formula names are in uppercase.
|
978
|
+
# 4. Ensure that you are using ':' as the range operator, A1:A4.
|
979
|
+
# 5. Ensure that you are using ',' as the union operator, SUM(1,2,3).
|
980
|
+
# 6. Ensure that the function is in the above table.
|
981
|
+
#
|
982
|
+
# If you go through steps 1-6 and you still have a problem, mail me.
|
983
|
+
# Improving performance when working with formulas
|
984
|
+
#
|
985
|
+
# Writing a large number of formulas with Spreadsheet::WriteExcel can be slow.
|
986
|
+
# This is due to the fact that each formula has to be parsed and with the
|
987
|
+
# current implementation this is computationally expensive.
|
988
|
+
#
|
989
|
+
# However, in a lot of cases the formulas that you write will be quite
|
990
|
+
# similar, for example:
|
991
|
+
#
|
992
|
+
# worksheet.write_formula('B1', '=A1 * 3 + 50', format)
|
993
|
+
# worksheet.write_formula('B2', '=A2 * 3 + 50', format)
|
994
|
+
# ...
|
995
|
+
# ...
|
996
|
+
# worksheet.write_formula('B99', '=A999 * 3 + 50', format)
|
997
|
+
# worksheet.write_formula('B1000', '=A1000 * 3 + 50', format)
|
998
|
+
#
|
999
|
+
# In this example the cell reference changes in iterations from A1 to A1000.
|
1000
|
+
# The parser treats this variable as a token and arranges it according to
|
1001
|
+
# predefined rules. However, since the parser is oblivious to the value of
|
1002
|
+
# the token, it is essentially performing the same calculation 1000 times.
|
1003
|
+
# This is inefficient.
|
1004
|
+
#
|
1005
|
+
# The way to avoid this inefficiency and thereby speed up the writing of
|
1006
|
+
# formulas is to parse the formula once and then repeatedly substitute
|
1007
|
+
# similar tokens.
|
1008
|
+
#
|
1009
|
+
# A formula can be parsed and stored via the store_formula() worksheet method.
|
1010
|
+
# You can then use the repeat_formula() method to substitute _pattern_,
|
1011
|
+
# _replace_ pairs in the stored formula:
|
1012
|
+
#
|
1013
|
+
# formula = worksheet.store_formula('=A1 * 3 + 50')
|
1014
|
+
#
|
1015
|
+
# (0...1000).each do |row|
|
1016
|
+
# worksheet.repeat_formula(row, 1, formula, format, 'A1', 'A' + (row +1).to_s)
|
1017
|
+
# end
|
1018
|
+
#
|
1019
|
+
# On an arbitrary test machine this method was 10 times faster than the
|
1020
|
+
# brute force method shown above.
|
1021
|
+
#
|
1022
|
+
# It should be noted however that the overall speed of direct formula parsing
|
1023
|
+
# will be improved in a future version.
|
1024
|
+
#
|
1025
|
+
# ==Chart
|
1026
|
+
#
|
1027
|
+
# ===Synopsis(Chart)
|
1028
|
+
#
|
1029
|
+
# To create a simple Excel file with a chart using WriteExcel:
|
1030
|
+
#
|
1031
|
+
# #!/usr/bin/ruby -w
|
1032
|
+
#
|
1033
|
+
# require 'writeexcel'
|
1034
|
+
#
|
1035
|
+
# workbook = WriteExcel.new('chart.xls')
|
1036
|
+
# worksheet = workbook.add_worksheet
|
1037
|
+
#
|
1038
|
+
# chart = workbook.add_chart(:type => Chart::Column)
|
1039
|
+
#
|
1040
|
+
# # Configure the chart.
|
1041
|
+
# chart.add_series(
|
1042
|
+
# :categories => '=Sheet1!$A$2:$A$7',
|
1043
|
+
# :values => '=Sheet1!$B$2:$B$7'
|
1044
|
+
# )
|
1045
|
+
#
|
1046
|
+
# # Add the data to the worksheet the chart refers to.
|
1047
|
+
# data = [
|
1048
|
+
# [ 'Category', 2, 3, 4, 5, 6, 7 ],
|
1049
|
+
# [ 'Value', 1, 4, 5, 2, 1, 5 ]
|
1050
|
+
# ]
|
1051
|
+
#
|
1052
|
+
# worksheet.write('A1', data)
|
1053
|
+
#
|
1054
|
+
# workbook.close
|
1055
|
+
#
|
1056
|
+
# ===DESCRIPTION(Chart)
|
1057
|
+
#
|
1058
|
+
# The Chart module is an abstract base class for modules that implement charts
|
1059
|
+
# in WriteExcel. The information below is applicable to all of the available
|
1060
|
+
# subclasses.
|
1061
|
+
#
|
1062
|
+
# The Chart module isn't used directly, a chart object is created via the
|
1063
|
+
# Workbook add_chart() method where the chart type is specified:
|
1064
|
+
#
|
1065
|
+
# chart = workbook.add_chart(:type => Chart::Column)
|
1066
|
+
#
|
1067
|
+
# Currently the supported chart types are:
|
1068
|
+
#
|
1069
|
+
# * Chart::Column: Creates a column style (histogram) chart. See Column.
|
1070
|
+
# * Chart::Bar: Creates a Bar style (transposed histogram) chart. See Bar.
|
1071
|
+
# * Chart::Line: Creates a Line style chart. See Line.
|
1072
|
+
# * Chart::Area: Creates an Area (filled line) style chart. See Area.
|
1073
|
+
# * Chart::Scatter: Creates an Scatter style chart. See Scatter.
|
1074
|
+
# * Chart::Stock: Creates an Stock style chart. See Stock.
|
1075
|
+
#
|
1076
|
+
# More chart types will be supported in time. See the "TODO" section.
|
1077
|
+
#
|
1078
|
+
# === Chart names and links
|
1079
|
+
#
|
1080
|
+
# The add_series()), set_x_axis(), set_y_axis() and set_title() methods all
|
1081
|
+
# support a name property. In general these names can be either a static
|
1082
|
+
# string or a link to a worksheet cell. If you choose to use the name_formula
|
1083
|
+
# property to specify a link then you should also the name property.
|
1084
|
+
# This isn't strictly required by Excel but some third party applications
|
1085
|
+
# expect it to be present.
|
1086
|
+
#
|
1087
|
+
# chartl.set_title(
|
1088
|
+
# :name => 'Year End Results',
|
1089
|
+
# :name_formula => '=Sheet1!$C$1'
|
1090
|
+
# )
|
1091
|
+
#
|
1092
|
+
# These links should be used sparingly since they aren't commonly
|
1093
|
+
# used in Excel charts.
|
1094
|
+
#
|
1095
|
+
# === Chart names and Unicode
|
1096
|
+
#
|
1097
|
+
# The add_series()), set_x_axis(), set_y_axis() and set_title() methods all
|
1098
|
+
# support a name property. These names can be UTF8 strings.
|
1099
|
+
#
|
1100
|
+
# This methodology is explained in the "UNICODE IN EXCEL" section of WriteExcel
|
1101
|
+
# but is semi-deprecated. If you are using Unicode the easiest option is to
|
1102
|
+
# just use UTF8.
|
1103
|
+
#
|
1104
|
+
# === TODO(Chart)
|
1105
|
+
#
|
1106
|
+
# Charts in WriteExcel are a work in progress. More chart types and
|
1107
|
+
# features will be added in time. Please be patient. Even a small feature
|
1108
|
+
# can take a week or more to implement, test and document.
|
1109
|
+
#
|
1110
|
+
# Features that are on the TODO list and will be added are:
|
1111
|
+
#
|
1112
|
+
# * Additional chart types. Stock, Pie and Scatter charts are next in line.
|
1113
|
+
# Send an email if you are interested in other types and they will be
|
1114
|
+
# added to the queue.
|
1115
|
+
# * Colours and formatting options. For now you will have to make do
|
1116
|
+
# with the default Excel colours and formats.
|
1117
|
+
# * Axis controls, gridlines.
|
1118
|
+
# * Embedded data in charts for third party application support.
|
1119
|
+
#
|
1120
|
+
# == KNOWN ISSUES(Chart)
|
1121
|
+
#
|
1122
|
+
# * Currently charts don't contain embedded data from which the charts
|
1123
|
+
# can be rendered. Excel and most other third party applications ignore
|
1124
|
+
# this and read the data via the links that have been specified. However,
|
1125
|
+
# some applications may complain or not render charts correctly. The
|
1126
|
+
# preview option in Mac OS X is an known example. This will be fixed
|
1127
|
+
# in a later release.
|
1128
|
+
# * When there are several charts with titles set in a workbook some of
|
1129
|
+
# the titles may display at a font size of 10 instead of the default
|
1130
|
+
# 12 until another chart with the title set is viewed.
|
1131
|
+
#
|
1132
|
+
class WriteExcel < Workbook
|
1133
|
+
VERSION = "0.3.0"
|
1134
|
+
end
|