我附上了样本excel表。 我的脚本应在“Rate”列中搜索负值,并将值设为零。
所以我首先开始编写脚本,读取列中的值并打印值,但我的脚本始终将值设置为零。
请帮助我找到可以找到相关信息的解决方案或指针。
提前致谢,
请在下面找到我的脚本
#!/usr/bin/perl -w
use strict;
use warnings;
use Spreadsheet::ParseExcel::SaveParser;
my $parser = Spreadsheet::ParseExcel->new();
my $workbook = $parser->parse('Emm Fill Single Radd.xls');
die $parser->error(), ".\n" if ( !defined $workbook );
for my $worksheet ( $workbook->worksheets() ) {
my ( $row_min, $row_max ) = $worksheet->row_range();
for my $row( $row_min .. $row_max ){
for my $col(3){
my $cell = $worksheet->get_cell( $row, $col );
next unless $cell;
print "value = ",$cell->value(), "\n";
}
}
}
答案 0 :(得分:1)
你需要使用Spreadsheet :: ParseExcel :: SaveParser而不是Spreadsheet :: ParseExcel,而不是$ parser-> parse,你需要$ parser-> Parse
http://search.cpan.org/~dougw/Spreadsheet-ParseExcel-0.65/lib/Spreadsheet/ParseExcel/SaveParser.pm
这是一个工作版本:
#!/usr/bin/perl -w
use strict;
use warnings;
use Spreadsheet::ParseExcel::SaveParser;
use Scalar::Util qw( looks_like_number );
my $parser = Spreadsheet::ParseExcel::SaveParser->new();
my $workbook = $parser->Parse('30514571.xls');
die $parser->error(), ".\n" if ( !defined $workbook );
for my $worksheet ( $workbook->worksheets() ) {
my ( $row_min, $row_max ) = $worksheet->row_range();
my ( $col_min, $col_max ) = $worksheet->col_range();
for my $row ( $row_min .. $row_max ) {
# for my $col ( $col_min .. $col_max ) {
for my $col ( 3 ) { # 'Rate' Column
my $cell = $worksheet->get_cell( $row, $col );
next unless $cell;
my $value = $cell->value();
next unless looks_like_number($value); # Skip headers / empty
print "[ $row, $col ] value = ",$value, "\n";
if ($value < 0) {
$worksheet->AddCell( $row, $col, 0 );
}
}
}
}
$workbook->SaveAs('30514571.new.xls');