Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
MarekChr opened this issue Jul 10, 2023 · 1 comment

Comments

@MarekChr
Copy link

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
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants