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

Re^3: Using Template::Toolkit with Excel files

by Aldebaran (Curate)
on Dec 01, 2018 at 01:44 UTC ( [id://1226575]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Using Template::Toolkit with Excel files
in thread Using Template::Toolkit with Excel files

So I found Excel::Template

I thought you might head down this path and got to work trying to work up an example, but I have failed. The example I was shooting for was the one right on the cpan site: https://metacpan.org/pod/Excel::Template.

I worked it up two different ways and get the same error, so I'm hoping that my failure is diagnostic for the many contributors to the monastery whose experience in this is greater than mine. So far, I only have 2 failures.

At first, I thought my input file wasn't correct on the path, but I took the extra measure of stringifying the Path::Tiny values, with the attending certainty and liternalness of a string, and there is a file there with the xml example in it:

$ ./2.excel.pl abs is /home/bob/Documents/meditations/2.excel.pl path1 is /home/bob/Documents/meditations path2 is /home/bob/Documents/meditations/template_stuff This script will build path2? Abort with ctrl-c. $VAR1 = { 'monk_tags' => bless( [ '/home/bob/Documents/meditations/tem +plate_stuff/1.test.xml', '/home/bob/Documents/meditations/tem +plate_stuff/1.test.xml' ], 'Path::Tiny' ), 'book' => 'excel template example ', 'output' => bless( [ '/home/bob/Documents/meditations/templa +te_stuff/output', '/home/bob/Documents/meditations/templa +te_stuff/output' ], 'Path::Tiny' ) }; string monk is /home/bob/Documents/meditations/template_stuff/1.test.x +ml string save is /home/bob/Documents/meditations/template_stuff/output/3 +0-11-2018-17-15-37.monk.txt Not a GLOB reference at /usr/local/share/perl/5.26.1/Spreadsheet/Write +Excel/OLEwriter.pm line 250. (in cleanup) Not a GLOB reference at /usr/local/share/perl/5.26.1/ +Spreadsheet/WriteExcel/OLEwriter.pm line 418. $ cat 2.excel.pl #!/usr/bin/perl -w use 5.011; use Path::Tiny; use POSIX qw(strftime); use Data::Dumper; # initialization that must precede main data structure # User: enter a subdirectory you would like to create # enter a subdirectory of this^^^ for output my $ts = "template_stuff"; my $output = "output"; # User: enter a name for a template file my $template_file = "1.test.xml"; ## turning things to Path::Tiny my $abs = path(__FILE__)->absolute; my $path1 = Path::Tiny->cwd; my $path2 = path( $path1, $ts ); say "abs is $abs"; say "path1 is $path1"; say "path2 is $path2"; print "This script will build path2? Abort with ctrl-c."; my $prompt = <STDIN>; chomp $prompt; die if ( $prompt eq "n" ); # Path::Tiny creates $ts directory if necessary my $abs_to_template = path( $path2, $template_file )->touchpath; # script params my %vars = ( monk_tags => path( $path2, $template_file ), output => path( $path2, $output ), book => 'excel template example ', ); my $rvars = \%vars; print Dumper $rvars; my $munge = strftime( "%d-%m-%Y-%H-%M-%S", localtime ); $munge .= ".monk.txt"; # use Path::Tiny to create and write to a text in relevant directory my $save_file = path( $vars{output}, $munge )->touchpath; ## stringify input and output files for export my $string_monk = $vars{monk_tags}->stringify; say "string monk is $string_monk"; my $string_save = $save_file->stringify; say "string save is $string_save"; ## end preliminaries # keep time my $start = time; use Excel::Template; # Create the Excel template my $template = Excel::Template->new( filename => $string_monk, ); # Add a few parameters $template->param( HOME => $ENV{HOME}, PATH => $ENV{PATH}, ); $template->write_file($save_file); say time - $start, "seconds elapsed during execution"; say "created file $save_file"; system("gedit $save_file &"); __END__ $

Then I read further on the cpan site that I can get the data from __DATA__ with FILE => \*DATA . I give that a whirl:

$ ./3.excel.pl abs is /home/bob/Documents/meditations/3.excel.pl path1 is /home/bob/Documents/meditations path2 is /home/bob/Documents/meditations/template_stuff This script will build path2. Abort with ctrl-c.$VAR1 = { 'output' => bless( [ '/home/bob/Documents/meditations/templa +te_stuff/output', '/home/bob/Documents/meditations/templa +te_stuff/output' ], 'Path::Tiny' ), 'book' => 'excel template example ', 'monk_tags' => bless( [ '/home/bob/Documents/meditations/tem +plate_stuff/1.test.xml', '/home/bob/Documents/meditations/tem +plate_stuff/1.test.xml' ], 'Path::Tiny' ) }; string monk is /home/bob/Documents/meditations/template_stuff/1.test.x +ml string save is /home/bob/Documents/meditations/template_stuff/output/3 +0-11-2018-10-58-04.monk.txt Not a GLOB reference at /usr/local/share/perl/5.26.1/Spreadsheet/Write +Excel/OLEwriter.pm line 250, <INFILE> line 0.000000. (in cleanup) Not a GLOB reference at /usr/local/share/perl/5.26.1/ +Spreadsheet/WriteExcel/OLEwriter.pm line 418, <INFILE> line 0.000000. $ ./3.excel.pl abs is /home/bob/Documents/meditations/3.excel.pl path1 is /home/bob/Documents/meditations path2 is /home/bob/Documents/meditations/template_stuff This script will build path2. Abort with ctrl-c or n.standard in waits + for you $VAR1 = { 'monk_tags' => bless( [ '/home/bob/Documents/meditations/tem +plate_stuff/1.test.xml', '/home/bob/Documents/meditations/tem +plate_stuff/1.test.xml' ], 'Path::Tiny' ), 'book' => 'excel template example ', 'output' => bless( [ '/home/bob/Documents/meditations/templa +te_stuff/output', '/home/bob/Documents/meditations/templa +te_stuff/output' ], 'Path::Tiny' ) }; string monk is /home/bob/Documents/meditations/template_stuff/1.test.x +ml string save is /home/bob/Documents/meditations/template_stuff/output/3 +0-11-2018-11-07-45.monk.txt Not a GLOB reference at /usr/local/share/perl/5.26.1/Spreadsheet/Write +Excel/OLEwriter.pm line 250, <INFILE> line 0.000000. (in cleanup) Not a GLOB reference at /usr/local/share/perl/5.26.1/ +Spreadsheet/WriteExcel/OLEwriter.pm line 418, <INFILE> line 0.000000. $ cat 3.excel.pl #!/usr/bin/perl -w use 5.011; use Path::Tiny; use POSIX qw(strftime); use Data::Dumper; # initialization that must precede main data structure # User: enter a subdirectory you would like to create # enter a subdirectory of this^^^ for output my $ts = "template_stuff"; my $output = "output"; # User: enter a name for a template file my $template_file = "1.test.xml"; ## turning things to Path::Tiny my $abs = path(__FILE__)->absolute; my $path1 = Path::Tiny->cwd; my $path2 = path( $path1, $ts ); say "abs is $abs"; say "path1 is $path1"; say "path2 is $path2"; say "This script will build path2. Abort with ctrl-c or n."; print "standard in waits for you"; my $prompt = <STDIN>; chomp $prompt; die if ( $prompt eq "n" ); # Path::Tiny creates $ts directory if necessary my $abs_to_template = path( $path2, $template_file )->touchpath; # script params my %vars = ( monk_tags => path( $path2, $template_file ), output => path( $path2, $output ), book => 'excel template example ', ); my $rvars = \%vars; print Dumper $rvars; my $munge = strftime( "%d-%m-%Y-%H-%M-%S", localtime ); $munge .= ".monk.txt"; # use Path::Tiny to create and write to a text in relevant directory my $save_file = path( $vars{output}, $munge )->touchpath; ## stringify input and output files for export my $string_monk = $vars{monk_tags}->stringify; say "string monk is $string_monk"; my $string_save = $save_file->stringify; say "string save is $string_save"; ## end preliminaries # keep time my $start = time; use Excel::Template; # Create the Excel template my $template = Excel::Template->new( FILE => \*DATA ); # Add a few parameters $template->param( HOME => $ENV{HOME}, PATH => $ENV{PATH}, ); $template->write_file($save_file); say time - $start, "seconds elapsed during execution"; say "created file $save_file"; system("gedit $save_file &"); __DATA__ <workbook> <worksheet name="tester"> <cell text="$HOME" /> <cell text="$PATH" /> </worksheet> </workbook> $

Same error:

Not a GLOB reference at /usr/local/share/perl/5.26.1/Spreadsheet/Write +Excel/OLEwriter.pm line 250, <INFILE> line 0.000000. (in cleanup) Not a GLOB reference at /usr/local/share/perl/5.26.1/ +Spreadsheet/WriteExcel/OLEwriter.pm line 418, <INFILE> line 0.000000.

I hope we can figure this out....

Replies are listed 'Best First'.
Re^4: Using Template::Toolkit with Excel files
by llarochelle (Beadle) on Dec 07, 2018 at 15:40 UTC

    Hello Aldebaran, I finally got something neat that addresses 2 requirements : being able to access the XML and creating the XLS file for rendering to the user.

    I'll share a piece of code below, which might answer your question about file/filename in the New() method.

    I'm using 2 modules to achieve my objectives. Template & Excel::Template. I use the Template Toolkit to generate the required XML so that Excel::Template can produce the XLS. So my whole config generator engine benefits from TT and when I need an XLS file it's being treated separately.

    use Template; use Excel::Template; sub xml_to_excel { my ($template_conf, $file, $hashref) = @_; # template configuration +from config file, filename based on request number, data for the tem +plate processing my $xml; my $tt = Template->new({ RELATIVE => 1, # in my case I had to use relative path +because of $template_conf->{file} based on CWD. }); $tt->process( $template_conf->{file}, $hashref, \$xml ) || die $tt->e +rror; # write the output to the $xml string open my $xml_fh, "<", \$xml; # create a filehandle from the $xml str +ing so that Excel::Template can read it. my $ET = Excel::Template->new({ file => $xml_fh, # use the filehandle here }); $ET->write_file($file); return $xml || 0; }

    Hope this helps !

    Luc

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-04-24 02:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found