odftoolkit icon indicating copy to clipboard operation
odftoolkit copied to clipboard

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

Open MarekChr opened this issue 2 years ago • 2 comments

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.

MarekChr avatar Jul 10 '23 10:07 MarekChr

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

  • In Writer: all new cell content is added left - likely in writing direction (as European I write from left ro right, so it is left aligned).
  • In Calc: Numbers are aligned right (as the calculation direction is still from their arabian origin right to left) and Text is aligned left.

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

svanteschubert avatar Jul 13 '23 18:07 svanteschubert

@MarekChr Are you able to provide some further information, a failing regression test would be great for this use case or even a pull request with a solution?

svanteschubert avatar May 13 '25 20:05 svanteschubert