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

Help with Excel::Table

by BernieC (Pilgrim)
on Sep 25, 2020 at 14:27 UTC ( [id://11122204]=perlquestion: print w/replies, xml ) Need Help??

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

I'm trying to do some Excel hacking and Excel::Table looks like the perfect module. but I can't get it to open a spreadsheet. my 'open' subroutine is simple:
sub getWB { my ($dir, $excel) = $_[0] =~ /(.*)\/(.*)/ ; my $xs = Excel::Table->new(dir => $dir) ; $xs->open($excel) ; return $xs ; }
and when I try to open the spreadsheet with 'getWB("D:myexcel.xlsx")' I get
Log4perl: Seems like no initialization happened. Forgot to call init() +? no such path [D:\myexcel.xslx] at D:\Perl\spreadsheet.pl line 55.
Any advice at what I'm missing/getting wrong? Thanks

Replies are listed 'Best First'.
Re: Help with Excel::Table
by Athanasius (Archbishop) on Sep 25, 2020 at 15:08 UTC

    Hello BernieC,

    It looks like there’s a mismatch between (1) the regular expression used to separate directory and filename, and (2) the actual input. The regex /(.*)\/(.*)/ splits on a forward slash, but the input contains either a backslash (from the error message) or no slash at all (from the specified input: getWB("D:myexcel.xlsx")).

    It’s safer to use the core module File::Basename here:

    my ($dir, $excel) = fileparse($_[0]);

    Whether this is your real problem, I don’t know, but at the least it’ll remove one layer of possible confusion as you seek to debug the problem. In that case, the monks will require more information — preferably in the form of an SSCCE — to be able to assist with the debugging process.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      The forward slash to backslash is the way things work on windows -- I always use forward slashes in path names and Perl happily flips them to backslashes when it talks to the windows. The program {as y'all will see} shows what the parse returned and that's exactly right. I put in the 'open' to double-check that the hardwired excel path was correct.

      I neglected to include that this is with Strawberry Perl running on a win10/64 system. Here's the little test program:

      #!/usr/bin/perl use v5.10 ; use strict; use warnings ; use Excel::Table ; use constant WORKBOOK => "d:/desktop/testWB.xlsx" ; # test if the path works open(P, "<", WORKBOOK) or die "Can't find the workbook: $!\n" ; close P ; my $xs = getWB(WORKBOOK) ; exit ; sub getWB { my ($dir, $wb) = $_[0] =~ /(.*)\/(.*)/ ; say "Directory is $dir" ; say "Excel file is $wb" ; my $xs = Excel::Table->new(dir => $dir) ; $xs->open($wb) ; return $xs ; }
      I don't know how to upload the Excel worksheet that the file needs {anything will do, since I am only trying to open it and {for the nonce} don't care about whats *in* the file}, but when I run the program I get.
      d:\Desktop>testWb.pl Directory is d:/desktop Excel file is testWB.xlsx Log4perl: Seems like no initialization happened. Forgot to call init() +?

        Thanks for the SSCCE. As toolic says, the error (really just a warning) message appears to relate to Log::Log4perl; the Excel workbook is being opened correctly.

        The example code in the SYNOPSIS section of the documentation for Excel::Table doesn’t seem to work for me. If you have difficulty using this module, you’ll probably be better off directly using Spreadsheet::XLSX — which you already have, since it’s used by Excel::Table!

        Cheers,

        Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: Help with Excel::Table
by Tux (Canon) on Sep 26, 2020 at 11:23 UTC

    Note that Excel::Table currently depands on Spreadsheet::XLSX for modern Excel files (.xlsx). That module is completely unreliable, buggy and unmaintained and should be avoided at all costs. It was very useful at the time, but should be replaced with Spreadsheet::ParseXLSX as soon as possible.

    Maybe Spreadsheet::Read offers an alternative to your wishes.

    As your quest opens a .xlsx file, I felt compelled to make this note.

    use Spreadsheet::Read; my $workbook = Spreadsheet::Read->new ("D:/myexcel.xlsx"); my $sheet = $workbook->sheet (1);

    Enjoy, Have FUN! H.Merijn
Re: Help with Excel::Table
by toolic (Bishop) on Sep 25, 2020 at 23:59 UTC
    If I simplify your code, I am able to silence the "Log4perl" message on STDERR. I figured this out by looking at the tests in the Excel::Table distribution (such as t/Excel-Table1a.t). I think the module has Log::Log4perl enabled by default, and apparently it can be disabled by importing :easy. Since I'm not familiar with Log::Log4perl, this is just a guess.

    I have a directory named "mydir" and a file in it named "test.xlsx". The following code works for me:

    use warnings; use strict; use Excel::Table; use Log::Log4perl qw(:easy); use Data::Dumper; my $xs = Excel::Table->new(dir => 'mydir'); $xs->open('test.xlsx'); print Dumper($xs);

    The "Dumper" code shows me the contents of my Excel file (but is quite verbose).

    I find it hard to follow the module documentation. Good luck trying to figure out how to extract the data you need from your file.

Log In?
Username:
Password:

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

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

    No recent polls found