masaccio / numbers-parser

Python module for parsing Apple Numbers .numbers files
MIT License
208 stars 15 forks source link

table.height does not update when rows are added #85

Closed GerdHoeren closed 5 months ago

GerdHoeren commented 5 months ago

Describe the bug I'm creating two tables on a sheet. A variable number of rows gets added to the first table. I'd like the table.height property to reflect the updated height after adding the rows, so that I can use it to correctly position the second table below it.

To Reproduce def WriteTableData(dict, table):

Append rows to the end of this table

#
tableRow = 0    # Simplified example - no table header
for i in sorted(dict.keys()):
    table.write(tableRow, 0, i)  # Symbol
    [ ... ]
    tableRow += 1

return tableRow

def bug(): table0 = [ ... ] # First table in sheet

# Add rows to the top table
#
print("Before insert: table0.height", table0.height)
numRows = WriteTableData(dict, table0)
print("Rows added", numRows)
print("After insert: table0.height", table0.height)

Output is: Before insert: table0.height 36 Rows added 110 After insert: table0.height 36

Expected behavior table0.height should increase to account for the 110 rows that have been added.

Attachments If you can, upload the Numbers spreadsheet or a simple reproducer that triggers the bug.

Additional information python -m pip list [ ... ] numbers-parser 4.10.5

Add any other context about the problem here. I'm working around this issue by calculating the Y position of table 1 with:

Create table 1

#
# Unfortunately table0.height is the created table height -- not the height after adding data rows.
# The +2 on the rowHeight are for the top and bottom row borders
#
rowHeight = table0.row_height(1)     # Number of pixels in the first data row
y = table0.height + (rowHeight+2) * numRows + 100.0

But, this doesn't work if added rows contain text that wraps and expands the row height.

masaccio commented 5 months ago

Looks like the height is incorrect for new documents as well (it's 20 which is the height of one row).

masaccio commented 5 months ago

Calculation fixed (and for width too). v4.11.2 is latest.

GerdHoeren commented 5 months ago

Thank you for the quick update. The table.height field now updates, but it appears that it does not take into account the cell borders. I would expect the following code to create table1 100 pixels below table0:

y = table0.height + 100.0
table1 = spreadsheet.sheets[tab].add_table("Virtual " + tab, 0, y, 2, 4)

Notice in this image table1 overlaps the bottom of table0: OverlappingTables

If I change the code to: y = table0.height *+ numRows2** + 100.0 table1 = spreadsheet.sheets[tab].add_table("Virtual " + tab, 0, y, 2, 4)

Table1 is created where I would expect: CorrectTableSpacing

masaccio commented 5 months ago

Row heights and columns widths now include borders. I'm matching the sizes reported in Numbers in my testing so 0.35pt is the default which appears not to affect overall table height/width.

GerdHoeren commented 5 months ago

Thank you for fixing this issue. "table.height" now works as expected.