excel - 无法在未定义的值上调用方法 Worksheet
问题描述
use strict;
use warnings;
use Spreadsheet::ParseExcel;
use Spreadsheet::ParseExcel::SaveParser;
my $excel_file_name = $ARGV[0];
my $parser = Spreadsheet::ParseExcel::SaveParser->new();
my $workbook_orig = $parser->Parse($excel_file_name);
# We will edit column 7 of the first worksheet.
my $worksheet = $workbook_orig->worksheet(0);
my $EDIT_COL = 6;
my ($row_min, $row_max) = $worksheet->row_range();
for my $r ($row_min .. $row_max){
my $cell = $worksheet->get_cell($r, $EDIT_COL);
unless (defined $cell){
next; # Modify as needed to handle blank cells.
}
my $val = $cell->value . '_append_text';
$worksheet->AddCell( $r, $EDIT_COL, $val, $cell->{FormatNo} );
}
# You can save the modifications to the same file, but when
# you are learning, it's safer to write to a different file.
$excel_file_name =~ s/\.xls$/_new.xls/;
$workbook_orig->SaveAs($excel_file_name);
我上面的实现在下面的行中给出了错误“无法在未定义的值上调用方法工作表”。我正在尝试读取 excel 文件并解析该文件,以便稍后向其添加附加内容。我正在使用 ParseExcel 模块执行此操作,如果有任何更好的方法可以解决此问题,或者有任何关于我为什么会收到错误的指导,我们将不胜感激。谢谢。
my $worksheet = $workbook_orig->worksheet(0);
解决方案
Parse 在错误时返回 undef 并且您没有对此进行检查。尝试:
my $workbook_orig = $parser->Parse($excel_file_name);
unless ($workbook_orig) {
die "Got error " . $parser->error() . " parsing spreadsheet.";
}
查看它出现了什么错误,以便您决定如何修复它。
推荐阅读
- android - BottomNavigation - 为什么一个项目的空间与其他项目不同?
- sql-server - 识别仅包含单个值的所有列(例如 0,0.00, Z, Y, N)
- javascript - 在 JavaScript 中从 InOrder 和 PostOrder 构建二叉树
- python - 为什么网页元素的点击功能不起作用(使用 Python 的 Selenium 模块)?
- python-3.x - string.replace ,python 正在擦除句子的其余部分
- django - 删除购物车django
- paypal - 贝宝 PDT 问题
- vhdl - VHDL 函数在精细中的行为与在仿真中的行为不同
- python - 使用函数和 while 循环,我如何将许多票价插入列表形式。用户可以决定需要插入多少票价
- kubernetes - srimzi operator 0.20 kafka 'useServiceDnsDomain' 没有效果