Re: Color highlighting in Excel
by chester (Hermit) on Sep 16, 2005 at 14:12 UTC
|
I have this lying around because I did something similar recently (but this is entirely untested):
use warnings;
use strict;
use Spreadsheet::WriteExcel;
my $workbook = Spreadsheet::WriteExcel->new("filename.xls") or die $!
+;
my $worksheet = $workbook->add_worksheet();
my $purple = $workbook->set_custom_color(40, 175,200,200);
my $format = $workbook->add_format(bold => 1,bg_color => $purple);
my $row = 0;
foreach my $data (@something) {
$worksheet->write_row($row,0,[$data],$format);
$row++;
}
| [reply] [d/l] |
|
|
<workbook>
<worksheet>
<loop name="data">
<row>
<if name="highlight_this">
<format bgcolor="purple">
<cell><var name="something" /></cell>
</format>
</if>
<if name="highlight_this" is="false">
<cell><var name="something" /></cell>
</if>
</row>
</loop>
</worksheet>
</workbook>
My criteria for good software:
- Does it work?
- Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
| [reply] [d/l] |
Re: Color highlighting in Excel
by Util (Priest) on Sep 16, 2005 at 14:53 UTC
|
Suggestions on your original code:
- $ident needs a newline after you print it.
- /$ident/ needs to be /$ident/o to prevent the (slow) recompiling of the regex on each loop.
- You should chomp the filename, just like you do for $ident.
- If the open fails on either of your files, then the rest of your program cannot run sensibly, so change warn into die.
- use strict and use warnings.
- In your FOUT warning, you used $i instead of $!. 'use strict' would catch that!
The example code below creates a spreadsheet with two rows, "Harry James Potter" and "Amelia Susan Bones", highlighting "James" and "Amelia".
Working, tested code:
use strict;
use warnings;
use Spreadsheet::WriteExcel;
my $out_file = 'result.xls';
my $search_string = 'me';
my $search_pattern = quotemeta($search_string);
my $workbook = Spreadsheet::WriteExcel->new($out_file)
or die "Can't create new workbook '$out_file': $!";
my $worksheet = $workbook->add_worksheet()
or die;
my $highlight = $workbook->add_format();
$highlight->set_bg_color('yellow');
my $row = 0;
while (<DATA>) {
chomp;
next unless /$search_pattern/o;
my $col = 0;
my @fields = split;
foreach my $field (@fields) {
if ( $field =~ /$search_pattern/o ) {
$worksheet->write_string($row, $col, $field, $highlight);
}
else {
$worksheet->write_string($row, $col, $field);
}
$col++;
}
$row++;
}
$workbook->close()
or warn "Error closing file '$out_file': $!";
__END__
Harry James Potter
Albus Percival Wulfric Brian Dumbledore
Cornelius Oswald Fudge
Amelia Susan Bones
Dolores Jane Umbridge
Percy Ignatius Weasley
| [reply] [d/l] [select] |
|
|
Thank you all so much. Well, Im not sure how it works but when I say result.xls, there is an excel worksheet created with the output in it. However, I must say that I started it out with a .txt extension and just experimented with a .xls extension. Im quite a novice in Perl, simply because I havent really been dedicating my time for it, but let me try the suggestions and see if it helps.
Thanks again
| [reply] |
Re: Color highlighting in Excel
by dorward (Curate) on Sep 16, 2005 at 12:40 UTC
|
I'm not an expert on the subject, so I might be wrong, but it looks that you aren't outputting to an Excel spreadsheet. It looks like you are outputting to a plain text file with a .xls file extension and new lines delimiting each entry then hoping that whatever application the user has registered to deal with Excel files can interpret that data and convert it to a real Excel format.
If so, then the solution is almost cetainly a case of using one of the Excel data handling modules on CPAN.
| [reply] |
Re: Color highlighting in Excel
by Skeeve (Parson) on Sep 16, 2005 at 12:42 UTC
|
| [reply] [d/l] |
Re: Color highlighting in Excel
by jmcnamara (Monsignor) on Sep 16, 2005 at 15:43 UTC
|
Here is a short example that matches an identifier and highlights it within the line written to an Excel file.
It used Spreadsheet::WriteExcelXML because this type of in-string highlighting isn't (currently) possible with Spreadsheet::WriteExcel. This means, however, that the end user must have Excel 2002/2003.
#!/usr/bin/perl -w
use strict;
use Spreadsheet::WriteExcelXML;
my $workbook = Spreadsheet::WriteExcelXML->new("file.xls");
my $worksheet = $workbook->add_worksheet();
my $ident = 'days';
my $row = 0;
while (<DATA>) {
if (s[\b($ident)\b][<B>$1</B>]og) {
chomp;
$worksheet->write_html_string($row++, 0, $_);
}
}
__DATA__
These are the days and these are the days.
What are days for?
This is the time.
And this is the record of the time.
I thank you for the days.
--
John.
| [reply] [d/l] |
|
|
The file gets terminated at the second line, since its unable to find Spreadsheet::WriteExcelXML or evern Spreadsheet::WriteExcel. Do I need to add WriteExcel into the repository.
| [reply] |
|
|
That error means that the module hasn't been installed.
There are installation instructions for Spreadsheet::WriteExcel here. The first two methods also apply to Spreadsheet::WriteExcelXML.
With ActivePerl you can also install the modules via ppm from the ActiveState repositories.
See also Writing, Installing, and Using Perl Modules.
--
John.
| [reply] |
|
|
|
|