doy / spreadsheet-parsexlsx

parse XLSX files
http://metacpan.org/release/Spreadsheet-ParseXLSX
27 stars 35 forks source link

Does not return background color of a cell. #78

Open bulrush15 opened 6 years ago

bulrush15 commented 6 years ago

When using my code getcellcolor(), I get undef or garbage back that is meaningless. Here is my subroutine below.

###########################################################################
# In: Workbook object;
#     Sheet object;
#     row number (0-based);
#     col number (0-based);
#     XLSX filename (string);
#     tab name (string);
#     row in spreadsheet (just $row+1), 1-based.
# Out: String of cell contents.

# How the parameters passed in are created:
# my $wkbkin=$parser->parse($xlsfnin);
#     if (not $sheetin=$wkbkin->worksheet(0)) # Get first tab.
#         {
#         $s=$prefixsp."$procname ERROR: Could not get first tab in $xlsfnin";
#         print "$s\n";
#         $err=$s;
#         exit 1;
#         }
sub getcellcolor
{my($wkbkin,$sheetin,$row,$col,$xlsfnin,$tab,$zpos)=@_;
my($s,$t);

my $procname=(caller(0))[3];

my $prefixsp='  ';
my $new='';
my $cellin;
my $v='';

eval {
    $cellin=$sheetin->get_cell($row,$col);
    };
if ($@)
    {
    $s="$procname ERROR cell ".makecellname($row,$col).
    " from XLSX sheetin->get_cell. ";
    if ( defined($@) and (length($@)>0) )
        {
        $s.="\n\  $@ error: ".$@;
        }
    writeerr($s);
    return '';
    }

# Get value for debugging.
eval {
    $v=$cellin->value();
    };
if ($@)
    {
    $s="$procname ERROR cell ".makecellname($row,$col).
    ": from XLSX sheetin->value. ";
    writeerr($s);
    usleep $optsleep;
    }

my $bgcolor='';
if ($cellin)
    {
    my $format=$cellin->get_format();
    my $fillref=$format->{Fill};
    my @fillarr=@$fillref;
    my $idx='';
    if (defined($cellin->{Format}->{Fill}->[2]))
        {
        $idx=$cellin->{Format}->{Fill}->[2]; # Get background color index of this cell.
        if ($idx=~m/^[\d]+$/)
            {
            $bgcolor=$wkbkin->color_idx_to_rgb($idx);
            }
        else {
            $s="$procname ERROR cell ".makecellname($row,$col).": ".
            "Background color was blank for index '$idx'.";
            writeerr($s);
            sleep 1;
            }
        }
    #$font=$format->{Font}; # Returns font.
    #my $idx=$font->{Color}; # Returns color index.
    #my $rgbcolor=$ssin->ColorIdxToRGB(0,$idx); # Return RGB color as 6 ctr string.
    $s="$procname: ".makecellname($row,$col)." background ='$bgcolor'".
    ".  Val=$v";
    debugprgif($s,'  ');
    }
else {
   $s=$prefixsp."$procname ERROR ".makecellname($row,$col).", $xlsfnin: ".
      ", cell->get_format returned blank. ";
    return ''; # Error, color not found.
    }

return trim($bgcolor); # getcellcolor
}