Hong has asked for the wisdom of the Perl Monks concerning the following question:

I tried to run following code from http://search.cpan.org/dist/Spreadsheet-WriteExcel/lib/Spreadsheet/WriteExcel/Chart/Pie.pm page, got error message "Can't locate object method "add_chart" via package "Spreadsheet::WriteExcel" ". I checked that "add_chart" exists in Workbook.pm

use strict; use Spreadsheet::WriteExcel; my $workbook = Spreadsheet::WriteExcel->new( 'chart_pie.xls' ); my $worksheet = $workbook->add_worksheet(); my $bold = $workbook->add_format( bold => 1 ); # Add the worksheet data that the charts will refer to. my $headings = [ 'Category', 'Values' ]; my $data = [ [ 'Apple', 'Cherry', 'Pecan' ], [ 60, 30, 10 ], ]; $worksheet->write( 'A1', $headings, $bold ); $worksheet->write( 'A2', $data ); # Create a new chart object. In this case an embedded chart. my $chart = $workbook->add_chart( type => 'pie', embedded => 1 ); # Configure the series. $chart->add_series( name => 'Pie sales data', categories => '=Sheet1!$A$2:$A$4', values => '=Sheet1!$B$2:$B$4', ); # Add a title. $chart->set_title( name => 'Popular Pie Types' ); # Insert the chart into the worksheet (with an offset). $worksheet->insert_chart( 'C2', $chart, 25, 10 ); #print "chart:@chart\"; __END__

Replies are listed 'Best First'.
Re: Can't locate object method "add_chart"
by kcott (Archbishop) on Jan 25, 2017 at 05:50 UTC

    G'day Hong,

    Welcome to the Monastery.

    ""Can't locate object method "add_chart" via package "Spreadsheet::WriteExcel" ". I checked that "add_chart" exists in Workbook.pm" [my emphasis]

    It can't find add_chart via WriteExcel.pm, not via Workbook.pm.

    So, WriteExcel.pm is where to start your checking.

    — Ken

Re: Can't locate object method "add_chart"
by poj (Abbot) on Jan 25, 2017 at 11:27 UTC

    Run this to check which version you have, it needs to be 2.34 (2010-08-01) or above

    use strict; use Spreadsheet::WriteExcel; print $Spreadsheet::WriteExcel::VERSION;
    poj
Re: Can't locate object method "add_chart"
by Anonymous Monk on Jan 25, 2017 at 01:35 UTC
Re: Can't locate object method "add_chart"
by madtoperl (Hermit) on Jan 25, 2017 at 13:42 UTC
    Have you installed Spreadsheet::WriteExcel properly from CPAN or PPM? i.e did not copy Workbook.pm or other dependent modules from other locations.
    You may try this to update the module and check if the problem is getting resolved.
    CPAN: perl -MCPAN -e "install 'Spreadsheet::WriteExcel'" Activeperl ppm install Spreadsheet-WriteExcel