mwarrior has asked for the wisdom of the Perl Monks concerning the following question:
I have an excel file connected to a "data source" with a .iqy file. I used Perl to to open the excel file, refresh the data, and extract the data to be reformatted and output to some other text file. Initially, my code worked. However, I needed to change what .iqy file my spreadsheet was linked to, and doing this seems to have broken my Perl script (despite not actually changing anything inside the script itself). Now it fails when I call
my $LastRow = $Sheet->UsedRange->Find({What=>"*", SearchDirection=>xlPrevious, SearchOrder=>xlByRows})->{Row};
and cmd reads: "Can't use an undefined value as a HASH reference at sharing.pl line 20." I've tried debugging this, but I don't know enough about the guts of the Win32::OLE module to know how to even catch where or why the problem happens in the debugger. The source code for my script (except the portion that outputs to a text file) is:
#!/usr/bin/perl use Win32::OLE; use Win32::OLE qw(in with); use Win32::OLE::Variant; use Win32::OLE::Const 'Microsoft Excel'; $Excel = Win32::OLE->GetActiveObject('Excel.Application') || Win32::OLE->new('Excel.Application'); $Excel->{'Visible'} = 0; #0 is hidden, 1 is visible $Excel->{DisplayAlerts}=0; #0 is hide alerts # Open File and Worksheet my $Book = $Excel->Workbooks->Open ('C:\shareP\sp.xlsx'); # open Excel + file $Sheet = $Book->Worksheets(1); # Refresh Data (ActiveWorkbook.RefreshAll) $Book->RefreshAll(); # Find Last Column and Row my $LastRow = $Sheet->UsedRange->Find({What=>"*", SearchDirection=>xlPrevious, SearchOrder=>xlByRows})->{Row}; my $LastCol = $Sheet->UsedRange->Find({What=>"*", SearchDirection=>xlPrevious, SearchOrder=>xlByColumns})->{Column}; # read values from each column my @hasher; my $c = "a"; for (my $cn=1; $cn <= $LastCol; $cn++){ for (my $r=2; $r <= $LastRow; $r++){ $hasher[$r-2]{ $Sheet->Range($c.'1')->{Value} } = $Sheet->Rang +e($c.$r)->{Value}; } $c++; } # Save as Excel $Book->Save(); $Book->Close(); $Excel->Quit();
Thank you in advance for any and all advice. I'm really stuck on this one.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Problems parsing Excel 2010 .xlsx file with Win32::OLE
by blakew (Monk) on Feb 22, 2012 at 18:16 UTC | |
by Anonymous Monk on Feb 22, 2012 at 18:35 UTC | |
by blakew (Monk) on Feb 22, 2012 at 21:44 UTC | |
by mwarrior (Initiate) on Feb 22, 2012 at 21:48 UTC | |
|
Re: Problems parsing Excel 2010 .xlsx file with Win32::OLE
by nemesdani (Friar) on Feb 23, 2012 at 10:15 UTC |