Hello Monks,
My question is related to Getting the background color of a cell in an existing Excel spreadsheet. I was working on resolving the question and although that it looks that the question is resolved I got stuck.
My problem is that when I define the color as decimal int coming from parsed Excel sheet from module Spreadsheet::ParseExcel works just fine, sample of code:
my %opts = (bg_color => $front_color,
align => "center",
pattern => $pattern,
);
# Add and define a format
my $format_2 = $workbook->add_format(%opts);
If I use the methods:
my $sRGB = $parser->ColorIdxToRGB($format->{Fill}->[2]);
my $sRGB_2 = $workbook->color_idx_to_rgb($format->{Fill}->[2]);
I can convert the int to RGB format (e.g. ffff00), but when I try to convert it back to int using this method set_custom_color($index, $red, $green, $blue):
my $backgroundcolor = $workbook->set_custom_color($back_color,
$sRGB);
I also tried to apply straight RGB hex directly ĺike:
my %opts = (bg_color => "ffff00",
align => "center",
pattern => $pattern,
);
# Add and define a format
my $format_2 = $workbook->add_format(%opts);
But the output is not as source Excel sheet in all of my attempts, unless if I use directly parsed int.
Sample of code that replicates my problem, and printout of expect output.
#!/usr/bin/perl
use strict;
use warnings;
use Spreadsheet::WriteExcel;
use Spreadsheet::ParseExcel;
my $parser = Spreadsheet::ParseExcel->new();
my $workbook_parse = $parser->Parse( 'Report.xls' );
my $worksheet_parse = $workbook_parse->Worksheet("Sheet1");
my ( $col_min, $col_max ) = $worksheet_parse->col_range();
my ( $row_min, $row_max ) = $worksheet_parse->row_range();
for my $col ( $col_min .. $col_max ) {
for my $row ( $row_min .. $row_max ) {
# Return the cell object at $row and $col
my $cell = $worksheet_parse->get_cell( $row, $col );
next unless $cell;
my $value = $cell->value();
my $format = $cell->get_format();
# Create a new Excel workbook
my $workbook = Spreadsheet::WriteExcel->new( 'perl.xls' );
# Add a worksheet
my $worksheet = $workbook->add_worksheet( "Health_Report" );
my $pattern = $format->{Fill}->[0];
my $front_color = $format->{Fill}->[1];
my $back_color = $format->{Fill}->[2];
my $sRGB = $parser->ColorIdxToRGB($format->{Fill}->[2]);
my $backgroundcolor = $workbook->set_custom_color($back_color,
$sRGB);
use Color::Rgb;
# Linux rgb file (I do not know if same directory in WindowsOS)
my $rgb = new Color::Rgb(rgb_txt=>'/etc/X11/rgb.txt');
my $yellow_hex = $rgb->hex('yellow');
print "RGB Yellow: " . $yellow_hex . "\n";
print "RGB Excel: " . $sRGB . "\n";
print "RGB int: " . $backgroundcolor . "\n";
my %opts = (bg_color => $front_color,
align => "center",
pattern => $pattern,
);
# Add and define a format
my $format_2 = $workbook->add_format(%opts);
# Write a formatted and unformatted string, row and column notatio
+n.
$worksheet->write($row, $col, $value, $format_2);
}
}
__END__
$ perl excel.pl
RGB Yellow: ffff00
RGB Excel: FFFF00
RGB int: 34
The reason that I am trying to convert RGB int to hex RGB and back to RGB in is because I was told that different Excel sheets can have different output, this is why we need to apply hex RGB.
Anyone has encounter this problem before?
Thanks in advance for your time and effort.
Seeking for Perl wisdom...on the process of learning...not there...yet!