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!

In reply to How to convert RGB decimal to RGB hex and vise versa in Excel sheets? by thanos1983

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.