Skip to content

Instantly share code, notes, and snippets.

@KentaGoto
Created December 6, 2019 02:43
Show Gist options
  • Save KentaGoto/d781eeb9c18d43f3098661fbcfd8e2be to your computer and use it in GitHub Desktop.
Save KentaGoto/d781eeb9c18d43f3098661fbcfd8e2be to your computer and use it in GitHub Desktop.
Check Excel's font ColorIndex
use strict;
use warnings;
use utf8;
use Win32::OLE qw(in with);
use Win32::OLE::Const 'Microsoft Excel';
Win32::OLE->Option( CP=>Win32::OLE::CP_UTF8 );
use Win32::OLE::Variant;
###############################################################
# Check Excel's font ColorIndex
###############################################################
print "xlsx: ";
chomp( my $xlsx = <STDIN> );
$xlsx =~ s{^"}{};
$xlsx =~ s{"$}{};
print "Sheet name: ";
chomp( my $sheet_name = <STDIN> );
$sheet_name =~ s{^"}{};
$sheet_name =~ s{"$}{};
print "Column: ";
chomp( my $column = <STDIN> );
$column =~ s{^"}{};
$column =~ s{"$}{};
my $Excel = Win32::OLE->new('Excel.Application')
|| die "cannot get active excel!";
$Excel->{DisplayAlerts} = 'False';
my $book = $Excel->Workbooks->Open( $xlsx ) or die;
my $sheet= $book->Worksheets( $sheet_name );
my $row;
for ( $row=1; defined( $sheet->Range( $column . $row )->{Value} ); $row++ ){
# Check the font color.
my $color = $sheet->Range( $column . $row )->Font->{ColorIndex};
if ( $color ){
print $color . "\n";
}
}
$book->Close();
$Excel->quit();
system('pause > nul')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment