Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

get_xf_index error

by ag4ve (Monk)
on Jul 24, 2012 at 05:11 UTC ( [id://983293]=perlquestion: print w/replies, xml ) Need Help??

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

i posted this couple of hours ago to the spreadsheet-writeexcel mailing list and haven't seen any response yet: https://groups.google.com/forum/?fromgroups#!topic/spreadsheet-writeexcel/qqR-RSffesc i'm hoping i can get a quicker response here. thanks.

why am i getting this: (in cleanup) Can't call method "get_xf_index" on unblessed reference at /usr/local/share/perl/5.10.1/Excel/Writer/XLSX/Worksheet.pm line 5599.

when i try to run this: https://gist.github.com/3166010

btw, when i just do ->write(0,0, $comp) it writes around the wrong axis but doesn't give the error.

Replies are listed 'Best First'.
Re: get_xf_index error
by jmcnamara (Monsignor) on Jul 24, 2012 at 09:27 UTC

    At a guess I'd say that the $worksheet object has been garbage collected before you try to use it in colcmp():

    sub colcmp { ... $worksheet->write_rich_string($comp_row, $col->[0], join(",", +@wmap)); }
    You can verify that by adding a $workbook->close() to the end of your program.

    --
    John.

Re: get_xf_index error
by MidLifeXis (Monsignor) on Jul 24, 2012 at 12:27 UTC

    May I suggest posting the contents of the gist posted above? PerlMonks discourages having data related to a question off-site because it may not be there long term. Include it in <code>...</code> tags.

    --MidLifeXis

      i figured it out. i was passing write_col an array instead of a ref. so this works: $worksheet->write_col(0, 0, $comp);. i think the error handling could be improved though.

      as for the code dump, here's what i have right now i had some issues in my logic i'm trying to solve so it's changed more than just resolving this issue:

      #!/usr/bin/perl use strict; use warnings; use Data::Dumper; use List::UtilsBy qw/max_by/; use Getopt::Long; use Pod::Usage; use Spreadsheet::ParseExcel; use Excel::Writer::XLSX; my $opts; $opts->{comp} = shift @ARGV; $opts->{with} = shift @ARGV; GetOptions( 'output|o=s' => \$opts->{out}, 'colmap|c=s' => \$opts->{col}, 'format|f=s' => \$opts->{format}, 'help|h' => \$opts->{help}, 'man|m' => \$opts->{man} ) or pod2usage({ -verbose => 0, -output => \*STDERR, -msg => "$0 no parameter found.\n Use --help for more options.\n" } ); if ($opts->{man}) { pod2usage( -verbose => 2 ); } elsif ($opts->{help} or !defined($opts->{comp}) or !defined($opts->{ +with})) { pod2usage( -verbose => 0, -output => \*STDERR, -msg => "$0 [options]" ); } my $workbook = Excel::Writer::XLSX->new($opts->{'out'} // 'out.xlsx'); my $worksheet = $workbook->add_worksheet(); my $fmt = $workbook->add_format(color => $opts->{format} // 'blue'); my @cols = map {/(\d+):(\d+)/ ? [$1, $2] : die "Malformed pair: $_"} s +plit(' ', $opts->{col}) if $opts->{col}; my ($comp, $comp_parm) = xltree($opts->{comp}); my ($with, $with_parm) = xltree($opts->{with}); $worksheet->write_col(0, 0, $comp); if (@cols) { foreach my $col (@cols) { colcmp($worksheet, $comp, $with, $comp_parm, $with_parm, $col); } } else { foreach my $col ($comp_parm->[2] .. $comp_parm->[3]) { colcmp($worksheet, $comp, $with, $comp_parm, $with_parm, $col); } } $workbook->close(); sub xltree { # from excel file, return array $data structure and an ar +ray of $row_min, $row_max, $col_min, $col_max; my ($file) = @_; my $parser = Spreadsheet::ParseExcel->new(); my $workbook = $parser->parse($file); if (!defined($workbook)) { die "Problem with $file: " . $parser->error() . ".\n"; } my $worksheet = $workbook->worksheet(0); my ($row_min, $row_max) = $worksheet->row_range(); my ($col_min, $col_max) = $worksheet->col_range(); my $data; for my $row ($row_min .. $row_max) { for my $col ($col_min .. $col_max) { my $cell = $worksheet->get_cell($row, $col); $data->[$row][$col] = defined($cell) ? $cell->unformatted : " + "; } } return $data, [$row_min, $row_max, $col_min, $col_max]; } sub colcmp { my ($worksheet, $comp, $with, $comp_parm, $with_parm, $col) = @_; for my $comp_row ($comp_parm->[0] .. $comp_parm->[1]) { my @row_i; for my $with_row ($with_parm->[0] .. $with_parm->[1]) { $row_i[$with_row] = grep { $_ ~~ [split(" ", $with->[$with_ro +w][$col->[1]])] } split(" ", $comp->[$comp_row][$col->[0]]); } my $max_match = max_by { $row_i[$_] } 0 .. $#row_i; my @wmap = map { $with->[$max_match][$col->[1]] =~ /$_/ ? ($fmt, + "$_ ") : "$_ " } split(" ", $comp->[$comp_row][$col->[0]]); $worksheet->write_rich_string($comp_row, $col->[0], @wmap); } }
Re: get_xf_index error
by Anonymous Monk on Jul 24, 2012 at 07:30 UTC

    Guessing from the error message, because of the closure

    my $worksheet ... sub colcmp { ... $worksheet

    Usually the remedy is to write your functions like

    colcmp( $worksheet, ... ); sub colcmp { my( $worksheet, ... }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://983293]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-04-24 21:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found