I need to retrieve the background color of an excel cell (set to red/green based on a conditional formatting in the sheet). Tried searching the forums but couldnt find anything to retrieve although setting the colors are there. Any help would be appreciated...
Asked
Active
Viewed 1,755 times
1 Answers
2
You need $Range->Interior()->ColorIndex();
Here is sample program:
#!/usr/bin/perl
use Modern::Perl;
use Win32::OLE;
use FindBin qw($Bin);
my $ex;
eval { $ex = Win32::OLE->GetActiveObject('Excel.Application') };
die "Excel not installed" if $@;
unless ( defined $ex ) {
$ex = Win32::OLE->new( 'Excel.Application', sub { $_[0]->Quit; } )
or die "Oops, cannot start Excel";
}
my $book = $ex->Workbooks->Open("$Bin/test_background.xls");
my $sheet = $book->Worksheets(1);
my $Range = $sheet->Range("A1:A1");
say $Range->Interior()->ColorIndex();
$Range = $sheet->Range("B1:B1");
say $Range->Interior()->ColorIndex();
$Range = $sheet->Range("C1:C1");
say $Range->Interior()->ColorIndex();
The output for this file
is this:
3
6
3

gangabass
- 10,607
- 2
- 23
- 35
-
thanks. but as i mentioned the color is set based on conditional formatting. will this work for that also? – Siva Jul 09 '13 at 14:25
-
Hmmm... I'm not sure but you can try. – gangabass Jul 09 '13 at 14:58