merrymonk has asked for the wisdom of the Perl Monks concerning the following question:

I want to fill the background of excel cells with colours made up from the known red, green and blue components of the colours. To do this I found I needed to use:
$sheet->Range($rng)->Interior -> {PatternColor} = rgb colour value
$sheet->Range($rng)->Interior -> {Pattern} = pattern value
This site https://docs.microsoft.com/en-us/office/vba/api/Excel.XlPattern gives ‘values’ for 20 patterns including xlPatternSolid which I thought would give me the whole cell filled with that colour. This was not the case as the Perl below shows.
This code gives an Excel spreadsheet where:
Column C is the key to a hash which holds the pattern ‘names’ such as xlPatternSolid
Column D gives the cell filled with the colour and effect of the pattern
Column E gives the value of the pattern ‘names’ as shown on the web-site.
Cell D20 is where the xlPatternSolid has been used. This has no colour instead of the solid colour I was hoping for. Also cell D3 has no colour – the name of this colour pattern xlPatternAutomatic – I was not sure what to expect for this.
I did notice that the sides of cells D20 and D3 are no longer there.
How can I get cells filled with the solid colour?
use strict "vars"; use OLE; use Win32::OLE::Const "Microsoft Excel"; sub RGB ($$$$) { my ($jr, $jg, $jb, $ret_c) =@_; $$ret_c = $jr + $jg * 256 + $jb *256** 2; } my ($excel, $workbook, $sheet, $rng, $jh); my ($jr, $jg, $jb, $patterncolour, $jq, $jqa); my %cell_interior_pattern; # list from https://docs.microsoft.com/en-us/office/vba/api/Excel.XlPa +ttern $cell_interior_pattern{1} = xlPatternAutomatic; $cell_interior_pattern{2} = xlPatternChecker; $cell_interior_pattern{3} = xlPatternCrissCross; $cell_interior_pattern{4} = xlPatternDown; $cell_interior_pattern{5} = xlPatternGray16; $cell_interior_pattern{6} = xlPatternGray25; $cell_interior_pattern{7} = xlPatternGray50; $cell_interior_pattern{8} = xlPatternGray75; $cell_interior_pattern{9} = xlPatternGray8; $cell_interior_pattern{10}= xlPatternGrid; $cell_interior_pattern{11}= xlPatternHorizontal; $cell_interior_pattern{12}= xlPatternLightDown; $cell_interior_pattern{13}= xlPatternLightHorizontal; $cell_interior_pattern{14}= xlPatternLightUp; $cell_interior_pattern{15}= xlPatternLightVertical; $cell_interior_pattern{16}= xlPatternNone; $cell_interior_pattern{17}= xlPatternSemiGray75; $cell_interior_pattern{18}= xlPatternSolid; $cell_interior_pattern{19}= xlPatternUp; $cell_interior_pattern{20}= xlPatternVertical; $excel = CreateObject OLE "Excel.Application"; $excel -> {Visible} = 1; $workbook = $excel -> Workbooks -> Add; $sheet = $workbook -> Worksheets("Sheet1"); $sheet -> Activate; $sheet -> Range('C2') -> {Value} = 'Index'; $sheet -> Range('D2') -> {Value} = 'Colour'; $sheet -> Range('E2') -> {Value} = 'Value of array index'; $jr = 120; $jg = 140; $jb = 55; RGB($jr, $jg, $jb, \$patterncolour); for $jq (sort {$a <=> $b} keys %cell_interior_pattern) { $jqa = $jq + 2; $rng = 'D' . $jqa; $sheet->Range($rng)->Interior -> {PatternColor} = $patternco +lour; $sheet->Range($rng)->Interior -> {Pattern} = $cell_interior_ +pattern{$jq}; $rng = 'E' . $jqa; $sheet -> Range($rng) -> {Value} = $cell_interior_pattern{$jq}; $rng = 'c' . $jqa; $sheet -> Range($rng) -> {Value} = $jq; }

Replies are listed 'Best First'.
Re: Problem filling Excel cells with Solid Colour: Arrays
by hippo (Archbishop) on Mar 03, 2021 at 10:50 UTC
    $cell_interior_pattern{1} = xlPatternAutomatic; $cell_interior_pattern{2} = xlPatternChecker; $cell_interior_pattern{3} = xlPatternCrissCross; $cell_interior_pattern{4} = xlPatternDown; #... for $jq (sort {$a <=> $b} keys %cell_interior_pattern) {

    Did you know that Perl has arrays? You don't need to sort the index list for them and you can construct them with a list like:

    my @cell_interior_pattern = (xlPatternAutomatic, xlPatternChecker, xlP +atternCrissCross, xlPatternDown);

    Just remember that the index starts from 0 and you'll be fine. Less verbose code and lower memory requirements and the right tool for the job. HTH.


    🦛

      Thanks I did. I just did it this way to change from a hash with keys which were names to simple numbers.

        Without any further context that sounds like a really bad idea. Names convey meaning and help reduce errors.

        Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
        Update - I found the solution - it is in the line below where Interior can have simply Color
        $sheet->Range($rng)->Interior ->{Color} = $patterncolour;
        to fill the cell.
        However it would be good to know why the solid colour definition does not work