Perl script to accept specific columns from excel

Hi All,

I have below perl script which writes xml from .xls file.
Now i want to add below two conditions in this script :

  1. to check if the the input .xls file has ony two columns , if more tahn two columns then script should pop up an error.
  2. If there are two columns , then first column should have header 'ID' and second column as 'Name' , if this is not the case then script should fire an error.

Here is my code :

 
#!/usr/bin/perl -w 
use strict;
use warnings;
use Spreadsheet::ParseExcel;
use XML::Writer;
use Time::Piece;
my $num_args = $#ARGV + 1; 
if ($num_args != 2) {     
print "\nUsage: baseline.pl Baseline_name xls_filename\n";     
exit; 
}   
my $Baseline_name=$ARGV[0]; 
my $xls_filename=$ARGV[1];
my $date = localtime->strftime('%Y-%m-%d');
my $date1 = localtime->strftime('%a, %d %b %Y %H:%M:%S +0000');
my @columns = ('ID', 'Name');
my $parser = Spreadsheet::ParseExcel->new();
my $workbook = $parser->parse($xls_filename) or die $parser->error();
my $worksheet = $workbook->worksheet(0);
my ( $row_min, $row_max ) = $worksheet->row_range();
my @data;
for my $row ( ($row_min+1) .. $row_max ) {
    my %hash;
    for my $col (0 .. $#columns) {
        my $cell = $worksheet->get_cell( $row, $col );
        $hash{$columns[$col]} = $cell->value();
    }
    push(@data,\%hash)
}
my $writer = XML::Writer->new(OUTPUT => 'self', DATA_MODE => 1);
$writer->xmlDecl("UTF-8");
$writer->startTag("BES", "xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance", "xsi:noNamespaceSchemaLocation" => "BES.xsd");
$writer->startTag('Baseline');
$writer->dataElement(Title => $Baseline_name);
$writer->dataElement(Description => "");
$writer->dataElement(Relevance => "true");
$writer->dataElement(Category=> "");
$writer->dataElement(Source => "Internal");
$writer->dataElement(SourceID => "");
$writer->dataElement(SourceReleaseDate => $date);
$writer->dataElement(SourceSeverity => "");
$writer->dataElement(CVENames => "");
$writer->dataElement(SANSID => "");
$writer->startTag('MIMEField');
$writer->dataElement(Name => "x-fixlet-modification-time");
$writer->dataElement(Value => $date1);
$writer->endTag('MIMEField');
$writer->dataElement(Domain => "BESC");
$writer->startTag('BaselineComponentCollection');
$writer->startTag('BaselineComponentGroup');
for my $row (@data) {
    $writer->startTag("BaselineComponent", Name => $row->{'Name'}, IncludeInRelevance => "true", SourceSiteURL => "http://sync.bigfix.com/cgi-bin/bfgather/bessecurity",SourceID => $row->{'ID'}, ActionName => "Action1");
 $writer->dataElement(ActionScript => "");
 $writer->dataElement(Relevance => "");
 $writer->endTag('BaselineComponent');
}
 $writer->endTag('BaselineComponentGroup');
 $writer->endTag('BaselineComponentCollection');
 $writer->endTag('Baseline');
 $writer->endTag('BES');
$writer->end;
print $writer->to_string;

Can someone please help me in adding these two condition in it.

can someone please help over here..

For check # 1 - use the col_range() method to determine the min and max column values and proceed from there.

For check # 2 - use the get_cell(row, col) method to fetch the values of the relevant cells and perform your checks.

More detailed information of all methods of this module are at this location:

could you please shown me small examples of the same ... i will refer it for doing the modification.
thanks..

But you've used those (or very similar) methods in your program already!
So I'm assuming you know how to use them.
In case you didn't write the program you posted, I'd suggest going through it and understanding each method invocation first, by cross-checking with the metacpan page. Once that understanding is in place, the usage of the two methods in my earlier post will be obvious.