我有一个 .iqy 文件连接到“数据源”的 excel 文件 (.xlsx)。我使用 Perl 打开 excel 文件并刷新数据。最初,我的代码有效。但是,我需要更改我的电子表格链接到的 .iqy 文件,这样做似乎破坏了我的 Perl 脚本(尽管实际上并没有更改脚本本身的任何内容)。现在当我打电话时它失败了
my $LastRow = $Sheet->UsedRange->Find({What=>"*",
SearchDirection=>xlPrevious,
SearchOrder=>xlByRows})->{Row};
和 cmd 输出显示:“不能在 shared.pl 第 20 行使用未定义的值作为 HASH 引用。” 我已经尝试过对此进行调试,但我对 Win32::OLE 模块的内容知之甚少,无法知道如何在调试器中捕获问题发生的位置或原因。我的脚本的源代码是:
#!/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};
####### EDIT: I initially didn't post this portion because it stops
# before reaching it unless I make $LastCol and $LastRow constants
my @hasher;
my $c = "a";
for (my $cn=1; $cn <= $LastCol; $cn++){
for (my $r=2; $r <= $LastRow; $r++){
# stops here with same error if I make $LastCol and $LastRow constants
$hasher[$r-2]{ $Sheet->Range($c.'1')->{Value} } = $Sheet->Range($c.$r)->{Value};
}
$c++;
}
####### end of EDIT
# Save as Excel
$Book->Save();
$Book->Close();
$Excel->Quit();
提前感谢您的任何建议。我真的坚持这个。