my $workbook = Spreadsheet::WriteExcel->new($file_name);
my $worksheet = $workbook->add_worksheet();
my $format_heading = $workbook->add_format();
my $condition = "some value";
getFormat($condition);
$worksheet->write($row_position, $column_position, $some_info,$format_heading);
sub getFormat {
$condition = shift;
if (Condition 1){
$format_heading->set_bgcolor('yellow');
}
elsif (Condition 2){
$format_heading->set_bgcolor('pink');
$format_heading->set_color('red');
}
elsif (Condition 3){
$format_heading->set_bgcolor('blue');
}
else {
$format_heading->set_bgcolor('white'); # Default color
}
}
Hi [shilpam], you change 'set_bgcolor' method into 'set_bg_color'. The following code works for me.
use strict;
use Spreadsheet::WriteExcel;
my $workbook = Spreadsheet::WriteExcel->new("test.xls");
my $worksheet = $workbook->add_worksheet();
my $format_heading = $workbook->add_format();
my $condition = "a";
getFormat($condition);
$worksheet->write("A1", "sometext", $format_heading);
sub getFormat {
$condition = shift;
if ($condition eq 'a'){
$format_heading->set_bg_color('yellow');
}
elsif ($condition eq 'b'){
$format_heading->set_bg_color('pink');
$format_heading->set_color('red');
}
elsif ($condition eq 'c'){
$format_heading->set_bg_color('blue');
}
else {
$format_heading->set_bg_color('white'); # Default color
}
}
Prasad
#!/usr/bin/perl
use strict;
use Spreadsheet::WriteExcel;
my $workbook = Spreadsheet::WriteExcel->new("test.xls");
my $worksheet = $workbook->add_worksheet();
my $format_heading = $workbook->add_format();
my $condition = "a";
getFormat($condition);
$worksheet->write("A1", "sometext", $format_heading);
$condition = "c";
getFormat($condition);
$worksheet->write("A2", "sometext", $format_heading);
$condition = "d";
getFormat($condition);
$worksheet->write("A3", "sometext", $format_heading);
$condition = "b";
getFormat($condition);
$worksheet->write("A4", "sometext", $format_heading);
sub getFormat {
$condition = shift;
if ($condition eq 'a'){
$format_heading->set_bg_color('yellow');
}
elsif ($condition eq 'b'){
$format_heading->set_bg_color('pink');
}
elsif ($condition eq 'c'){
$format_heading->set_bg_color('blue');
}
else {
$format_heading->set_bg_color('green'); # Default color
}
}
[shilpam], the problem is you are using 'pink' color, which sets white color, you try someother color in that condition. But as per your logic the condition which get satisfied at last will be set as background color for rest.
If i understood your requirement correctly, the following coding exactly doing your job. But as far as efficiency of the code, i cant guarantee you. You can minimize the following code.
use strict;
use Spreadsheet::WriteExcel;
my $workbook = Spreadsheet::WriteExcel->new("test.xls");
my $worksheet = $workbook->add_worksheet();
my $format_heading1 = $workbook->add_format();
my $format_heading2 = $workbook->add_format();
my $format_heading3 = $workbook->add_format();
my $format_heading4 = $workbook->add_format();
my $condition = "a";
getFormat1($condition);
$worksheet->write("A1", "sometext", $format_heading1);
$condition = "c";
getFormat2($condition);
$worksheet->write("A2", "sometext", $format_heading2);
$condition = "d";
getFormat3($condition);
$worksheet->write("A3", "sometext", $format_heading3);
$condition = "b";
getFormat4($condition);
$worksheet->write("A4", "sometext", $format_heading4);
sub getFormat1 {
$condition = shift;
if ($condition eq 'a'){
$format_heading1->set_bg_color('yellow');
}
elsif ($condition eq 'b'){
$format_heading1->set_bg_color('red');
}
elsif ($condition eq 'c'){
$format_heading1->set_bg_color('blue');
}
else {
$format_heading1->set_bg_color('green'); # Default color
}
}
sub getFormat2 {
$condition = shift;
if ($condition eq 'a'){
$format_heading2->set_bg_color('yellow');
}
elsif ($condition eq 'b'){
$format_heading2->set_bg_color('red');
}
elsif ($condition eq 'c'){
$format_heading2->set_bg_color('blue');
}
else {
$format_heading2->set_bg_color('green'); # Default color
}
}
sub getFormat3 {
$condition = shift;
if ($condition eq 'a'){
$format_heading3->set_bg_color('yellow');
}
elsif ($condition eq 'b'){
$format_heading3->set_bg_color('red');
}
elsif ($condition eq 'c'){
$format_heading3->set_bg_color('blue');
}
else {
$format_heading3->set_bg_color('green'); # Default color
}
}
sub getFormat4 {
$condition = shift;
if ($condition eq 'a'){
$format_heading4->set_bg_color('yellow');
}
elsif ($condition eq 'b'){
$format_heading4->set_bg_color('red');
}
elsif ($condition eq 'c'){
$format_heading4->set_bg_color('blue');
}
else {
$format_heading4->set_bg_color('green'); # Default color
}
}
Cheers!!! :)
Prasad
The first is that you must create a unique format for each colour. Otherwise the format object will have the background colour of the last call to bg_colour. This is explained [http://search.cpan.org/~jmcnamara/Spreadsheet-WriteExcel/lib/Spreadsheet/WriteExcel.pm#Working_with_formats|here].
Secondly, [cpan://Spreadsheet::WriteExcel] doesn't support a named colour called "pink"*. Thus the colour reverts to the default value of white. The colour closest to what you require is "magenta" (I think that this adheres to the Windows colour naming convention).
So changing your code around to account for these issues will give you something like this.
#!/usr/bin/perl -w
use strict;
use Spreadsheet::WriteExcel;
my $workbook = Spreadsheet::WriteExcel->new("test.xls");
my $worksheet = $workbook->add_worksheet();
my $format_heading_yellow = $workbook->add_format(bg_color => 'yellow' );
my $format_heading_pink = $workbook->add_format(bg_color => 'magenta');
my $format_heading_blue = $workbook->add_format(bg_color => 'blue' );
my $format_heading_green = $workbook->add_format(bg_color => 'green' );
my $condition = "a";
my $format_heading = getFormat($condition);
$worksheet->write("A1", "sometext", $format_heading);
$condition = "c";
$format_heading = getFormat($condition);
$worksheet->write("A2", "sometext", $format_heading);
$condition = "d";
$format_heading = getFormat($condition);
$worksheet->write("A3", "sometext", $format_heading);
$condition = "b";
$format_heading = getFormat($condition);
$worksheet->write("A4", "sometext", $format_heading);
sub getFormat {
$condition = shift;
if ($condition eq 'a'){
return $format_heading_yellow;
}
elsif ($condition eq 'b'){
return $format_heading_pink;
}
elsif ($condition eq 'c'){
return $format_heading_blue;
}
else {
return $format_heading_green; # Default color
}
}
--
John.
* Although it probably should.
I need conditional formatting in the sense that if certain conditions are fulfilled the background color should change.The thing is that the way you've structured your code, it will only happen at spreadsheet generation time, not when the user opens up the spreadsheet and changes the value. If you look at the docs for Spreadsheet::WriteExcel and search for "condtional formatting", you'll see a more flexible solution. It may or may not work for you depending on what your conditions are, but my guess is that it will.
thor
The only easy day was yesterday
perlmonks.org content © perlmonks.org and davidrw, jmcnamara, prasadbabu, shilpam, thor
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03