你能从数组元素中调用一个方法吗?

时间:2014-02-25 16:36:11

标签: excel perl xlsx

我有以下代码:

my $workbook  = Excel::Writer::XLSX->new( 'a_simple.xlsx' );
my $worksheet = $workbook->add_worksheet();
my @chart_performance1 = $workbook->add_chart( type => 'column', embedded => 1 );
my $no_of_titles = 3;

for ( my $no_of = 0; $no_of < $no_of_titles; $no_of++ ) {
    $chart_performance1[ $no_of ]->add_series(
        name       => $chart_heading[ 0 ],
        categories => [ 'Sheet1', $array_game_titles[ $no_of ] , $row_range_max , 0, 0 ],
        values     => [ 'Sheet1', $array_game_titles[ $no_of ] , $row_range_max , 1, 1 ],
    );
}

当我运行它时,我收到错误:

Can't call method "add_series" on an undefined value

为什么?

2 个答案:

答案 0 :(得分:3)

这一行

my @chart_performance1 = $workbook->add_chart( type => 'column', embedded => 1 );

看起来不对。 add_chart方法返回单个Excel::Writer::XLSX::Chart对象,因此结果通常分配给标量,而不是数组。目前还不清楚你在问什么,但是如果你想创建一个绘制三个系列数据的图表,那么你想要更像的东西:

my $chart = $workbook->add_chart( type => 'column', embedded => 1 );
...
for ( my $no_of = 0; $no_of < $no_of_titles; $no_of++ ) {
    $chart->add_series( ... );
}

答案 1 :(得分:0)

得到我的答案,只需要声明数组而不是在同一个实例中使用add_chart方法声明和定义数组“chart_performance”。由于add_chart方法返回单个对象,我收到错误。感谢BTW的帮助。

my $workbook  = Excel::Writer::XLSX->new( 'a_simple.xlsx' );
my $worksheet = $workbook->add_worksheet();
my @chart_performance;
my $no_of_titles = 3;

for ( my $no_of = 0; $no_of < $no_of_titles; $no_of++ ) {
    $chart_performance[ $no_of ] = $workbook->add_chart( type => 'column', embedded => 1);
    $chart_performance[ $no_of ]->add_series(
        name       => $chart_heading[ 0 ],
        categories => [ 'Sheet1', $array_row[ $no_of ], $array_col[ $no_of ], 0, 0 ],
        values     => [ 'Sheet1', $array_row[ $no_of ], $array_col[ $no_of ], 1, 1 ],
    );
}
相关问题