[Solved] Find predecessor paragraph for TextTable/Cell

Keyboard macros or custom scripts

[Solved] Find predecessor paragraph for TextTable/Cell

Postby brauart » Mon Aug 03, 2020 4:41 pm

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
Last edited by Hagar Delest on Tue Aug 04, 2020 11:42 am, edited 1 time in total.
Reason: tagged solved
OpenOffice 4.1.7 on Windows 10
brauart
 
Posts: 14
Joined: Thu Jul 23, 2020 4:31 pm

Re: Find predecessor paragraph for TextTable/Cell

Postby JeJe » Mon Aug 03, 2020 4:58 pm

table.anchor

For a textcursor in the text where the table is anchored

p = table.anchor
tc = p.text.createtextcursorbyrange(p)
Openoffice 4.1.6
Windows 8
JeJe
Volunteer
 
Posts: 1690
Joined: Wed Mar 09, 2016 2:40 pm

Re: Find predecessor paragraph for TextTable/Cell

Postby brauart » Tue Aug 04, 2020 9:26 am

@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
OpenOffice 4.1.7 on Windows 10
brauart
 
Posts: 14
Joined: Thu Jul 23, 2020 4:31 pm

Re: Find predecessor paragraph for TextTable/Cell

Postby JeJe » Tue Aug 04, 2020 10:24 am

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
Openoffice 4.1.6
Windows 8
JeJe
Volunteer
 
Posts: 1690
Joined: Wed Mar 09, 2016 2:40 pm

Re: Find predecessor paragraph for TextTable/Cell

Postby brauart » Tue Aug 04, 2020 11:11 am

@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.
OpenOffice 4.1.7 on Windows 10
brauart
 
Posts: 14
Joined: Thu Jul 23, 2020 4:31 pm

Re: Find predecessor paragraph for TextTable/Cell

Postby JeJe » Tue Aug 04, 2020 11:20 am

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
Openoffice 4.1.6
Windows 8
JeJe
Volunteer
 
Posts: 1690
Joined: Wed Mar 09, 2016 2:40 pm

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

Postby Lupp » Tue Aug 04, 2020 12:36 pm

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
On Windows 10: LibreOffice 7.2 and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
User avatar
Lupp
Volunteer
 
Posts: 3164
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

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

Postby JeJe » Tue Aug 04, 2020 1:30 pm

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)
Openoffice 4.1.6
Windows 8
JeJe
Volunteer
 
Posts: 1690
Joined: Wed Mar 09, 2016 2:40 pm

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

Postby brauart » Tue Aug 04, 2020 2:48 pm

@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)
OpenOffice 4.1.7 on Windows 10
brauart
 
Posts: 14
Joined: Thu Jul 23, 2020 4:31 pm


Return to OpenOffice Basic, Python, BeanShell, JavaScript

Who is online

Users browsing this forum: No registered users and 2 guests