I spent a couple hours banging my head against a problem of trying to change the cell type of a spreadsheet created by OpenOffice::OODoc to something numeric for sorting purposes. The documentation that came with the module wasn't especially helpful in figuring it out, but eventually I stumbled across this article in the CPAN forums that shed some light on the issue. I thought it might be good to create a node in the monastery on the topic, so I'm going to share a little here.

use strict; use warnings; use OpenOffice::OODoc; my $ods_doc = odfDocument(file => 'somefile.ods', create => 'spreadsheet',); # Set table to desired size my $sheet = 0; # First sheet in the file. my $num_rows = 3; my $num_cols = 1; # <- really small spreadsheet. $ods_doc->expandTable($sheet, $num_rows, $num_cols); # If you only need the cell to contain a text string, than # all you have to do is write to it, that's the default. $ods_doc->updateCell($sheet, 0, 0, "The title of the column"); # Even though we're writing a number, it will be escaped # as a string because we haven't change the format of # the cell. $ods_doc->updateCell($sheet, 1, 0, 2); $ods_doc->updateCell($sheet, 2, 0, 12); $ods_doc->save;
In that snippet, 12 will come before 2 because it's doing an ASCII sort. Not very appealing, and all too familiar. By overriding the default cell value type to floating point numbers, we can get the cells to sort properly.
# &c as above. my $cell = $ods_doc->getCell($sheet, 1, 0); $ods_doc->cellValueType($cell, 'float'); $ods_doc->updateCell($sheet, 1, 0, 2); $cell = $ods_doc->getCell($sheet, 2, 0); $ods_doc->cellValueType($cell, 'float'); $ods_doc->updateCell($sheet, 2, 0, 12); $ods_doc->save;
I hope you have found this witty and informative.

In reply to Changing the cell value to a numeric type using OpenOffice::OODoc by starX

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.