Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Can Spreadsheet::ParseExcel extract formulas?

by petecm99 (Pilgrim)
on Oct 07, 2010 at 19:21 UTC ( [id://864068]=perlquestion: print w/replies, xml ) Need Help??

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

UPDATE: I had a bug in the code, ParseExcel actually pulled the results of the formula very nicely, then I accidentally over-wrote it - D'OH!

Most holy of holies,

I have a program that reads in an Excel Spreadsheet, massages some data, and creates a new spreadsheet. I just noticed that when I read in a cell that contains a formula (SUM of some cells), it appears to be blank (or null). Is there a way to pull the formula from the cell? Preferably would like the evaluated answer, but the formula itself will suffice in a pinch. Some code snippets below...

my $inputfile = Spreadsheet::ParseExcel::Workbook->Parse($ExcelFileIn) +; my $insheet = $inputfile->Worksheet(0); foreach $row ($insheet->{MinRow} .. $insheet->{MaxRow}) { foreach $col ($insheet->{MinCol} .. $insheet->{MaxCol}) { my $cell = $insheet->{Cells}[$row][$col]; ... do some stuff with $cell ... } }
Many thanks for your insight...

Pete

Replies are listed 'Best First'.
Re: Can Spreadsheet::ParseExcel extract formulas?
by jmcnamara (Monsignor) on Oct 07, 2010 at 23:08 UTC
    It isn't possible to extract a formula from an Excel file using Spreadsheet::ParseExcel. The documentation should be clearer on this.

    The main reason is that formulas are stored in Excel as parsed symbols in an RPN structure. In theory it would be possible to deparse this back into the textual representation of the formula but it is one of those large time consuming features that I and the previous maintainers never got around to.

    --
    John.

      Thanks for clarifying that, John!
Re: Can Spreadsheet::ParseExcel extract formulas?
by Corion (Patriarch) on Oct 07, 2010 at 19:29 UTC
      I did a search in the CPAN module documentation for 'formula' - nothing jumped out at me. There was a blurb about reading formulas created by WriteExcel -

      "This module cannot read the values of formulas from files created with Spreadsheet::WriteExcel unless the user specified the values when creating the file (which is generally not the case). The reason for this is that Spreadsheet::WriteExcel writes the formula but not the formula result since it isn't in a position to calculate arbitrary Excel formulas without access to Excel's formula engine."

      My input spreadsheet was not created by WriteExcel, so I assume this comment does not apply. I also looked in my site/lib install folder for examples, there were none. There are plenty for WriteExcel, but ParseExcel is rather sparse.

        Quoting Spreadsheet::ParseExcel

        TODO

        • ...
        • Add Formula support, Hyperlink support, Named Range support.

        I would assume from that that formulae are currently unsupported.

Re: Can Spreadsheet::ParseExcel extract formulas?
by dasgar (Priest) on Oct 07, 2010 at 21:11 UTC

    If you're on a Windows box with Excel installed, you do have another route available to: Win32::OLE. With this module, you can get the following items from a cell: value, formatted value, and formula. However, if you're on a *nix or Mac, I'm not sure what to suggest as an alternative.

    You'll definitely have a steeper learning with Win32::OLE, but it's advantage is that you can do more in an Excel file than you can with Spreadsheet::ParseExcel. However, Spreadsheet::ParseExcel has the advantage when you don't have Excel for Perl to manipulate through Win32::OLE, such as working in a *nix environment. Overall, which one is better depends on the programmer, his/her environment and what he/she needs to do.

      Much appreciated, I'll dig further...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (2)
As of 2025-12-15 02:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (95 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.