tdf / odftoolkit

Java ODF toolkit project
https://odftoolkit.org/
Apache License 2.0
115 stars 48 forks source link

Setting stringValue or displayText in table cell resets horizontal alignment to left #239

Open MarekChr opened 1 year ago

MarekChr commented 1 year ago

I have .odt file with table called statsTable.

Cells at row 1 have horizontal alignment set to RIGHT. Cells at row 2 have horizontal alignment set to CENTER.

When I set cell values programmatically by .setStringValue or .setDisplayText, horizontal alignment is reset to LEFT and need to set alignment again.

try (final InputStream input = Main.class.getResourceAsStream("stats.odt")) {
            final var doc = OdfDocument.loadDocument(input);
            final var odfTable = doc.getTableByName("statsTable");
            odfTable.getRowByIndex(1).getCellByIndex(1).setStringValue("test");
            odfTable.getRowByIndex(1).getCellByIndex(2).setDisplayText("hello world!");
            odfTable.getRowByIndex(2).getCellByIndex(1).setDisplayText("next test");
            odfTable.getRowByIndex(2).getCellByIndex(2).setDisplayText("very next test");
            // need to set alignment of each cell, because it is reset to LEFT
            odfTable.getRowByIndex(2).getCellByIndex(2).setHorizontalAlignment("center");
            ...
            doc.save(new File("manipulated.odt"));
        }

Am I missing something? Is this expected behavior? I would expect horizontal alignment to be unchanged while setting the cell value.

Using version 0.11.0 and .odt file with table was created with LibreOffice Writer 7.4.

svanteschubert commented 1 year ago

It does not sound like any desired behavior. Just tested a bit adding new content in LibreOffice Writer and Calc.

Nevertheless, if something is middle aligned it should stay this way. Most of the doc API was a gift from IBM Beijing, which was later forked to Simple API, which already abondoned due to heavy copy/pasted duplicated code to ODFDOM.

I would be happy if you could provide a pull-request. It usally helps to start with a test that fails and the patch makes the regression test run.

Thanks in advanced, Marek,

Greetings from Berlin, Svante