首页 > 解决方案 > 无法在 perl 脚本中获取 row_range

问题描述

我的代码似乎没有得到 row_range()。它显示一个错误,即:

无法通过 clamp_init_value.pl 第 41 行的包“Spreadsheet::ParseExcel::Worksheet”定位对象方法“row_range”。

 #!/usr/bin/perl

 use strict;
 use warnings;
 use Spreadsheet::ParseExcel;

 my $filename = '../../doc/Book1.xls';
 my $parser   = Spreadsheet::ParseExcel->new();
 my $workbook = $parser -> Parse( $filename);

 if ( !defined $workbook ) {
    die "-E-: cannot parse <$filename>.\n";
 }

 for my $worksheet ( $workbook-> Worksheet( 'family pin list' ) ) {

    # Find out the worksheet ranges
    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 ) {

            # Return the cell object at $row and $col
            my $cell = $worksheet->get_cell( $row, $col );
            next unless $cell;

            print "Row, Col    = ($row, $col)\n";
            print "Value       = ", $cell->value(),       "\n";

        }
    }
}

标签: perl

解决方案


稍微扩展TFBW 的答案......

此模块的更改文件包括以下条目:

0.43 January 7 2009

    + Restructured and rewrote the main documentation. This is the start of
      a general refactoring. If you would like to keep up to date with it
      keep an eye on the Spreadsheet::ParseExcel Google Group.
      http://groups.google.com/group/spreadsheet-parseexcel

    + Added worksheets() Workbook method to iterate over the Worksheet objects.

    + Added unformatted() method to get a Cell's unformatted value.

    + Renamed public methods RowRange(), ColRange() and Cell() to row_range(),
      col_range() and get_cell(). Old methods are still available.

    ! Turned on compatibility_mode() by default in SaveParser to avoid SP3
      problems.

    ! Fixed minor SaveParser bug with font rotation.
      http://rt.cpan.org/Public/Bug/Display.html?id=41626

因此,如果您的模块版本早于 0.43 版,则调用您想要的方法RowRange()。您可以通过在命令行提示符处键入以下内容来查找已安装的模块的版本:

perl -MSpreadsheet::ParseExcel -le'print $Spreadsheet::ParseExcel::VERSION'

如果您确实有一个超过十年的模块版本,那么我强烈建议您更新它。


推荐阅读