masaccio / numbers-parser

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

Support for Adding and Deleting Rows #72

Closed AndrewClose closed 4 months ago

AndrewClose commented 6 months ago

This is great! Thanks for creating this library!

Are there any plans to support adding and deleting rows?

Thanks!

masaccio commented 6 months ago

I'm working on something else at the moment, but happy to add this.

If you wand to append a row then Table.add_row() already exists. It's missing from the docs (oops ... can tell I am not a Technical Author) but is there and tested.

For deleting a row then you can hack with:

from numbers_parser import Document
doc = Document()
doc.sheets[0].tables[0].write(0,0,"hello")
doc.sheets[0].tables[0].write(1,0,"beautiful")
doc.sheets[0].tables[0].write(2,0,"world")
doc.save("test1.numbers")
del doc.sheets[0].tables[0]._data[1]
doc.sheets[0].tables[0].write(2,0,"OK?")
doc.save("test2.numbers")

Which is a hack as you're using a 'private' member of Table. The code above works and the last write correctly saves to the new row offset 2. I believe this is as much as is needed in that tables are completely recomputed on save.

If you wanted to give it a try and report back any bugs, that would be helpful.

For the implementation I'll probably just add a new offset parameter to the existing Table.add_row() and default to -1 (the end of the table) to keep the API consistent.

AndrewClose commented 6 months ago

Excellent! Thanks for your quick response Jon! This should work. Cheers!

masaccio commented 4 months ago

Closing this as v4.8.0 now has functions to do this: Table.add_row() and Table.delete_row(). Both methods take a start_row and a number of rows. There are column methods to match them.