Skip to content

Commit

Permalink
Add support for condtional format icon sets.
Browse files Browse the repository at this point in the history
Issue #116
  • Loading branch information
jmcnamara committed Sep 15, 2017
1 parent dbeef92 commit cf0af06
Show file tree
Hide file tree
Showing 10 changed files with 1,450 additions and 67 deletions.
103 changes: 62 additions & 41 deletions examples/conditional_format.pl
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,6 @@

);

# Blue fill with dark blue text.
my $format3 = $workbook->add_format(
bg_color => '#C6CEFF',
color => '#0000FF',

);

# Some sample data to run the conditional formatting against.
my $data = [
[ 34, 72, 38, 30, 75, 48, 75, 66, 84, 86 ],
Expand Down Expand Up @@ -242,19 +235,18 @@
#
$caption = 'Examples of color scales and data bars. Default colors.';

# Use different sample data for examples 7 and 8
my $data7 = [ 1 .. 12 ];
$data = [ 1 .. 12 ];

$worksheet7->write( 'A1', $caption );

$worksheet7->write ( 'B2', "2 Color Scale" );
$worksheet7->write_col( 'B3', $data7 );
$worksheet7->write_col( 'B3', $data );

$worksheet7->write ( 'D2', "3 Color Scale" );
$worksheet7->write_col( 'D3', $data7 );
$worksheet7->write_col( 'D3', $data );

$worksheet7->write ( 'F2', "Data Bars" );
$worksheet7->write_col( 'F3', $data7 );
$worksheet7->write_col( 'F3', $data );


$worksheet7->conditional_formatting( 'B3:B14',
Expand Down Expand Up @@ -282,16 +274,18 @@
#
$caption = 'Examples of color scales and data bars. Modified colors.';

$data = [ 1 .. 12 ];

$worksheet8->write( 'A1', $caption );

$worksheet8->write ( 'B2', "2 Color Scale" );
$worksheet8->write_col( 'B3', $data7 );
$worksheet8->write_col( 'B3', $data );

$worksheet8->write ( 'D2', "3 Color Scale" );
$worksheet8->write_col( 'D3', $data7 );
$worksheet8->write_col( 'D3', $data );

$worksheet8->write ( 'F2', "Data Bars" );
$worksheet8->write_col( 'F3', $data7 );
$worksheet8->write_col( 'F3', $data );


$worksheet8->conditional_formatting( 'B3:B14',
Expand Down Expand Up @@ -320,48 +314,75 @@
);



###############################################################################
#
# Example 9
# Example 9.
#
$caption = 'Cells with values >= 100 are always in blue. '
. 'Otherwise, cells with values >= 50 are in light red '
. 'and values < 50 are in light green.';
$caption = 'Examples of conditional formats with icon sets.';

$data = [
[ 1, 2, 3 ],
[ 1, 2, 3 ],
[ 1, 2, 3 ],
[ 1, 2, 3 ],
[ 1, 2, 3, 4 ],
[ 1, 2, 3, 4, 5 ],
[ 1, 2, 3, 4, 5 ],
];

# Write the data.
$worksheet9->write( 'A1', $caption );
$worksheet9->write_col( 'B3', $data );

# Write a conditional format over a range.
# Use stopIfTrue to prevent previous formats from being used
# if the conditions of this format are met.
$worksheet9->conditional_formatting( 'B3:K12',
$worksheet9->conditional_formatting( 'B3:D3',
{
type => 'cell',
criteria => '>=',
value => 100,
format => $format3,
stop_if_true => 1,
type => 'icon_set',
icon_style => '3_traffic_lights',
}
);

# Write another conditional format over the same range.
$worksheet9->conditional_formatting( 'B3:K12',
$worksheet9->conditional_formatting( 'B4:D4',
{
type => 'cell',
criteria => '>=',
value => 50,
format => $format1,
type => 'icon_set',
icon_style => '3_traffic_lights',
reverse_icons => 1,
}
);

# Write another conditional format over the same range.
$worksheet9->conditional_formatting( 'B3:K12',
$worksheet9->conditional_formatting( 'B5:D5',
{
type => 'cell',
criteria => '<',
value => 50,
format => $format2,
type => 'icon_set',
icon_style => '3_traffic_lights',
icons_only => 1,
}
);

$worksheet9->conditional_formatting( 'B6:D6',
{
type => 'icon_set',
icon_style => '3_arrows',
}
);

$worksheet9->conditional_formatting( 'B7:E8',
{
type => 'icon_set',
icon_style => '4_arrows',
}
);

$worksheet9->conditional_formatting( 'B8:F8',
{
type => 'icon_set',
icon_style => '5_arrows',
}
);


$worksheet9->conditional_formatting( 'B9:F9',
{
type => 'icon_set',
icon_style => '5_ratings',
}
);

Expand Down
117 changes: 116 additions & 1 deletion lib/Excel/Writer/XLSX.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4639,6 +4639,10 @@ Other, less commonly used parameters are:
max_color
bar_color
stop_if_true
icon_style
icons
reverse_icons
icons_only
Additional parameters which are used for specific conditional format types are shown in the relevant sections below.
Expand Down Expand Up @@ -4693,8 +4697,13 @@ The C<type> parameter is used to set the type of conditional formatting that you
formula criteria
icon_set icon_style
reverse_icons
icons
icons_only
All conditional formatting types have a C<format> parameter, see below. Other types and parameters such as icon sets will be added in time.
All conditional formatting types, apart from C<icon_set> have a C<format> parameter, see below.
=head2 type => 'cell'
Expand Down Expand Up @@ -5060,6 +5069,103 @@ The C<formula> type is used to specify a conditional format based on a user defi
The formula is specified in the C<criteria>.
=head2 type => 'icon_set'
The C<icon_set> type is used to specify a conditional format with a set of icons such as traffic lights or arrows:
$worksheet->conditional_formatting( 'A1:C1',
{
type => 'icon_set',
icon_style => '3_traffic_lights',
}
);
The icon set style is specified by the C<icon_style> parameter. Valid options are:
3_arrows
3_arrows_gray
3_flags
3_signs
3_symbols
3_symbols_circled
3_traffic_lights
3_traffic_lights_rimmed
4_arrows
4_arrows_gray
4_ratings
4_red_to_black
4_traffic_lights
5_arrows
5_arrows_gray
5_quarters
5_ratings
The criteria, type and value of each icon can be specified using the C<icon> array of hash refs with optional C<criteria>, C<type> and C<value> parameters:
$worksheet->conditional_formatting( 'A1:D1',
{
type => 'icon_set',
icon_style => '4_red_to_black',
icons => [ {criteria => '>', type => 'number', value => 90},
{criteria => '>=', type => 'percentile', value => 50},
{criteria => '>', type => 'percent', value => 25},
],
}
);
The C<icons criteria> parameter should be either C<< >= >> or C<< > >>. The default C<criteria> is C<< >= >>.
The C<icons type> parameter should be one of the following values:
number
percentile
percent
formula
The default C<type> is C<percent>.
The C<icons value> parameter can be a value or formula:
$worksheet->conditional_formatting( 'A1:D1',
{
type => 'icon_set',
icon_style => '4_red_to_black',
icons => [ {value => 90},
{value => 50},
{value => 25},
],
}
);
Note: The C<icons> parameters should start with the highest value and with each subsequent one being lower. The default C<value> is C<(n * 100) / number_of_icons>. The lowest number icon in an icon set has properties defined by Excel. Therefore in a C<n> icon set, there is no C<n-1> hash of parameters.
The order of the icons can be reversed using the C<reverse_icons> parameter:
$worksheet->conditional_formatting( 'A1:C1',
{
type => 'icon_set',
icon_style => '3_arrows',
reverse_icons => 1,
}
);
The icons can be displayed without the cell value using the C<icons_only> parameter:
$worksheet->conditional_formatting( 'A1:C1',
{
type => 'icon_set',
icon_style => '3_flags',
icons_only => 1,
}
);
=head2 min_type, mid_type, max_type
The C<min_type> and C<max_type> properties are available when the conditional formatting type is C<2_color_scale>, C<3_color_scale> or C<data_bar>. The C<mid_type> is available for C<3_color_scale>. The properties are used as follows:
Expand Down Expand Up @@ -5217,6 +5323,15 @@ Example 10. Highlight blank cells.
}
);
Example 11. Set traffic light icons in 3 cells:
$worksheet->conditional_formatting( 'A1:C1',
{
type => 'icon_set',
icon_style => '3_traffic_lights',
}
);
See also the C<conditional_format.pl> example program in C<EXAMPLES>.
Expand Down
Loading

0 comments on commit cf0af06

Please sign in to comment.