jferard / fastods

A very fast and lightweight (no dependency) library for creating ODS (Open Document Spreadsheet, mainly for Calc) files in Java. It's a Martin Schulz's SimpleODS fork
GNU General Public License v3.0
36 stars 6 forks source link

Varying column widths #243

Closed ideas-into-software closed 1 year ago

ideas-into-software commented 1 year ago

Attempt to set different column widths, produces document where all columns have the same width. This problem occurs regardless if column style is set via walker (com.github.jferard.fastods.TableCellWalker.setColumnStyle(TableColumnStyle)) or table (com.github.jferard.fastods.Table.setColumnStyle(int, TableColumnStyle)).

Example of how adjusted column width is constructed and set, in addition to resulting document (‘testExport.column-width-problem.ods’):

    private void adjustColumnWidth(TableCellWalker walker, int colIndex, int charsCount) {
        TableColumnStyle adjustedWithColumnStyle = constructAdjustedWithColumnStyle(charsCount);

        walker.getTable().setColumnStyle(colIndex, adjustedWithColumnStyle);
    }

    private TableColumnStyle constructAdjustedWithColumnStyle(int charsCount) {
        // @formatter:off
        return TableColumnStyle.builder("adjusted-with-column-style")
                .columnWidth(calculateColumnWidth(charsCount))
                .build();
        // @formatter:on
    }

    private SimpleLength calculateColumnWidth(int charsCount) {
        SimpleLength columnWidth = SimpleLength.mm(charsCount * 10);

        return columnWidth;
    }

I.e.. depending on number of characters in a given column, multiplied by some constant, e.g. 10.

I would expect same result as that obtained by manually setting column width, i.e. where I can set each column to different width, usually dependent on length of content in cells of that column.

Not sure if this is somehow related to ‘optimal width’ bug (https://github.com/jferard/fastods/issues/96) ?

Including @stbischof as per his request.

testExport.column-width-problem.ods

jferard commented 1 year ago

Hello. All the styles have the same name ("adjusted-with-column-style"), hence there is only one style in the final document (see content.xml):

    <table:table-column table:style-name="adjusted-with-column-style" table:number-columns-repeated="4" table:default-cell-style-name="Default" />

You could try to add a suffix, like String.format("adjusted-with-column-style-%s-%s", tableIndex, colIndex) and test if the result meets your expectations.

As you noticed (#96) there is a style:use-optimal-column-width attribute (accessible via TableColumnStyleBuilder.optimalWidth), but LibreOffice seems to ignore it. See Open Document Format for Office Applications (OpenDocument) Version 1.3. - Part 3: OpenDocument Schema - 20.393.

ideas-into-software commented 1 year ago

@jferard

Sorry for the belated reply.

At the time I reported this, I barely new ODS spec / what is happening underneath, and only worked through the API exposed by your library. So I apologize for misreporting this as an issue.

Unfortunately, due to prohibitive license (https://github.com/jferard/fastods/issues/246) we were forced to switch to a different library.

I see the other two issues (https://github.com/jferard/fastods/issues/244, https://github.com/jferard/fastods/issues/143) I filed were already fixed. Thank you and regards.