Posted on 2008-10-02 15:49:07-07 by benat
Autofilter in Calc
Hi all, I use OpenOffice::OODoc to generate Calc spreadsheets with data extracted from a database, and I would know whether it's possible to set (I mean programmatically) an "autofilter" on a given range of cells. I have tried setting the "autofilter" on an empty .ODS template, then filling it with OODoc, but the filter gets removed from the final document. I have been googling a while around this but I couldn't find any useful hint. Sorry indeed if I missed anything obvious. Thanks a lot. Benat.
Direct Responses: 10480 | Write a response
Posted on 2009-04-13 10:36:02-07 by smc in response to 8921
Re: Autofilter in Calc (solution)

It appears to me that you need to add the raw XML code to accomplish this now. Fortunately this isn't too hard once you do the necessary reverse engineering. :-)

my $xml = '<table:database-ranges>' . '<table:database-range table:target-range-address="' . $sheetname . '.A1:' . $sheetname . '.U' . $ +row_cnt . '" table:display-filter-buttons="true"/>' . '</table:database-ranges>'; my $xmlpos = $doc->getElement('//table:table', 0); $doc->insertElement ( $xmlpos, $xml, position => 'after' );

In the above example:

$xml is the raw xml we need to insert into our spreadsheet.

$sheetname is the name of the worksheet (a scalar, not a ref to the sheet). Note that some characters in the sheetname, such as a period, will cause this not to work.

A1 is the starting coordinate for the autofilter. U is the ending column, $row_cnt is the number of rows in the auto filter range (you could also parametrize the U).

I also tried using appendElement with similar syntax, but it was nesting the new xml within </table:table> instead of after it. A bug?

Direct Responses: Write a response
Perl Weekly newsletter
A free weekly newsletter for people who are busy to read all the blogs. click here to check it out.