Page 1 of 1

[Solved] Find predecessor paragraph for TextTable/Cell

PostPosted: Mon Aug 03, 2020 4:41 pm
by brauart
Hello,
I have a TextField within a TextTable/Cell. From there I would like to navigate (or get access) to the previous (in terms of textflow) text paragraph.
I tried several things.
From the cell I can get the TextTable through properties, but am stuck then.
Using cursors I did not succeed too.

Any help appreciated.

Thanks,
brauart

Re: Find predecessor paragraph for TextTable/Cell

PostPosted: Mon Aug 03, 2020 4:58 pm
by JeJe
table.anchor

For a textcursor in the text where the table is anchored

p = table.anchor
tc = p.text.createtextcursorbyrange(p)

Re: Find predecessor paragraph for TextTable/Cell

PostPosted: Tue Aug 04, 2020 9:26 am
by brauart
@JeJe
it does not work. The line p.text.createtextcursorbyrange(p) gives an error. When I replace it as shown below tc is void and the selection is still in the table.
To test is a document with a paragrapah and a table is needed, where in some table cell a User Field "MyField" is inserted.

Code: Select all   Expand viewCollapse view
Sub testSelection
   oMasters = ThisComponent.getTextFieldMasters()
   depField = oMasters.getByName("com.sun.star.text.fieldmaster.User.MyField").DependentTextFields(0)
   ThisComponent.CurrentController.select(depField.Anchor)
   
    myCursor = ThisComponent.CurrentController.getViewCursor().getText().createTextCursorByRange(depField.Anchor)
    table = myCursor.TextTable
   
    p = table.anchor
   'tc = p.text.createtextcursorbyrange(p)
   tc = ThisComponent.CurrentController.getViewCursor().getText().createTextCursorByRange(p)
   
   ' here I would like to select the previous paragraph
   ThisComponent.CurrentController.select(tc)
End Sub

Re: Find predecessor paragraph for TextTable/Cell

PostPosted: Tue Aug 04, 2020 10:24 am
by JeJe
Works for me on OO.

Code: Select all   Expand viewCollapse view

Sub testSelection
  oMasters = ThisComponent.getTextFieldMasters()
  depField = oMasters.getByName("com.sun.star.text.fieldmaster.User.MyField").DependentTextFields(0)
ThisComponent.CurrentController.select(depField.Anchor)
   
  myCursor = ThisComponent.CurrentController.getViewCursor().getText().createTextCursorByRange(depField.Anchor)
table = myCursor.TextTable

p = table.anchor

tc = p.text.createtextcursorbyrange(p)
tc.goleft(1,false)
tc.gotoStartOfParagraph(false)
tc.gotoEndOfParagraph(true)
   ThisComponent.CurrentController.select(tc)
End Sub

Re: Find predecessor paragraph for TextTable/Cell

PostPosted: Tue Aug 04, 2020 11:11 am
by brauart
@JeJe
you are right, it does work - in AOO. Thank you.
I must admit I tried this with LibreOffice, and there p.text is void.
I did not expect to find differences in the data model AOO vs LO.

Re: Find predecessor paragraph for TextTable/Cell

PostPosted: Tue Aug 04, 2020 11:20 am
by JeJe
Another method would be once you have the table name to enumerate the document's text paragraphs (if you know that's where its anchored) looking for supportsService("com.sun.star.text.TextTable")

From Useful Macro Information For OpenOffice By Andrew Pitonyak

Code: Select all   Expand viewCollapse view
Sub EnumerateParagraphs
REM Author: Andrew Pitonyak
Dim oParEnum 'Enumerator used to enumerate the paragraphs
Dim oPar 'The enumerated paragraph
REM Enumerate the paragraphs.
REM Tables are enumerated along with paragraphs
oParEnum = ThisComponent.getText().createEnumeration()
Do While oParEnum.hasMoreElements()
oPar = oParEnum.nextElement()
REM This avoids the tables. Add an else statement if you want to
REM process the tables.
If oPar.supportsService("com.sun.star.text.Paragraph") Then
MsgBox oPar.getString(), 0, "I found a paragraph"
ElseIf oPar.supportsService("com.sun.star.text.TextTable") Then
Print "I found a TextTable"
Else
Print "What did I find?"
End If
Loop
End Sub

Re: [Solved] Find predecessor paragraph for TextTable/Cell

PostPosted: Tue Aug 04, 2020 12:36 pm
by Lupp
A cell may not have a preceding TextParagraph. The TextTable will - in most cases, but not always.

Since you also work with LibreOffice the VERY raw code below might help. It did during my tests even for a nested TextTable.
TextTable immediately following another one (without a paragraph in-between), or at the very beginning of the text were not tested.
I also would not expect the code to work in AOO. (Didn't test.)
Code: Select all   Expand viewCollapse view
Function precedingParagraphToTheTextTableContainingTheSelctedTextField()
doc = ThisComponent
cCtrl = doc.CurrentController
sel = doc.CurrentSelection
tf = sel(0)
tTa = tf.TextTable
tTcur = tTa.createCursorByCellName("A1")
cCtrl.select(tTcur)
vc = cCtrl.ViewCursor
vc.goLeft(1, False)
h =  vc.TextParagraph
precedingParagraphToTheTextTableContainingTheSelctedTextField = h
cCtrl.select(sel)
End Function

Re: [Solved] Find predecessor paragraph for TextTable/Cell

PostPosted: Tue Aug 04, 2020 1:30 pm
by JeJe
If you're in the first cell and there is a previous text paragraph you could just use

Code: Select all   Expand viewCollapse view
thiscomponent.currentcontroller.viewcursor.goup(1,false)

Re: [Solved] Find predecessor paragraph for TextTable/Cell

PostPosted: Tue Aug 04, 2020 2:48 pm
by brauart
@JeJe and @Lupp
Thank you. Yes, switching to the viewCursor solved my problem.

Code: Select all   Expand viewCollapse view
   myCursor = myTable.getCellByPosition(0,0).createTextCursor()
   myCursor.gotoStartOfParagraph(false)
   ThisComponent.CurrentController.select(myCursor)
   
   viewCursor = ThisComponent.CurrentController.viewcursor
   viewCursor.goLeft(1,false)
   myCursor = viewCursor.getText().createTextCursorByRange(viewCursor)
   myCursor.gotoEndOfParagraph(false)
   myCursor.gotoStartOfParagraph(true)
   ThisComponent.CurrentController.select(myCursor)