How to detect row-spanning table cells

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

How to detect row-spanning table cells

Post by _savage »

Hello, Suppose I have the following two tables:
tables.jpg
and I’d like to iterate over their cells and determine the row & column span for each cell. It looks like I can create very irregular tables that don’t map well to HTML tables but let’s not worry about that for now.

So, I can iterate over every row in a table, and then using TableColumSeparators I can determine how cells in a particular row are distributed (more details in this Stackoverflow answer, or related discussion in this forum thread).

Code: Select all

rows = table.getRows()
for row in rows:
    seps = row.TableColumnSeparators
    # len(seps) is the number of cells in this row, 
    # and a separator’s `Position` tells me where.
However, I just can’t figure out how to determine whether a cell spans more than one rows, like cell A1 in Table 2 above. I’ve been poking around with MRI for quite some time, but nothing...

Any tips and suggestions would be greatly appreciated ;)
Mac 10.14 using LO 7.2.0.2, Gentoo Linux using LO 7.2.3.2 headless.
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: How to detect row-spanning table cells

Post by Villeroy »

A text table has method getCellNames() returning an array of strings (A1, A2, A3 etc) where the names of the submerged cells are missing. Write your own function which tests if there is a B1, C1, D1 for the given A1.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: How to detect row-spanning table cells

Post by JeJe »

Create a 2-d boolean array the size of the row and column counts and fill it using the getcellnames function perhaps...

Edit:
Or perhaps a 2d array of strings which you set to the span covered by looping through getcellnames.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
_savage
Posts: 187
Joined: Sun Apr 21, 2013 12:55 am

Re: How to detect row-spanning table cells

Post by _savage »

Villeroy wrote:A text table has method getCellNames() returning an array of strings (A1, A2, A3 etc) […]
Right, I came across that function but I seem to remember that cell names can be modified? But maybe that’s a symbolic cell name for Calc (thread) independent of the constant (?) “coordinate” cell names we’re talking about here?
Mac 10.14 using LO 7.2.0.2, Gentoo Linux using LO 7.2.3.2 headless.
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: How to detect row-spanning table cells

Post by Villeroy »

As far as I can see (using my MRI crystal ball), text table cells can't be renamed.
[Tutorial] Introduction into object inspection with MRI
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: How to detect row-spanning table cells

Post by JeJe »

The Calc naming is naming a range, which might be one cell, but its not changing a cell name/address.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
_savage
Posts: 187
Joined: Sun Apr 21, 2013 12:55 am

Re: How to detect row-spanning table cells

Post by _savage »

Villeroy wrote:A text table has method getCellNames() returning an array of strings (A1, A2, A3 etc) where the names of the submerged cells are missing.
I think the cell names alone are not enough. For example, these two tables
tables.jpg
tables.jpg (8.88 KiB) Viewed 5965 times
both have the following cell names: ('A1', 'B1', 'C1', 'B2'). It’s not possible to know whether B2 spans C2 horizontally or whether C1 spans C2 vertically. To resolve that ambiguity, I think TableColumnSeparators needs to be incorporated. For the first table, row two has one separator at Position=5000; for the second table, row two has two separators at Position=5000 and Position=7500.
Mac 10.14 using LO 7.2.0.2, Gentoo Linux using LO 7.2.3.2 headless.
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: How to detect row-spanning table cells

Post by Villeroy »

Indeed. Can not work this way.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: How to detect row-spanning table cells

Post by JeJe »

The information appears to be available from the accessible context of the table cell (you'll need to find that from searching through the accessible children of the componentwindow). The table will have to be on screen to use that though.

http://www.openoffice.org/api/docs/comm ... owExtentAt
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
_savage
Posts: 187
Joined: Sun Apr 21, 2013 12:55 am

Re: How to detect row-spanning table cells

Post by _savage »

JeJe wrote:(you'll need to find that from searching through the accessible children of the componentwindow)
I’ve just spent the last hour trying to get access to some form of accessible context but no dice. I open the Desktop and the TextDocument… and then? Where’s the hook to the Accessibility Tree of a Writer document I opened on the Desktop?
Mac 10.14 using LO 7.2.0.2, Gentoo Linux using LO 7.2.3.2 headless.
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: How to detect row-spanning table cells

Post by Villeroy »

Code: Select all

Sub inspectAccessible()
mri = createUnoService("mytools.Mri")
f = ThisComponent.CurrentController.Frame 
comp = f.ComponentWindow
cont = f.ContainerWindow
mri.inspect(comp.AccessibleContext)
mri.inspect(cont.AccessibleContext)
End Sub
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: How to detect row-spanning table cells

Post by JeJe »

I use MRI for this. Start with this and you'll see the accessiblecontext of the component window and go through the children and children of the children etc to find what you want.

Code: Select all

mri thiscomponent.currentcontroller.componentwindow

The accessible role contants for table and table cell are here:

https://www.openoffice.org/api/docs/com ... eRole.html

You need to find the main text window and then the children will include the table.

It will be tedious exploring with MRI - but if you create a simple document with one text window and close all the toolbars/ruler/status bar/everything you can close apart from the main window it'll be a little easier.

You might want to write a function to go through all the children and look for the accessible role and call mri when its found.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: How to detect row-spanning table cells

Post by JeJe »

try running this - make sure your table is on screen.

Code: Select all

sub findtable
	ww= ThisComponent.CurrentController.Frame.componentwindow 
	arole = 58
	hasAssessibleRole (ww,arole,found,description)
	if found then msgbox description
end sub

sub hasAssessiblerole(byval w ,byval arole,byref found,byref description)
	dim ac,ccount as long, i as long
	on error resume next
	ac = w.accessiblecontext
	if ac.accessiblerole = arole then
		description= ac.accessibledescription
		found = true
		mri w
	else
		ccount =ac.getaccessiblechildcount
		if ccount >0 then
			for i = 0 to ccount - 1
				hasAssessiblerole( ac.getaccessiblechild(i),arole,found,description)
				if found = true then exit sub
			next
		end if
	end if
End sub


Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
_savage
Posts: 187
Joined: Sun Apr 21, 2013 12:55 am

Re: How to detect row-spanning table cells

Post by _savage »

JeJe wrote:try running this - make sure your table is on screen.
Thank you, JeJe and Villeroy for the code suggestions. I ended up with this Python snippet:

Code: Select all

def print_accessible_context(ac):
    nonlocal indent
    print(indent, "name", ac.AccessibleName, "role", ac.AccessibleRole, "children", ac.AccessibleChildCount)
    if ac.AccessibleRole == 58:
        print(indent, " rows", ac.AccessibleRowCount, "cols", ac.AccessibleColumnCount)
        for row in range(ac.AccessibleRowCount):
            for col in range(ac.AccessibleColumnCount):
                print(indent, " cell at", row, col, "rowspan", ac.getAccessibleRowExtentAt(row, col), "colspan", ac.getAccessibleColumnExtentAt(row, col))
    indent += " "
    for i in range(ac.AccessibleChildCount):
        print_accessible_context(ac.getAccessibleChild(i).AccessibleContext)
    indent = indent[1:]

indent = ""
# controller is a com.sun.star.frame.XController 
print_accessible_context(controller.Frame.ComponentWindow.AccessibleContext)
Which returns for the first table in the initial post

Code: Select all

    name Table1-1 role 58 children 4
     rows 2 cols 3
     cell at 0 0 rowspan 1 colspan 3
     cell at 0 1 rowspan 1 colspan 2
     cell at 0 2 rowspan 1 colspan 1
     cell at 1 0 rowspan 1 colspan 1
     cell at 1 1 rowspan 1 colspan 1
     cell at 1 2 rowspan 1 colspan 1
     role 59 children 1
      role 41 children 0
     role 59 children 1
      role 41 children 0
     role 59 children 1
      role 41 children 0
     role 59 children 1
      role 41 children 0
Merged cells are listed, too, which is alright, I guess. However, I run office in headless and invisible mode and perhaps that’s why not all tables in my document appear in the accessible context dump. The other problem I haven’t figured out yet is how to connect a table and its accessible context (the AccessibleName (Table1-1) seems to be a variation of the actual object’s name (Table1)).

Perhaps it’s time to take a look at the HTML export filter for some help…
Mac 10.14 using LO 7.2.0.2, Gentoo Linux using LO 7.2.3.2 headless.
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: How to detect row-spanning table cells

Post by JeJe »

The accessible context relates to a window. For the document's main text window the children will be the paragraphs or cells that are visible on screen - one's before or after aren't available. You'll have to select/goto each table in turn so its available via the accessible context.

Table1-1 is the table name, page 1, MRI will help you (from memory of the other day anyway).
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: How to detect row-spanning table cells

Post by Villeroy »

AccessibleContext is made for the visually impaired, for things like screen reader software.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Post Reply