Iterating over table cells in order.

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
_savage
Posts: 198
Joined: Sun Apr 21, 2013 12:55 am

Iterating over table cells in order.

Post by _savage »

I’d like to visit all cells of a table in order, left-to-right (row) and top-to-bottom would be fine. I’ve read this wiki page but would like some clarification.

Suppose I iterate over the rows (interface XTableRows, service TextTableRow):

Code: Select all

for row in table.getRows():                                             
    print("row " + str(row) + " cells " + str(row.getCount()))
Actually gives me an error: AttributeError: getCount. Inspecting the same document with MRI shows that there is indeed a Count attribute and a getCount() method for row, yet the AttributeError exception. Either way, having a row still would not give me access to the cells of that row, would it?

Next I tried to iterate over all cells by their name (method getCellNames() of the XTextTable interface):

Code: Select all

for n in table.getCellNames():                                        
    cell = table.getCellByName(n)
which works and seems to iterate in-order over all rows top down. However, can I assume that the sequence of cell names is always in that same order?

Furthermore, looking and the cell names parameter to getCellByName() it seems that table cell names follow a naming convention: columns are enumerated with capital letters A ... Z, and rows with numbers. How does that scheme extend to larger tables, will columns then be named AA, AB, AC, ... , AZ, BA, BB, BC, ... and so forth?
Mac 13.7 using LO 24.8.2.1, Ubuntu Linux using LO 24.8 headless.
F3K Total
Volunteer
Posts: 1044
Joined: Fri Dec 16, 2011 8:20 pm

Re: Iterating over table cells in order.

Post by F3K Total »

If the tables name is Table1, this should help.

Code: Select all

Sub iterate_TextTableCells
    oTexttables = ThisComponent.Texttables
    oTexttable = oTexttables.getByName("Table1")
    nRows = oTexttable.Rows.count - 1
    nColumns = oTexttable.Columns.count - 1
    for i = 0 to nRows
        for k = 0 to nColumns
            oCell = oTexttable.getCellbyPosition(k,i)
            Print "Cell: " & ocell.CellName & " Value: " & ocell.Value 'for numbers, .string for text
        next k
    next i
End Sub
  • MMove 1.0.6
  • Extension for easy, exact positioning of shapes, pictures, controls, frames ...
  • my current system
  • Windows 11 AOO, LO | Linux Mint AOO, LO
_savage
Posts: 198
Joined: Sun Apr 21, 2013 12:55 am

Re: Iterating over table cells in order.

Post by _savage »

Thank you, that seems to work for simple tables. However, how does this approach iterate over split or spanning cells? For example, for this table:

Code: Select all

+----+----+----+-----+
|    |    |    |     |  (4 columns)
+----+----+----+-----+
|    |               |  (2 columns)
+----+---+---+---+---+
|    |   |   |   |   |  (5 columns)
+----+---+---+---+---+
the above code snippet gives me 3 rows (ok) and 4 columns (wrong), which causes an OutOfBounds in row 2 when I read column 3 or 4, and misses column 5 in row 3.
Mac 13.7 using LO 24.8.2.1, Ubuntu Linux using LO 24.8 headless.
F3K Total
Volunteer
Posts: 1044
Joined: Fri Dec 16, 2011 8:20 pm

Re: Iterating over table cells in order.

Post by F3K Total »

You're right. Doesn't work for split or spanning cells, use your 2nd approach.
Texttables seem to use the alphabetical order, but also lower case for the columns:
A,B...Z
a,b...z
AA,AB ...AZ
Aa,Ab ...Az...
R
  • MMove 1.0.6
  • Extension for easy, exact positioning of shapes, pictures, controls, frames ...
  • my current system
  • Windows 11 AOO, LO | Linux Mint AOO, LO
_savage
Posts: 198
Joined: Sun Apr 21, 2013 12:55 am

Re: Iterating over table cells in order.

Post by _savage »

F3K Total wrote:You're right. Doesn't work for split or spanning cells, use your 2nd approach.
So my worry is that getCellNames() returns an unordered sequence of cell names, which would lead to a more or less random visitation of the table’s cell. I can’t find documented that this functions returns an ordered sequence of cell names...
Mac 13.7 using LO 24.8.2.1, Ubuntu Linux using LO 24.8 headless.
F3K Total
Volunteer
Posts: 1044
Joined: Fri Dec 16, 2011 8:20 pm

Re: Iterating over table cells in order.

Post by F3K Total »

Try it
Attachments
iterate.odt
(14.42 KiB) Downloaded 211 times
  • MMove 1.0.6
  • Extension for easy, exact positioning of shapes, pictures, controls, frames ...
  • my current system
  • Windows 11 AOO, LO | Linux Mint AOO, LO
F3K Total
Volunteer
Posts: 1044
Joined: Fri Dec 16, 2011 8:20 pm

Re: Iterating over table cells in order.

Post by F3K Total »

Here i colored them, you can see that everything works fine first from left to right then from top to bottom.
Attachments
iterate.odt
(14.68 KiB) Downloaded 261 times
  • MMove 1.0.6
  • Extension for easy, exact positioning of shapes, pictures, controls, frames ...
  • my current system
  • Windows 11 AOO, LO | Linux Mint AOO, LO
_savage
Posts: 198
Joined: Sun Apr 21, 2013 12:55 am

Re: Iterating over table cells in order.

Post by _savage »

The thing that irritates me, and looking at this from a HTML colspan, rowspan perspective, is that I find it difficult to find out whether a cell spans others or if two others are a split cell.

For example, in the above table all cells are equal simple cells, yet one can argue that they split and span: B2 spans A2, A3, A4; or C2, C3, C4, C5 split B2 but none of the A2/3/4 spans any of the C2/3/4/5 cells.

Or another example:

Code: Select all

+---------+
|         |  <- Does this span the bottom two cells, or
+----+----+
|    |    |  <- is this a split cell?
+----+----+
Finding out if a cell is indeed a split cell can be done by its name. But then I still don’t really know which cells in relation it splits... Perhaps Office table setup isn't as complex and flexible as HTML?
Mac 13.7 using LO 24.8.2.1, Ubuntu Linux using LO 24.8 headless.
F3K Total
Volunteer
Posts: 1044
Joined: Fri Dec 16, 2011 8:20 pm

Re: Iterating over table cells in order.

Post by F3K Total »

Yes, you're right, i can't help further, do also find no information but only the TableColumnSeparators of the rows, maybe the position helps you.
  • MMove 1.0.6
  • Extension for easy, exact positioning of shapes, pictures, controls, frames ...
  • my current system
  • Windows 11 AOO, LO | Linux Mint AOO, LO
Post Reply