masaccio / numbers-parser

Python module for parsing Apple Numbers .numbers files
MIT License
201 stars 14 forks source link

Preserve style when writing new value to cells #60

Closed burakgok closed 1 year ago

burakgok commented 1 year ago

Current behavior of Table.write is to overwrite cell content and style altogether. Is there a way to type something in a cell without overwriting its style?

Currently, styles of empty cells are lost when saving the document. Can we preserve their styles as well?

In Numbers, when a new row/column is added, depending on the content menu option, the styles of the neighboring row/column are copied. Can we achieve the same effect when an out-of-range cell is referenced in Table.write?

masaccio commented 1 year ago

Sounds like a duplicate of #56. v4.0.0 has some fixes for this and I'll shortly be releasing some updates to create new styles and apply them to individual cells.

burakgok commented 1 year ago

I have a table, which is left-aligned, has percentage format and potentially has empty cells. When I do table.write(i, j, "35%"), left-alignment and percentage format are lost. The resultant cell has no specific alignment, has automatic format and has text value of "35%". When I type 35% in that cell in Numbers, it is interpreted as 0.35 and displayed as a percentage. I am trying to achieve the same thing using numbers-parser. To replicate Numbers' behavior:

  1. Cell styles should be preserved in write() or at least there should be a new method or flag.
  2. Styles of empty cells shouldn't be discarded on save.
  3. New rows/columns should copy styles of neighboring cells.
masaccio commented 1 year ago

Version v4.1.0 now has support for paragraph styles and cell styles. Numbers stores some styling (background colour and vertical alignment) per-cell rather than in a paragraph style, but number-parser should keep track of this.

For usage, see new docs for style editing. It's all very new so could still have plenty of bugs. There are some tests for all the new code, but I've not exhaustively tested it yet.

burakgok commented 1 year ago

numbers-parser: 4.1.1 Numbers: 13.1 (7037.0.101) Files.zip

  1. When I open Test.numbers and save it as Test2.numbers without doing any modification; A1, A2 and B1 become italic, and the text alignment of B2 is lost.
  2. When I do the following in Test.numbers and save it as Test3.numbers; the background color of A2 is preserved, but its text inset is reset to 0 (from default 4pt).
    bg_color = table.cell("A2").style.bg_color
    table.write("A2", "Item 2")
    table.cell("A2").style.bg_color = bg_color
  3. I also tried to preserve the percentage format of B2 on change, but no matter what I write to it ("35%" or 0.35), the data format is defaulted to "Automatic" and I couldn't find a way to change it.
masaccio commented 1 year ago

I can see most of that visually too, though the percentage format when I open in Numbers 13.1 is still a percentage data format. The alignment is changed to Automatic though.

Curiously automated tests on the data show its changed so there's clearly something else that needs to be updated in the saved file to preserve the formatting. The saved files show the same errors as yours.

masaccio commented 1 year ago

I believe this is fixed in 4.1.2. The text inset is not preserved as that's not supported yet, so the new bg_color style cannot inherit that. Copying styles between cells won't work in the way I think you're hoping; only the supported fields can be copied.

masaccio commented 1 year ago

Looking at the defaults of paragraph styles, it looks like an inset of 4pt is the default so I could either set that on new styles or add support. It looks like text padding (what the UI calls a text inset) and the 3 indents configurable in the UI are easily changeable.

masaccio commented 1 year ago

Implemented in 4.3