Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Weird behaviour with libreoffice spreadsheet and OpenOffice::OODoc

by Anonymous Monk
on Mar 24, 2016 at 11:55 UTC ( [id://1158711]=perlquestion: print w/replies, xml ) Need Help??

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

I'm trying to update a libreoffice spreadsheet (test.ods) with perl's module OpenOffice::OODoc, and I am coming across spurious behaviour which I'm not sure if it's from my poor understanding, a bug in OpenOffice::OODoc or simply an artifact with libreoffice. Here's the test code:

use OpenOffice::OODoc; my $document=odfDocument(file=>'test.ods'); foreach my $rr (0..10){ $document->updateCell("Sheet1",$rr,0,$rr."0"); } $document->save;

The code is simply meant to place into column A for the first 10 rows, the row number followed by a '0' (i.e., '00', '10', '20'...)

Before this code is ran, test.ods was blank except for the number 999 placed in cells "C1", "B2 and "F3". The cell "A5" was selected. So roughly, it looked like:
   
      999         
999
999
 
 

After the perl code was ran, the spreadsheet looked like:
0000999
10999
2020202020999
303030303030
404040404040

It seems like the line $document->updateCell(...); does not update just one cell, but rather all cells on that line, starting at the specified column and ending at either the first non-black cell or end of spreadsheet's extent in columns. Also, it only does this to up to row 5, where the cell is selected, presumably to the spreadsheet's extent in rows.

Would somebody please explain what I am doing wrong?
-perl version: 5.16.3
-OpenOffice::OODoc version: 2.125
-libreoffice version: 4.1.6.2

Replies are listed 'Best First'.
Re: Weird behaviour with libreoffice spreadsheet and OpenOffice::OODoc
by tangent (Parson) on Mar 24, 2016 at 16:33 UTC
    OpenOffice documents are in a compressed XML format and before you can add rows and columns you need to account for this compression. From the OpenOffice::OODoc docs:
    Caution: The direct cell addressing works only when the table XML storage is "normalized", i.e. when every table object (row, column or cell) is mapped to an exclusive XML element... with OpenOffice.org Calc spreadsheets, several contiguous objects are mapped to a single XML element as long as they have the same content, the same type and the same presentation.
    And also:
    before addressing cells in a spreadsheet document, a program must "declare" the size of the used area in each target sheet
    So you will need to call expandTable() to the correct size, which also "normalizes" the sheet, and then use getCell() / updateCell(), or the shortcut cellValue(), to insert the new cell. But there is a gotcha with expandTable() - from the OpenOffice::OODoc::Text docs:
    expandTable(table, height, width)

    The vertical expansion is implemented by repetitive replication of the last row, while the horizontal expansion is implemented by repetitive replication of the last cell of the last row. So the new cells in the right side are copies of the old bottom-right cell, while the new rows are copies of the old last row.
    So you will need to deal with any values in the last row and column. It may be easier to copy the old values and create a new document, or if you need to retain the formatting of the old document you could empty the values, resize the sheet, and then re-insert them.
Re: Weird behaviour with libreoffice spreadsheet and OpenOffice::OODoc
by GotToBTru (Prior) on Mar 24, 2016 at 13:16 UTC

    I would try selecting the first cell you want to update before executing the updateCell. The interpretation of commands in spreadsheets often depends on context, such as what is currently selected.

    If the column/row numbers are zero based, your for will update 11 rows, not 10.

    But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1158711]
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 exploiting the Monastery: (7)
As of 2024-04-19 12:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found