Hi All,
I have below perl script which writes xml from .xls file.
Now i want to add below two conditions in this script :
- to check if the the input .xls file has ony two columns , if more tahn two columns then script should pop up an error.
- 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.