I am using Win32. I have created an excel spreadsheet, formatted the columns and rows, and would like to write to the cells…all of this using Spreadsheet::Wr iteExcel. My issue is, my script is very vast with a lot of subroutines, but I need the excel spreadsheet created in the main subroutine becasue this is where the data is that i want to capture. So if I create and format the spreadsheet within the main subroutine, and as it loops through, a new spreadsheet is being created every time, so that when I move to the next row, it writes to it fine, but essentially it is writing over the previous row making it blank. If I create and format the spreadsheet outside of the main subroutine, then when I get to the part where it writes to the excel spreadsheet, it gives me an error that says, “Can’t call method ‘write’ on an undefined value…” I even tried to put the spreadsheet lines in its own subroutine and have the main subrouting call it, but i still run into the same problem when trying to write to it.
My question is how do I initially create and format the spreadsheet at the beginning and then as the subroutine loops, write to the cells, incrementing the rows in my loop? Any insight would be greatly appreciated.
Here is an example of the script format with the excel creation inside the main subroutine. Basically, the way it's set up, the main subroutine will continue looping until it goes through all of the declared %testcase_corre lation variables:
[HTML]#!/usr/bin/perl
use Spreadsheet::Wr iteExcel;
$report_file_pa th_name = "C:\\results.tx t";
my %testcase_corre lation1 = (
name => "Name1",
test_case => "NM204",
Code No. => "Z-204.5 CHEM",
);
my %testcase_corre lation2 = (
name => "Name2",
test_case => "NM366",
Code No. => "Z-366 CHEM",
);
my %testcase_corre lation3 = (
name => "Name3",
test_case => "NM876",
Code No. => "Z-876 CHEM",
);
my $row_count = 1;
my other declared variables;
execute_main_su broutine (\%testcase_cor relation1, $report_file_pa th_name, 'CHEM');
execute_main_su broutine (\%testcase_cor relation2, $report_file_pa th_name, 'CHEM');
execute_main_su broutine (\%testcase_cor relation3, $report_file_pa th_name, 'CHEM');
sub defined_subrout ine1{
.
.
}
sub defined_subrout ine2{
.
.
}
sub execute_main_su broutine{
my $params = shift;
my %testcase = %$params;
my $report_file_pa th_name = shift;
my $NBC_type = shift;
my $workbook = Spreadsheet::Wr iteExcel->new("c:\\perl. xls");
# Add worksheet
my $sheet1 = $workbook->add_worksheet( "Results Overview");
$sheet1->set_margins_LR (.3);
$sheet1->set_margin_top (.75);
$sheet1->set_margin_bot tom(.75);
$sheet1->center_horizon tally();
# Add Formats
my $format = $workbook->add_format() ;
$format->set_align('lef t');
$format->set_align('top ');
$format->set_border() ;
$format->set_text_wrap( );
my $format_fail = $workbook->add_format() ;
$format_fail->set_align('lef t');
$format_fail->set_align('top ');
$format_fail->set_color('red ');
$format_fail->set_border() ;
$format_fail->set_text_wrap( );
my $format_header = $workbook->add_format() ;
$format_header->set_align('cen ter');
$format_header->set_align('vce nter');
$format_header->set_bold();
$format_header->set_text_wrap( );
$format_header->set_border() ;
# Set the height of the rows
$sheet1->set_row(0, 60,$format_head er );
# Set the width of the columns
$sheet1->set_column('A: A', 15);
$sheet1->set_column('B: B', 25);
$sheet1->set_column('C: C', 15);
$sheet1->set_column('D: D', 41);
$sheet1->write(0, 0, "Test Case -\nMessage Group", undef,$format_h eader );
$sheet1->write(0, 1, "AEP-45\nParagraph\n Tested", undef,$format_h eader);
$sheet1->write(0, 2, "Completion\nSt atus", undef,$format_h eader);
$sheet1->write(0, 3, "Reason for Failure", undef,$format_h eader);
defined_subrout ine1();
.
.
defined_subrout ine2();
.
.
$sheet1->write($row_cou nt, 0, "$testcase{test _case}", $format);
$sheet1->write($row_cou nt, 1, "$testcase{AEP_ 45}", $format);
if (xyz == 0)
{
$sheet1->write($row_cou nt, 2, "PASS", $format);
}
else{
$sheet1->write($row_cou nt, 2, "FAIL", $format_fail);
}
$row_count++;
return 0;
} #end main subroutine[/HTML]
My question is how do I initially create and format the spreadsheet at the beginning and then as the subroutine loops, write to the cells, incrementing the rows in my loop? Any insight would be greatly appreciated.
Here is an example of the script format with the excel creation inside the main subroutine. Basically, the way it's set up, the main subroutine will continue looping until it goes through all of the declared %testcase_corre lation variables:
[HTML]#!/usr/bin/perl
use Spreadsheet::Wr iteExcel;
$report_file_pa th_name = "C:\\results.tx t";
my %testcase_corre lation1 = (
name => "Name1",
test_case => "NM204",
Code No. => "Z-204.5 CHEM",
);
my %testcase_corre lation2 = (
name => "Name2",
test_case => "NM366",
Code No. => "Z-366 CHEM",
);
my %testcase_corre lation3 = (
name => "Name3",
test_case => "NM876",
Code No. => "Z-876 CHEM",
);
my $row_count = 1;
my other declared variables;
execute_main_su broutine (\%testcase_cor relation1, $report_file_pa th_name, 'CHEM');
execute_main_su broutine (\%testcase_cor relation2, $report_file_pa th_name, 'CHEM');
execute_main_su broutine (\%testcase_cor relation3, $report_file_pa th_name, 'CHEM');
sub defined_subrout ine1{
.
.
}
sub defined_subrout ine2{
.
.
}
sub execute_main_su broutine{
my $params = shift;
my %testcase = %$params;
my $report_file_pa th_name = shift;
my $NBC_type = shift;
my $workbook = Spreadsheet::Wr iteExcel->new("c:\\perl. xls");
# Add worksheet
my $sheet1 = $workbook->add_worksheet( "Results Overview");
$sheet1->set_margins_LR (.3);
$sheet1->set_margin_top (.75);
$sheet1->set_margin_bot tom(.75);
$sheet1->center_horizon tally();
# Add Formats
my $format = $workbook->add_format() ;
$format->set_align('lef t');
$format->set_align('top ');
$format->set_border() ;
$format->set_text_wrap( );
my $format_fail = $workbook->add_format() ;
$format_fail->set_align('lef t');
$format_fail->set_align('top ');
$format_fail->set_color('red ');
$format_fail->set_border() ;
$format_fail->set_text_wrap( );
my $format_header = $workbook->add_format() ;
$format_header->set_align('cen ter');
$format_header->set_align('vce nter');
$format_header->set_bold();
$format_header->set_text_wrap( );
$format_header->set_border() ;
# Set the height of the rows
$sheet1->set_row(0, 60,$format_head er );
# Set the width of the columns
$sheet1->set_column('A: A', 15);
$sheet1->set_column('B: B', 25);
$sheet1->set_column('C: C', 15);
$sheet1->set_column('D: D', 41);
$sheet1->write(0, 0, "Test Case -\nMessage Group", undef,$format_h eader );
$sheet1->write(0, 1, "AEP-45\nParagraph\n Tested", undef,$format_h eader);
$sheet1->write(0, 2, "Completion\nSt atus", undef,$format_h eader);
$sheet1->write(0, 3, "Reason for Failure", undef,$format_h eader);
defined_subrout ine1();
.
.
defined_subrout ine2();
.
.
$sheet1->write($row_cou nt, 0, "$testcase{test _case}", $format);
$sheet1->write($row_cou nt, 1, "$testcase{AEP_ 45}", $format);
if (xyz == 0)
{
$sheet1->write($row_cou nt, 2, "PASS", $format);
}
else{
$sheet1->write($row_cou nt, 2, "FAIL", $format_fail);
}
$row_count++;
return 0;
} #end main subroutine[/HTML]
Comment