Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Help using Spreadsheet::ParseExcel Module - Can't call method "value"on an undefined value (http://www.velocityreviews.com/forums/t910428-help-using-spreadsheet-parseexcel-module-cant-call-method-value-on-an-undefined-value.html)

perl Newbie 05-05-2009 06:02 PM

Help using Spreadsheet::ParseExcel Module - Can't call method "value"on an undefined value
 
Hi,

Please help me understand why I am getting error { Can't call method
"value" on an undefined value excelRead.pl line 34. }

Line 34 in my script is $qval = $qcell->value();

When I checked the excel file, there are no blank rows in column 1,
then how come it is throwing an error -undefined value?

Thanks!


__CODE__

use strict;
use warnings;
use Spreadsheet::ParseExcel;


my $excelfilename="Data2.xls";
my $datasheet="data";


my $parser = Spreadsheet::ParseExcel->new();
my $workbook = $parser->Parse($excelfilename);
my $worksheet = $workbook->Worksheet($datasheet);
my ( $row_min, $row_max ) = $worksheet->row_range();
my $row;
my $cell;
my $cell_value;

my $qcell;
my $qval;
my @qids;

for $row ($row_min .. $row_max) {
$cell = $worksheet->get_cell($row,3);
$cell_value = $cell->value();

if ($cell_value =~/single choice/i) {
$qcell = $worksheet->get_cell($row+1,0);
$qval = $qcell->value();
until ($qval =~/question/) {
$qcell = $worksheet->get_cell($row
+1,0);
$qval = $qcell->value();
push @qids, $worksheet->get_cell($row
+1,1)->value();
$row++;
}
}
}

foreach my $l (@qids) {
print $l, "\n";
}



J. Gleixner 05-05-2009 06:25 PM

Re: Help using Spreadsheet::ParseExcel Module - Can't call method"value" on an undefined value
 
perl Newbie wrote:
> Hi,
>
> Please help me understand why I am getting error { Can't call method
> "value" on an undefined value excelRead.pl line 34. }
>
> Line 34 in my script is $qval = $qcell->value();
>
> When I checked the excel file, there are no blank rows in column 1,
> then how come it is throwing an error -undefined value?

[...]
> my $row;
> my $cell;
> my $cell_value;
>
> my $qcell;
> my $qval;
> my @qids;


Define the variable in the smallest possible scope.

>
> for $row ($row_min .. $row_max) {

for my $row ( $row_min .. $row_max) {
> $cell = $worksheet->get_cell($row,3);

my $cell = $worksheet->get_cell($row,3);

etc.
> $cell_value = $cell->value();
>
> if ($cell_value =~/single choice/i) {
> $qcell = $worksheet->get_cell($row+1,0);


What happens when $row equals $row_max.. e.g. what's in ($row_max+1, 0)?

You could use another for loop, which would make your code much cleaner.

for my $next_row ( $row+1 .. $row_max ) {
# get the values you're after..
last if $qval =~ /question/;
}

> $row++;


Be careful modifying a variable you use in your for loop. In
this case perl saves you, but you should avoid doing it.

FYI: You can add print statements to your code, to help you debug things
on your own.

perl Newbie 05-06-2009 09:43 AM

Re: Help using Spreadsheet::ParseExcel Module - Can't call method"value" on an undefined value
 
On May 5, 11:25*pm, "J. Gleixner" <glex_no-s...@qwest-spam-no.invalid>
wrote:
> perl Newbie wrote:
> > Hi,

>
> > Please help me understand why I am getting error { Can't call method
> > "value" on an undefined value excelRead.pl line 34. }

>
> > Line 34 in my script is $qval = $qcell->value();

>
> > When I checked the excel file, there are no blank rows in column 1,
> > then how come it is throwing an error -undefined value?

> [...]
> > * * * * my $row;
> > * * * * my $cell;
> > * * * * my $cell_value;

>
> > * * * * my $qcell;
> > * * * * my $qval;
> > * * * * my @qids;

>
> Define the variable in the smallest possible scope.
>
>
>
> > * * * * for $row ($row_min .. $row_max) {

>
> for my $row ( $row_min .. $row_max) {> * * * * * * * * $cell = $worksheet->get_cell($row,3);
>
> my $cell = $worksheet->get_cell($row,3);
>
> etc.
>
> > * * * * * * * * $cell_value = $cell->value();

>
> > * * * * * * * * if ($cell_value =~/single choice/i) {
> > * * * * * * * * * * * * $qcell = $worksheet->get_cell($row+1,0);

>
> What happens when $row equals $row_max.. e.g. what's in ($row_max+1, 0)?
>
> You could use another for loop, which would make your code much cleaner.
>
> * * * * * * * * * * * * for my $next_row ( $row+1.. $row_max ) {
> * * * * * * * * * * * * * * * * # get thevalues you're after..
> * * * * * * * * * * * * * * * * last if $qval =~ /question/;
> * * * * * * * * * * * * }
>
> *> * * * * * * * * * * * * * * * $row++;
>
> Be careful modifying a variable you use in your for loop. In
> this case perl saves you, but you should avoid doing it.
>
> FYI: You can add print statements to your code, to help you debug things
> on your own.



>You could use another for loop, which would make your code much cleaner.


>for my $next_row ( $row+1 .. $row_max ) {
> # get the values you're after..
> last if $qval =~ /question/;
> }


Thanks for the help, using another loop solved the problem.

Why I am getting error if I loop using until?

__CODE__
use strict;
use warnings;
use Spreadsheet::ParseExcel;

my $excelfilename="data.xls";
my $datasheet="data";

my $parser = Spreadsheet::ParseExcel->new();
my $workbook = $parser->Parse($excelfilename);
my $worksheet = $workbook->Worksheet($datasheet);
my ( $row_min, $row_max ) = $worksheet->row_range();
my $row;
my $rowL;
my @qids;

for $row ($row_min .. $row_max) {
if ($worksheet->get_cell($row,3)->value() =~/single choice/i)
{
##print "Row: $row ", $worksheet->get_cell($row,3)-
>value(), "\n";

$rowL=$row+1;
for $rowL ($rowL .. $row_max ) {
last if ($worksheet->get_cell($rowL,0)->value()
=~/question/);
##until ($worksheet->get_cell($rowL,0)->value()=~/
question/) {
if ($worksheet->get_cell($rowL,0)->value()=~/
column/) {
##print "RowL: $rowL ", $worksheet-
>get_cell($rowL,6)->value(), "\n";

push @qids, $worksheet->get_cell($rowL,
6)->value();
}
$rowL++;
}
}
next;
}

foreach my $l (@qids) {
print $l, "\n";
}











All times are GMT. The time now is 05:09 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.