Writer Basic - retrieve TextTable from TextTableCursor (originally "4.1")

I would like to write a macro that works on the selected table. When I select a table, the object that ThisComponent.CurrentSelection returns is of type SwXTextTableCursor (will refer to it generically as TextTableCursor). According to DBG_methods it provides methods to traverse through the selected cells and merge or split the cells, but it doesn’t seem to provide a way to access the actual table itself. Conversely, ThisComponent.TextTables returns the tables, but as far as I can tell, no way to determine if any or all of a table is selected.

Is there any way to retrieve the TextTable(s) from TextTableCursor?

Thanks for reading.

Here are the equivalent objects.

This object is retrieved without reference to the cursor:

SwXTextTable = ThisComponent.TextTables.getByIndex(0)

This object is derived from the cursor:

cntrllr = ThisComponent.CurrentController
cursor = cntrllr.ViewCursor
SwXTextTable = cursor.TextTable

You can test this with the following operation, which will print out the contents of the top left cell of whichever text table you have invoked:

oCell = SwXTextTable.getCellByPosition(0,0)
MsgBox oCell.String

Hi

I do not know if this is helpful but could complete doug’s response on “determine if a table is selected”:

dim oCursor as object

oCursor = thiscomponent.currentcontroller.viewcursor

if isempty(oCursor.TextTable) then	
	msgbox "Current selection: no table"
else
	msgbox "Current selection:  " & oCursor.TextTable.name
end if

Regards

(Question reopened.)

TextTableCursor objects are poorly equipped, and to work with TextTable objects based on the API often seems to be a game of chance.
I had the given question in mind more than once, and nowhere found a ready-made solution. Recently I again stumbled into that donkeywork, and actually I managed to learn a bit more. The progress allowed me to also find a solution for this question.
Accepting a “dirty but not at all quick” approach, I can now retrieve the TextTable from a TextTableCursor! Wow!
Funny code:

Function getTextTableFromSelection(pDoc As Object, Optional pSel As Object)
Dim tT As Object
getTextTableFromSelection = tT
On Local Error Goto fail
cCtrl = pDoc.CurrentController
oldSel = pDoc.CurrentSelection
If IsMissing(pSel) Then pSel = oldSel
If pSel.supportsService("com.sun.star.text.TextTableCursor") Then
  wasCur = True
  splRgN = Split(pSel.RangeName, ":")
  wasSingle = (Ubound(splRgN)=0)
  tlCellN = splRgN(0)
  pSel.gotoCellByName(tlcellN, False)
  cCtrl.select(pSel)
  tRg = pDoc.CurrentSelection(0)
Else
  tRg = pSel(0)
End If
textTable = tRg.TextTable
If NOT IsObject(textTable) Then textTable = tT
getTextTableFromSelection = textTable
fail:
cCtrl.select(oldSel)
If wasCur AND wasSingle Then
  fr = cCtrl.Frame
  dh = CreateUnoService("com.sun.star.frame.DispatchHelper")
  dh.executeDispatch(fr, ".uno:EntireCell", "", 0, Array())
End If
End Function

Sub tryIt()
doc = ThisComponent
sel = doc.CurrentSelection
h = getTextTableFromSelection(doc, sel)
End Sub

With a lot of dirty tricks I might also be able to get the document from the TextTableCursor (or any selection in the doc), but I didn’t do that. To know the document is necessary to be able to use the CurrentController, which is needed for …
See yourself!

1 Like