1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
|
#!/usr/bin/perl -w
###############################################################################
#
# Example of how to add data validation and dropdown lists to a
# Spreadsheet::WriteExcel file.
#
# Data validation is a feature of Excel which allows you to restrict the data
# that a users enters in a cell and to display help and warning messages. It
# also allows you to restrict input to values in a drop down list.
#
# reverse(''), August 2008, John McNamara, jmcnamara@cpan.org
#
use strict;
use Spreadsheet::WriteExcel;
my $workbook = Spreadsheet::WriteExcel->new('data_validate.xls');
my $worksheet = $workbook->add_worksheet();
# Add a format for the header cells.
my $header_format = $workbook->add_format(
border => 1,
bg_color => 43,
bold => 1,
text_wrap => 1,
valign => 'vcenter',
indent => 1,
);
# Set up layout of the worksheet.
$worksheet->set_column('A:A', 64);
$worksheet->set_column('B:B', 15);
$worksheet->set_column('D:D', 15);
$worksheet->set_row(0, 36);
$worksheet->set_selection('B3');
# Write the header cells and some data that will be used in the examples.
my $row = 0;
my $txt;
my $heading1 = 'Some examples of data validation in Spreadsheet::WriteExcel';
my $heading2 = 'Enter values in this column';
my $heading3 = 'Sample Data';
$worksheet->write('A1', $heading1, $header_format);
$worksheet->write('B1', $heading2, $header_format);
$worksheet->write('D1', $heading3, $header_format);
$worksheet->write('D3', ['Integers', 1, 10]);
$worksheet->write('D4', ['List data', 'open', 'high', 'close']);
$worksheet->write('D5', ['Formula', '=AND(F5=50,G5=60)', 50, 60]);
#
# Example 1. Limiting input to an integer in a fixed range.
#
$txt = 'Enter an integer between 1 and 10';
$row += 2;
$worksheet->write($row, 0, $txt);
$worksheet->data_validation($row, 1,
{
validate => 'integer',
criteria => 'between',
minimum => 1,
maximum => 10,
});
#
# Example 2. Limiting input to an integer outside a fixed range.
#
$txt = 'Enter an integer that is not between 1 and 10 (using cell references)';
$row += 2;
$worksheet->write($row, 0, $txt);
$worksheet->data_validation($row, 1,
{
validate => 'integer',
criteria => 'not between',
minimum => '=E3',
maximum => '=F3',
});
#
# Example 3. Limiting input to an integer greater than a fixed value.
#
$txt = 'Enter an integer greater than 0';
$row += 2;
$worksheet->write($row, 0, $txt);
$worksheet->data_validation($row, 1,
{
validate => 'integer',
criteria => '>',
value => 0,
});
#
# Example 4. Limiting input to an integer less than a fixed value.
#
$txt = 'Enter an integer less than 10';
$row += 2;
$worksheet->write($row, 0, $txt);
$worksheet->data_validation($row, 1,
{
validate => 'integer',
criteria => '<',
value => 10,
});
#
# Example 5. Limiting input to a decimal in a fixed range.
#
$txt = 'Enter a decimal between 0.1 and 0.5';
$row += 2;
$worksheet->write($row, 0, $txt);
$worksheet->data_validation($row, 1,
{
validate => 'decimal',
criteria => 'between',
minimum => 0.1,
maximum => 0.5,
});
#
# Example 6. Limiting input to a value in a dropdown list.
#
$txt = 'Select a value from a drop down list';
$row += 2;
$worksheet->write($row, 0, $txt);
$worksheet->data_validation($row, 1,
{
validate => 'list',
source => ['open', 'high', 'close'],
});
#
# Example 6. Limiting input to a value in a dropdown list.
#
$txt = 'Select a value from a drop down list (using a cell range)';
$row += 2;
$worksheet->write($row, 0, $txt);
$worksheet->data_validation($row, 1,
{
validate => 'list',
source => '=E4:G4',
});
#
# Example 7. Limiting input to a date in a fixed range.
#
$txt = 'Enter a date between 1/1/2008 and 12/12/2008';
$row += 2;
$worksheet->write($row, 0, $txt);
$worksheet->data_validation($row, 1,
{
validate => 'date',
criteria => 'between',
minimum => '2008-01-01T',
maximum => '2008-12-12T',
});
#
# Example 8. Limiting input to a time in a fixed range.
#
$txt = 'Enter a time between 6:00 and 12:00';
$row += 2;
$worksheet->write($row, 0, $txt);
$worksheet->data_validation($row, 1,
{
validate => 'time',
criteria => 'between',
minimum => 'T06:00',
maximum => 'T12:00',
});
#
# Example 9. Limiting input to a string greater than a fixed length.
#
$txt = 'Enter a string longer than 3 characters';
$row += 2;
$worksheet->write($row, 0, $txt);
$worksheet->data_validation($row, 1,
{
validate => 'length',
criteria => '>',
value => 3,
});
#
# Example 10. Limiting input based on a formula.
#
$txt = 'Enter a value if the following is true "=AND(F5=50,G5=60)"';
$row += 2;
$worksheet->write($row, 0, $txt);
$worksheet->data_validation($row, 1,
{
validate => 'custom',
value => '=AND(F5=50,G5=60)',
});
#
# Example 11. Displaying and modify data validation messages.
#
$txt = 'Displays a message when you select the cell';
$row += 2;
$worksheet->write($row, 0, $txt);
$worksheet->data_validation($row, 1,
{
validate => 'integer',
criteria => 'between',
minimum => 1,
maximum => 100,
input_title => 'Enter an integer:',
input_message => 'between 1 and 100',
});
#
# Example 12. Displaying and modify data validation messages.
#
$txt = 'Display a custom error message when integer isn\'t between 1 and 100';
$row += 2;
$worksheet->write($row, 0, $txt);
$worksheet->data_validation($row, 1,
{
validate => 'integer',
criteria => 'between',
minimum => 1,
maximum => 100,
input_title => 'Enter an integer:',
input_message => 'between 1 and 100',
error_title => 'Input value is not valid!',
error_message => 'It should be an integer between 1 and 100',
});
#
# Example 13. Displaying and modify data validation messages.
#
$txt = 'Display a custom information message when integer isn\'t between 1 and 100';
$row += 2;
$worksheet->write($row, 0, $txt);
$worksheet->data_validation($row, 1,
{
validate => 'integer',
criteria => 'between',
minimum => 1,
maximum => 100,
input_title => 'Enter an integer:',
input_message => 'between 1 and 100',
error_title => 'Input value is not valid!',
error_message => 'It should be an integer between 1 and 100',
error_type => 'information',
});
__END__
|