createEnumeration problem

Creating a macro - Writing a Script - Using the API

createEnumeration problem

Postby karpo518 » Mon Jun 19, 2017 9:07 am

Hi. I have a problem. For example, text document contains 5 paragraphs. I edit paragraphs in loop. I add 'new line' symbols chr(13) in some paragraphs. I got more than 5 paragraphs in result, but loop will have 5 iterations only and does not get all paragraphs. How i can solve this problem?
Libre Office 5.1.6.2, Linux Mint 18 (64 bit)
karpo518
 
Posts: 15
Joined: Wed May 24, 2017 4:17 pm

Re: createEnumeration problem

Postby robleyd » Mon Jun 19, 2017 9:16 am

It would help if you could show the code you are using that does not perform as you expect it to.
Cheers
David
OpenOffice 4.1.3 - Windows 7
OpenOffice 4.2.0 - Slackware 14.2 - 64 bit
User avatar
robleyd
Volunteer
 
Posts: 953
Joined: Mon Aug 19, 2013 3:47 am
Location: Murbko, Australia

Re: createEnumeration problem

Postby karpo518 » Mon Jun 19, 2017 9:58 am

robleyd, thanks for answer. My code[python] below:
Code: Select all   Expand viewCollapse view
    model = XSCRIPTCONTEXT.getDocument()

    tCursor = model.getText().createTextCursor()

    tCursor.gotoStart(False)
    tCursor.gotoEnd(True)
   
    eIterator = tCursor.createEnumeration()

    while eIterator.hasMoreElements():

        pCursor = eIterator.nextElement()
        if pCursor.supportsService("com.sun.star.text.Paragraph") and pCursor.String != '':
           pCursor.String = pCursor.String + " prepared" + chr(13) + "test paragraph"

    return None

This code edits text:

Paragraph 1
Paragraph 2
Paragraph 3
Paragraph 4
Paragraph 5

to:

Paragraph 1 prepared
test paragraph
Paragraph 2 prepared
test paragraph
Paragraph 3 prepared
test paragraph
Paragraph 4
Paragraph 5



Paragraphs 4 and 5 a not prepared.
Libre Office 5.1.6.2, Linux Mint 18 (64 bit)
karpo518
 
Posts: 15
Joined: Wed May 24, 2017 4:17 pm

Re: createEnumeration problem

Postby hubert lambert » Mon Jun 19, 2017 10:29 am

Hello,

Try something like this:

Code: Select all   Expand viewCollapse view
   model = XSCRIPTCONTEXT.getDocument()
   text = model.getText()
   eIterator = text.createEnumeration()
   while eIterator.hasMoreElements():
       pCursor = eIterator.nextElement()
       if pCursor.supportsService("com.sun.star.text.Paragraph") and pCursor.String != '':
          pCursor.String = pCursor.String + " prepared" + chr(13) + "test paragraph"

or more pythonic (LibO 5.x only):

Code: Select all   Expand viewCollapse view
   model = XSCRIPTCONTEXT.getDocument()
   text = model.getText()
   for elem in text:
       if elem.supportsService("com.sun.star.text.Paragraph") and elem.String:
          elem.String += " prepared\rtest paragraph"
Last edited by hubert lambert on Mon Jun 19, 2017 10:48 am, edited 1 time in total.
AOOo 4.1.2 on Win7 | LibreOffice on various Linux systems
hubert lambert
 
Posts: 57
Joined: Mon Jun 13, 2016 10:50 am

Re: createEnumeration problem

Postby karpo518 » Mon Jun 19, 2017 10:47 am

Hi, hubert lambert. Method getText() returns all objects of document. I process selected textRanges only.
Libre Office 5.1.6.2, Linux Mint 18 (64 bit)
karpo518
 
Posts: 15
Joined: Wed May 24, 2017 4:17 pm

Re: createEnumeration problem

Postby hubert lambert » Mon Jun 19, 2017 11:35 am

karpo518 wrote:I process selected textRanges only.

That was'nt obvious.Could you explain how you get then selection?
Last edited by hubert lambert on Mon Jun 19, 2017 11:50 am, edited 1 time in total.
AOOo 4.1.2 on Win7 | LibreOffice on various Linux systems
hubert lambert
 
Posts: 57
Joined: Mon Jun 13, 2016 10:50 am

Re: createEnumeration problem

Postby karpo518 » Mon Jun 19, 2017 11:47 am

I got code from andrew pitonyak's book. I use paragraphs loop in function FormatWorker()

Code: Select all   Expand viewCollapse view

...
model = XSCRIPTCONTEXT.getDocument()
oCurs = CreateSelectedTextIterator(model)

if oCurs is None :
    msgBox("No selected text")
    return None

for i in range(0, len(oCurs)):
    FormatWorker(oCurs[i][0], oCurs[i][1], model, ".*")
...


def CreateSelectedTextIterator(oDoc):
    if not IsAnythingSelected(oDoc):
        return None
    else:
        oSels = oDoc.getCurrentSelection()
        lSelCount = oSels.getCount()
        oCurs = []

        for lWhichSelection in range(0, lSelCount):
            oCurs.append([])
            oSel = oSels.getByIndex(lWhichSelection)
            oLCurs = GetLeftMostCursor(oSel)
            oRCurs = GetRightMostCursor(oSel)
            oCurs[lWhichSelection].append(oLCurs)
            oCurs[lWhichSelection].append(oRCurs)
    return oCurs

def IsAnythingSelected(oDoc):
    if(oDoc is None):
        return False
    oSels = oDoc.getCurrentSelection()
    if(oSels is None) or (oSels.getCount() == 0):
        return False

    if (oSels.getCount() > 1):
        return True
    else:
        oSel = oSels.getByIndex(0)
        #msgBox(oSel.ImplementationName)
        if oSel.supportsService("com.sun.star.text.XTextRange") :
            msgBox("Поддержка текстовых интервалов")


        oCursor = oSel.getText().createTextCursorByRange(oSel)
        if not oCursor.isCollapsed():
            return True
    return False


def GetLeftMostCursor(oSel):
    if oSel.getText().compareRegionStarts(oSel.getEnd(), oSel) >= 0 :
        oRange = oSel.getEnd()
    else:
        oRange = oSel.getStart()
    oCursor = oSel.getText().createTextCursorByRange(oRange)
    oCursor.goRight(0, False)
    return oCursor

def GetRightMostCursor(oSel):
    if oSel.getText().compareRegionStarts(oSel.getEnd(), oSel) >= 0 :
        oRange = oSel.getStart()
    else:
        oRange = oSel.getEnd()
    oCursor = oSel.getText().createTextCursorByRange(oRange)
    oCursor.goLeft(0, False)
    return oCursor

Libre Office 5.1.6.2, Linux Mint 18 (64 bit)
karpo518
 
Posts: 15
Joined: Wed May 24, 2017 4:17 pm

Re: createEnumeration problem

Postby hubert lambert » Mon Jun 19, 2017 11:56 am

Keeping a separate reference to paragraphs seems to work (could be a kind of bug):

Code: Select all   Expand viewCollapse view
   model = XSCRIPTCONTEXT.getDocument()
   selection = model.CurrentSelection
   elements = []
   for element in selection[0]:
      elements.append(element)
   for element in elements:
      if element.supportsService("com.sun.star.text.Paragraph") and element.String:
         element.String += " prepared\rtest paragraph"
AOOo 4.1.2 on Win7 | LibreOffice on various Linux systems
hubert lambert
 
Posts: 57
Joined: Mon Jun 13, 2016 10:50 am

Re: createEnumeration problem

Postby karpo518 » Mon Jun 19, 2017 12:16 pm

hubert lambert, thanck you. I will test this solution later.
Libre Office 5.1.6.2, Linux Mint 18 (64 bit)
karpo518
 
Posts: 15
Joined: Wed May 24, 2017 4:17 pm

Re: createEnumeration problem

Postby musikai » Wed Jun 21, 2017 12:15 pm

Does it work? If not then it might because you make an enumeration but while going through it you edit the document. Going in reverse order (backwards) might help. Also test if the selection range adapts if you are adding more paragraphs. The start should be the same but I'm not sure about the end.
Win7 Pro, Lubuntu 15.10, LO 4.4.7, OO 4.1.3
Free Project: LibreOffice Songbook Architect (LOSA)
http://struckkai.blogspot.de/2015/04/libreofficesongbookarchitect.html
musikai
Volunteer
 
Posts: 161
Joined: Wed Nov 11, 2015 12:19 am

Re: createEnumeration problem

Postby karpo518 » Wed Jun 21, 2017 12:47 pm

Thanks for answers. The proposed method solves my problem.
musikai, it is works, but it is compromiss solution. I wanted to get ability to adding paragraph without problem in loop or dynamic initialization of the iterator
Libre Office 5.1.6.2, Linux Mint 18 (64 bit)
karpo518
 
Posts: 15
Joined: Wed May 24, 2017 4:17 pm


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 5 guests