2014-02-25 70 views
-1

我有以下代碼:你可以調用一個數組元素的方法嗎?

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 

爲什麼?

+3

你的問題是什麼? – ThisSuitIsBlackNot

+0

我們可以用這種方式調用模塊函數「add_series」。 – LohitRaj

+0

@LohitRaj當你嘗試時會發生什麼?閱讀有很多例子的文檔。 –

回答

0

得到了我的答案,只需要聲明數組,而不是使用add_chart方法在同一個實例中聲明和定義數組「chart_performance」。由於add_chart方法返回一個單一的對象,我得到了錯誤。感謝您的幫助。

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 ], 
    ); 
} 
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(...); 
} 
+0

雅我試圖將圖表對象分配給一個數組,以獲得不同的圖表爲3個不同的標題數據系列。 – LohitRaj

相關問題