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

sub add_chart_func { my $name = shift; my $chart = $workbook->add_chart( name => "$name",type => 'line' , +embedded => 1); $chart->set_title( name => "$name" ); $chart->add_series( categories => xl_range_formula( 'sheet', 1+$Graph_table_last_ro +w, $row, 1, 1 ), values => xl_range_formula( 'sheet', 1+$Graph_table_last_ro +w, $row, 2, 2 ), name => "$var1", ); $chart->add_series( categories => xl_range_formula( 'sheet', 1+$Graph_table_last_ro +w, $row, 1, 1 ), values => xl_range_formula( 'sheet', 1+$Graph_table_last_ro +w, $row, 3, 3 ), name => "$var2", ); $Graph_table_last_row = $Graph_table_last_row + 3; $grpsheet->insert_chart( "J$Graph_table_last_row", $chart, 10, 10, + 1.7, 1.5); $Graph_table_last_row = $row + 5; $graph_table_no++; }

$Graph_table_last_row is a global variable which contains the cell from top from where next chart will be drawn.

If I add '10' charts by calling the above function 10 times, In first 9 charts, the attributes on x-axis are printed properly. But in last chart, the attributes are printed alternately. I mean to say, while printing chart...x-axis attibutes are printed as 0,2,4,6,8,10. But at the place of 1,3,5,7,9 nothing is displayed. What shall I do to resolve this issue?? Regards.

Replies are listed 'Best First'.
Re: Excel editing perl
by GrandFather (Saint) on Sep 29, 2011 at 10:43 UTC

    The first thing I would do is remove all the global variables. It's not possible for us to diagnose your issue because we can't tell what may affect the global variables outside the scope of add_chart_func.

    There are various ways you can remove the need for global variables. The most obvious thing is simply to pass everything in as parameters. With the seven or so global variables used here that's getting to be a fairly large number of parameters to keep track of, but is an easy first step. Of course some of those variables are updated and there are several ways you could handle that too, but the simplest is probably just to return the updated values. Consider:

    sub Main { my $workbook; my $name; my $Graph_table_last_row; my $row; my $var1; my $var2; my $grpsheet; my $graph_table_no; ... ($Graph_table_last_row, $graph_table_no) = add_chart_func ($workbook, $name, $Graph_table_last_row, $row, + $var1, $var2, $grpsheet, $graph_table_no); ... } sub add_chart_func { my ($workbook, $name, $Graph_table_last_row, $row, $var1, $var2, $ +grpsheet, $graph_table_no) = @_; ...; $Graph_table_last_row = $row + 5; $graph_table_no++; return $Graph_table_last_row, $graph_table_no; }

    which has the advantage of explicitly showing the variables used by each sub and showing which variables are updated by each sub. If refactoring your code in that fashion doesn't turn up the problem then you need to mock up the Excel related calls in some fashion and post code we can run (without needing Excel or any OLE related modules) that demonstrates the problem you are seeing.

    True laziness is hard work
      Hi, I even tried keep that global variable as local and passing it as parameter to the required function. But it produces the same result. The problem is :-

      If I generate 10 charts, 9 charts are correct.

      But the last chart has problem. The labeling done at x-axis of last chart is missing every alternative attribute.

      what shall i do to resolve this issue??

      Regards

        The advice from GrandFather about global variables was not about correcting your bug but making your script a) debuggable and b) readable by others.

        The next thing to do is either a) use print statements or the built-in debugger to tell you for example what data the subroutine gets per invocation and what it does with it or b) reproduce your refinded script on this website to let perlmonks help you with the search for the bug (ideally a minimal(!) runable(!) script that shows the problem)