0
votes

I am trying to use the Spreadsheet::ParseExcel::SaveParser plugin using example code at both metacpan.org and on SO and I cannot define the template (workbook).

I have played around with variations on the new statement, quotes, file parh, etc - nothing works. I put a die after the template statement and it prints the error message. Without that I have either a $template->worksheet() or worksheets() statement and if I skip the die I get a different message. I confirmed that the path to the Excel file is correct. I also new()'ed Spreadsheet::ParseXLSX instead and the code got past the template undefined problem - of course it crashed when I tried to do an AddCell.

    use Spreadsheet::ParseExcel;
    use Spreadsheet::ParseExcel::SaveParser;

    my $saveParser  = Spreadsheet::ParseExcel::SaveParser->new();
    my $template    = $saveParser->Parse("some Excel file verified to exist");
    die "Error! Template not defined!\n" if (!defined($template));

dies

    use Spreadsheet::ParseExcel;
    use Spreadsheet::ParseExcel::SaveParser;

    my $saveParser  = Spreadsheet::ParseExcel::SaveParser->new();
    my $template    = $saveParser->Parse("some Excel file verified to exist");
    my $worksheet = $template->worksheet(0);

Can't call method "worksheet" on an undefined value at ../bin/update_tp.pl line nnn. It also errors out if I use the worksheet name instead of number.

Obviously I expect the $saveParser->Parse command to return a valid object so I can work with it - it doesn't. FYI all modules I'm using are at the current rev of 0.65 except WriteExcel (which isn't relevant yet), which is 2.4.

1
You have use Spreadsheet::ParseXLSX, I think you need use Spreadsheet::ParseExcel instead. See also How to read data from xlsx in perl - Håkon Hægland
I need to use SaveParser because I want to read/modify/write an Excel spreadsheet. - hal9e3
Ok, I understand that I am just asking if you have you tried to use the Excel version instead of the XLSX version? - Håkon Hægland
OK, fixed. That was a typo in the question, there is no ParseXLSX::SaveParser module. - hal9e3
Yep, that's at least one problem, I'm trying to use a ,xlsx; unfortunately, all the other data files are .xlsx, not sure I'll be able to convert them going forward, stayed tuned... - hal9e3

1 Answers

0
votes

Think you'd want to track down what error-handling is available for the Parse() function by reading the CPAN page for this module. Find and adapt that error-handling to give you an inkling of why Parse() would be failing. Your error about "worksheet on an undefined value" is indicative of that call failing. Could be you're not escaping file paths, maybe you don't have read permissions on the file, lots of other reasons Parse() could be failing. Eww, just went and looked myself, and there's fairly inadequate error-handling documented for that function. Maybe try die'ing with some error-handling variables? See https://perldoc.perl.org/perlvar.html#Error-Variables