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

Missing Type in com.github.jferard.fastods.datastyle.DataStyles #91

Closed Max4409 closed 6 years ago

Max4409 commented 6 years ago

Hi,

here is my code : final OdsFactory odsFactory = OdsFactory.create(); final AnonymousOdsFileWriter writer = odsFactory.createWriter(); final OdsDocument document = writer.document(); document.addChildCellStyle(TableCell.Type.STRING); => generate NullPointerException

The problem is Type.STRING is unknowned in com.github.jferard.fastods.datastyle.DataStyles :

EnumMap<TableCell.Type, DataStyle> dataStyleByType

jferard commented 6 years ago

Hi. Nice catch ! I'll fix it. To make it clean, I will find a way to ensure that DataStyles knows every TableCell.Type.

jferard commented 6 years ago

After some digging, here's a first result. There are some underlying problems in this issue.

  1. the types STRING or VOID do not currently need a specific data style. They are the default data style. (There's a tag 16.27.25<number:text-style> in the specs, but I don't use it now.) They are not meant to be used with document.addChildCellStyle.
  2. document.addChildCellStyle(TableCell.Type.STRING) should obviously not generate a NPE
  3. Incidentally (that's not an issue in your snippet), You add styles manually if and only if you don't set a file name in createWriter, because you want to flush data before the document is fully created (e.g. for a very big file). Thus, writer.document(), if writer is a OdsFileWriter AnonymousOdsFileWriter (and not an AnonymousOdsFileWriter OdsFileWriter) should return a document to which you cannot add data styles (because they are added automatically).

In your specific case, you have to remove document.addChildCellStyle(TableCell.Type.STRING). Depending on the size of your file (and perhaps performance issues), you may set the name of the file in odsFactory.createWriter and forget about data styles. DataStyles will be automatically created by TableCell.setXXXValue and other styles may be added on the fly (e.g. Table.setStyle).

EDIT: I made a confusion between anonymous writers (datastyles are automatically created and styles are added on the fly) and named writers.

jferard commented 6 years ago

Fixed. You may not anymore add manually styles to a document returned by an anonymous writer. The styles (including data styles) will be added on the fly. See for instance Table.setStyle, TableRow.setStyle, TableCell.setStyle, ... The data styles will be created quietly.

Max4409 commented 6 years ago

Thanks for your explanations.

Actually, i need to create a lot of files containing almost 35.000 lines each. Your FastOds is magic for me ! I used SimpleODF before, but it was too slow !!

Are you french ? Because I'm and I would like to explain to you what I have to do but my english is really poor !

jferard commented 6 years ago

I'm french (my english is also poor). Please open an issue with a [French] tag before the title, and I'll be happy to help you if I can.