【问题标题】:count excel rows dynamically using perl使用perl动态计算excel行数
【发布时间】:2012-11-16 02:40:06
【问题描述】:

我正在编写一个解析 Excel 文件的 Perl 脚本。此脚本的目的是计算第 1 列中的每个单元格值,即第 2 列中的值的数量。

例如一个如下所示的 Excel 文件:

12    abc
12    abc
12    efg
12    efg
13    hij
13    hij
13    klm

我的脚本会返回:

对于单元格值 12,我有:

2 values "abc", 2 values "efg" and for cell value 13 i have : 2 values "hij" and 1 value "klm". 

我的脚本看起来像这样(我从 perl 文档中获取了这个例子):

 use Spreadsheet::XLSX;

 my $excel = Spreadsheet::XLSX -> new ('Book1.xlsx');

 foreach my $sheet (@{$excel -> {Worksheet}}) {

    printf("Sheet: %s\n", $sheet->{Name});

    $sheet -> {MaxRow} ||= $sheet -> {MinRow}; 

     foreach my $row ($sheet -> {MinRow} .. $sheet -> {MaxRow}) {

            $sheet -> {MaxCol} ||= $sheet -> {MinCol};

            foreach my $col ($sheet -> {MinCol} ..  $sheet -> {MaxCol}) {

                    my $cell = $sheet -> {Cells} [$row] [$col];

                    if ($cell) {
                        #here I should count the cell values 
                    }
                print $cell;
            }

    }


 }

我不知道如何做到这一点,因为我以前从未使用过 perl,而且我无法在网上找到与我想要的完全匹配的示例。任何帮助将不胜感激。 谢谢

【问题讨论】:

  • 如果你不介意:为什么是 Perl 而不是 VBA?
  • 老实说,这不是我的选择。这是我在公司实习项目的一部分。
  • @Brad:因为简单的事情就应该简单?
  • @runrig 如果您了解 Perl,这很简单,如果您了解 VBA,这很简单。除了 perl 脚本可以与 Excel 文件一起保存/嵌入吗?
  • @Brad:VBA 会解析没有安装 Excel 的 Excel 文档吗?

标签: perl excel


【解决方案1】:

也许下面的注释脚本会有所帮助:

use strict;
use warnings;
use Spreadsheet::XLSX;
use Data::Dumper;

# No need to iterate through columns, so set val for col 1
my $col1 = 0;
my %hash;

my $excel = Spreadsheet::XLSX->new('Book1.xlsx');

# Just get the first sheet
my $sheet = ${ $excel->{Worksheet} }[0];

# Calculate the range of rows
$sheet->{MaxRow} ||= $sheet->{MinRow};

# Iterate through each row
foreach my $row ( $sheet->{MinRow} .. $sheet->{MaxRow} ) {

    # The cell in column 1
    my $cell = $sheet->{Cells}[$row][$col1];

    if ($cell) {

        # The adjacent cell in column 2
        my $adjacentCell = $sheet->{Cells}[$row][ $col1 + 1 ];

        # Use a hash of hashes
        $hash{ $cell->{Val} }{ $adjacentCell->{Val} }++;
    }
}

# Numerically sort the keys; the value is a hash reference
for my $key1 ( sort { $a <=> $b } keys %hash ) {
    print "For cell value $key1: ";

    # Dereference the hash reference and get the keys/values
    while ( my ( $key2, $val2 ) = each %{ $hash{$key1} } ) {
        print qq{$val2 value(s) "$key2" };
    }
    print "\n";
}

# Show the hash structure
print "\n", Dumper \%hash;

输出:

For cell value 12: 2 value(s) "abc" 2 value(s) "efg" 
For cell value 13: 1 value(s) "klm" 2 value(s) "hij" 

$VAR1 = {
          '13' => {
                    'klm' => 1,
                    'hij' => 2
                  },
          '12' => {
                    'abc' => 2,
                    'efg' => 2
                  }
        };

您可以执行以下操作来显示与键 '13' 关联的值:

# Show only the value(s) for key '13'
print "For cell value 13: ";

# Dereference the hash reference for key '13' and get the keys/values
while ( my ( $key2, $val2 ) = each %{ $hash{13} } ) {
    print qq{$val2 value(s) "$key2" };
}

输出:

For cell value 13: 1 value(s) "klm" 2 value(s) "hij"

【讨论】:

  • 非常感谢,这非常有效。我需要花更多的时间在 perl 上,这是肯定的!只是一个问题,我可以用这个哈希访问单元格值吗?我只想计算单元格“13”,klm + hij。这可能吗?谢谢你:)
  • @user1734229 - 很高兴这对你有用。已添加代码以显示与键“13”关联的值。
【解决方案2】:

使用哈希。计数$hash{$column1}{$column2}++。遍历键并打印计数值。是的,我给你留了一些工作来填充 column1、column2 的值,并迭代哈希。

【讨论】:

  • 感谢您的提示,所以我不再需要遍历 $col 和 $row 了吗?如何打印哈希,打印 $hash 给我 HASH(0x349adc)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多