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.
In reply to Problems parsing Excel 2010 .xlsx file with Win32::OLE by mwarrior
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |