[英]iterate over Excel file in perl using Spreadsheet::Read
I am trying to parse data from an excel file (can be xlsx or xls). 我试图解析excel文件中的数据(可以是xlsx或xls)。
I already know which worksheets I want to get, so I would like to iterate over them and extract the data from them. 我已经知道我想要获得哪些工作表,所以我想迭代它们并从中提取数据。
My code : 我的代码:
#!/usr/bin/perl -w
use strict;
use warnings;
use Spreadsheet::Read;
use Getopt::Long;
my $inputfile;
GetOptions (
'i=s' => \$inputfile,
);
die 'missing input file' unless $inputfile;
my $workbook = ReadData ($inputfile, debug => 9);
my @worksheets = (1);
foreach (@worksheets) {
my $sheet = $workbook->[$_-1];
next unless $sheet;
my ( $row_min, $row_max ) = $sheet->row_range();
my ( $col_min, $col_max ) = $sheet->col_range();
for my $row ($row_min .. $row_max) {
}
}
However, I get the following : 但是,我得到以下内容:
Can't call method "row_range" on unblessed reference at perl/parse_test.pl line 22.
I'm very new to perl, and don't understand yet the intricacies of hashes, arrays and references. 我对perl很新,并且还不了解哈希,数组和引用的复杂性。
First don't use array if you don't need it, 如果你不需要,首先不要使用数组,
my @sheet = $workbook->[$_-1];
=> my $sheet = $workbook->[$_-1];
=>
my $sheet = $workbook->[$_-1];
Print $sheet
reference to check instance of $sheet
if error still occurs. 如果仍然出现错误,打印
$sheet
引用以检查$sheet
实例。
next unless $sheet;
print ref($sheet), "\n";
It look like your data should be accessed in another way, this is from http://metacpan.org/pod/Spreadsheet::Read#Data-structure 看起来你的数据应该以另一种方式访问,这来自http://metacpan.org/pod/Spreadsheet::Read#Data-structure
$book = [
# Entry 0 is the overall control hash
{ sheets => 2,
sheet => {
"Sheet 1" => 1,
"Sheet 2" => 2,
},
type => "xls",
parser => "Spreadsheet::ParseExcel",
version => 0.59,
},
# Entry 1 is the first sheet
{ label => "Sheet 1",
maxrow => 2,
maxcol => 4,
cell => [ undef,
[ undef, 1 ],
[ undef, undef, undef, undef, undef, "Nugget" ],
],
A1 => 1,
B5 => "Nugget",
},
# Entry 2 is the second sheet
{ label => "Sheet 2",
:
:
]
so if you want to read label from $sheet
it would be $sheet->{label}
, and so on. 因此,如果你想从
$sheet
读取标签,它将是$sheet->{label}
,依此类推。
The problem is the following: 问题如下:
@sheet->row_range
You cannot use a method on a non-object. 您不能在非对象上使用方法。 Object must be a reference, ie a scalar.
对象必须是引用,即标量。 It should start with a
$
, not @
. 它应该以
$
开头,而不是@
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.