English 中文(简体)
Perl - 电子表格:::XLSX 指工作手册中的具体工作表
原标题:Perl - Spreadsheet::XLSX refer to a specific sheet in the workbook
  • 时间:2012-05-23 17:40:41
  •  标签:
  • perl

继续学习在珀尔散步(爬行? )

我有 Perl 代码, 我基本上尝试通过阵列字符串循环, 并在我的工作手册中找到这些工作表( 目前, 我100%肯定有一张工作手册中提供姓名的表格)。 $Worksholp 变量似乎被分配到基于 $Worksholp 的精细工作表上。

print $worksheet->(Name);

测试线。但是,下一行

my $cell = $worksheet->get_cell(1,1);

似乎没有设置单元格, 因为下面的行不打印单元格值( 我知道这是为本案设定的) 。 我知道这一点, 因为如果我在

my $value = $cell->value() if ($cell);

我知道错误:

Can t call method "value" on an undefined value at script.pl line 14

完整代码在此 :

use strict;
use warnings;
use Spreadsheet::XLSX;

my $excel = Spreadsheet::XLSX -> new ( C:Scott.xlsm ,);
my @sheets = qw(Fund_Data GL_Data);

foreach my $sheet (@sheets) {
    my $worksheet = $excel->Worksheet($sheet);
    print $worksheet->{Name}, "
"; #just a test to make it is being set to worksheet
    my $cell = $worksheet->get_cell(1,1);
    my $value = $cell->value() if ($cell);
    print $value, "
" if ($value);
}

我的结果很简单:

Fund_Data
GL_Data

何时应该

Fund_Data
you  --> (Range("A1").Value in Sheet("Fund_Data"))
GL_Data
me  --> (Range("A1").Value in Sheet("GL_Data"))

此外,我建立上述代码的依据不在下面的代码中,而下面的代码是经过试验和测试的。上面代码的主要区别是,我试图根据数组值来分配我想要的表格,而不是绕过每张工作表,而是试图根据数组值来分配工作表。

use strict;
use warnings;
use Spreadsheet::XLSX;

my $excel = Spreadsheet::XLSX -> new ( P:VBAHelpBook3.xlsx ,);

foreach my $sheet (@{$excel -> {Worksheet}}) {
    printf("Sheet:  %s
", $sheet->{Name});
    my $cell = $sheet->get_cell(2,1);
    my $value = $cell->value();
    printf("Cell value is: $value");
}
最佳回答

我使用数据:Dumper on $Workshit, 结果如下(部分结果):

$VAR1 = bless( {
                  DefColWidth  =>  8.43
                  MinCol  => 0,
                  MaxRow  => 25,
                  MinRow  => 0,
                  path  =>  worksheets/
                  MaxCol  => 0,
                  Name  =>  Fund_Data ,

MinCol gt; 0, MinRow gt; 0, 告知我我的单元格参考值为零。 当我更改时

my $cell = $worksheet->get_cell(1,1);

my $cell = $worksheet->get_cell(0,0);

我得到了我想要的结果!

我不仅得到Q回答,还学会了 一个很棒的内置PERL功能!

问题回答

暂无回答




相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签