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;
}