Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Excel::Template and a hash of arrays of hashes

by kwaping (Priest)
on Dec 29, 2006 at 16:28 UTC ( [id://592244]=perlquestion: print w/replies, xml ) Need Help??

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

I am trying to use Excel::Template (hi dragonchild!) to create an Excel workbook. Each worksheet should be titled with the name of a sales rep, and on that page should be four columns of data. Here's the relevant code.
Building the hash:
foreach my $row (@data) { push @{$hash{$row->[0]}}, { companyid => $row->[1], apptype => $row->[2], state => $row->[3], count => $row->[4], }; }
Sample data:
%hash = ( 'One' => [ { 'apptype' => 'xxx', 'count' => '123', 'companyid' => 'xxx', 'state' => 'xx' }, { 'apptype' => 'xxx', 'count' => '123', 'companyid' => 'xxx', 'state' => 'xx' }, { 'apptype' => 'xxx', 'count' => '123', 'companyid' => 'xxx', 'state' => 'xx' } ], 'Two' => [ { 'apptype' => 'xxx', 'count' => '123', 'companyid' => 'xxx', 'state' => 'xx' }, ] );
My first stab at building the params, not even close to working:
foreach my $rep (sort keys %hash) { $template->param( REP => { rep => $rep, DATA => $hash{$rep} }, ); }
And the XML to build the worksheet:
<workbook> <loop name="REP"> <worksheet name="$rep"> <row> <cell text="QM Distribution" /> </row> <row> </row> <row> <cell text="Comp_Recid" /> <cell text="ApplicationType" /> <cell text="State" /> <cell text="Count (*)" /> </row> <loop name="DATA"> <row> <cell text="$companyid" /> <cell text="$apptype" /> <cell text="$state" /> <cell text="$count" /> </row> </loop> </worksheet> </loop> </workbook>
Any suggestions to help me get this working are appreciated!

Update: fixed some obviously bad syntax in the param code.

---
It's all fine and dandy until someone has to look at the code.

Replies are listed 'Best First'.
Re: Excel::Template and a hash of arrays of hashes
by wfsp (Abbot) on Dec 29, 2006 at 17:11 UTC
    I can't help directly but if, as the docs say, you're looking for a data structure similar to that used by HTML::Template this may help get you started.

    #!/usr/bin/perl use strict; use warnings; use HTML::Template; use Data::Dumper; $Data::Dumper::Indent = 1; my %h = get_hash(); my @outer; for my $rep (keys %h){ my @inner; for my $detail (@{$h{$rep}}){ push @inner, $detail; } push @outer, { rep => $rep, inner => [@inner], } } my $param = {outer => [@outer]}; print Dumper $param; exit; # the following not directly relevant my $t = HTML::Template->new(filename => 'kwaping.html') or die "can't parse template: $!"; $t->param($param); print $t->output; sub get_hash{ return ( 'One' => [ { 'apptype' => 'xxx', 'count' => '123', 'companyid' => 'xxx', 'state' => 'xx' }, { 'apptype' => 'xxx', 'count' => '123', 'companyid' => 'xxx', 'state' => 'xx' }, { 'apptype' => 'xxx', 'count' => '123', 'companyid' => 'xxx', 'state' => 'xx' } ], 'Two' => [ { 'apptype' => 'xxx', 'count' => '123', 'companyid' => 'xxx', 'state' => 'xx' }, ] ); }
    Data::Dumper output:
    ---------- Capture Output ---------- > "C:\Perl\bin\perl.exe" _new.pl $VAR1 = { 'outer' => [ { 'inner' => [ { 'apptype' => 'xxx', 'count' => '123', 'companyid' => 'xxx', 'state' => 'xx' } ], 'rep' => 'Two' }, { 'inner' => [ { 'apptype' => 'xxx', 'count' => '123', 'companyid' => 'xxx', 'state' => 'xx' }, { 'apptype' => 'xxx', 'count' => '123', 'companyid' => 'xxx', 'state' => 'xx' }, { 'apptype' => 'xxx', 'count' => '123', 'companyid' => 'xxx', 'state' => 'xx' } ], 'rep' => 'One' } ] }; > Terminated with exit code 0.
      Thank you wfsp, that did the job perfectly! I owe you a beverage of your choice sometime.

      The code:
      my @outer; foreach my $rep (sort keys %hash) { my @inner; foreach my $detail (@{ $hash{$rep} }) { push @inner, $detail; } push @outer, { rep => $rep, inner => \@inner, } } $template->param( { outer => \@outer } );
      The XML:
      <workbook> <loop name="outer"> <worksheet name="$rep"> <row> <cell text="Comp_Recid" /> <cell text="ApplicationType" /> <cell text="State" /> <cell text="Count (*)" /> </row> <loop name="inner"> <row> <cell text="$companyid" /> <cell text="$apptype" /> <cell text="$state" /> <cell text="$count" /> </row> </loop> </worksheet> </loop> </workbook>

      ---
      It's all fine and dandy until someone has to look at the code.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-04-23 21:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found