Ask Your Question
0

Is there a way to select the inverse of what is selected?

asked 2019-11-02 17:53:45 +0100

Tobruk gravatar image

updated 2019-11-02 17:54:26 +0100

I'm working with a really big file so manual stuff is out of the question, therefore I ask for help :)

Say I just selected a specific style via "Search and Replace" that is the main body of the text. I'd like to delete text that uses other styles, but since there are almost a hundred other styles, I cannot pick each style via "Search and Replace" and hit delete.

So, since I have the main body text selected, can I invert it somehow and then hit delete?

Kindly appreciated :)

edit retag flag offensive close merge delete

Comments

When you have selected everything you need to keep, copy to clipboard, and either paste into a new document, or remove everything from this document and paste.

Mike Kaganski gravatar imageMike Kaganski ( 2019-11-02 18:10:26 +0100 )edit

@Mike-Kaganski yes, I did try that, but copying and pasting removes newlines for whatever reason.

Tobruk gravatar imageTobruk ( 2019-11-02 18:46:32 +0100 )edit

1° part This code creates a new section at the bottom of the document and copies the found paragraphs to it by adding a carriage return If you protect the section from writing, deleting everything should keep the paragraphs saved in the section (But I haven't tried this)

function pst(o, oV, oFr, oDsp,d)
   d.CurrentController.select(o)     
   oDsp.executeDispatch(oFr, ".uno:Copy", "", 0, Array())
   oT=d.text
   oV.gotoRange(oT.getEnd(), False)
   oDsp.executeDispatch(oFr, ".uno:Paste", "", 0, Array()) 
   oV.gotoRange(oT.getEnd(), False)
   oV.text.InsertControlCharacter(oV,_
           com.sun.star.text.ControlCharacter.APPEND_PARAGRAPH, False)  
   oV.gotoRange(oT.getEnd(), False)
   pst=oV
end function
torreone gravatar imagetorreone ( 2019-11-03 01:28:08 +0100 )edit

2° part

sub x()
   dim d, oCt, oDsp, oFr, oV, oS, n, i
   d=ThisComponent
   oDsp=  createUnoService("com.sun.star.frame.DispatchHelper")
   oCt= d.CurrentController
   oFr=oCt.Frame
   oV = oCt.getViewCursor() 
   oS = d.createInstance("com.sun.star.text.TextSection")
   oS.name="gg"
   d.text.insertTextContent(d.text.end,oS, false) 
   oV.gotoRange(oS.Anchor.Start,false)

   oFind = d.createSearchDescriptor()
   oFind.searchStyles=true 
   oFind.SearchString = "Heading 2" 
   oF = d.findAll(oFind)
   n=oF.count
   If n > 0 Then
      d.lockControllers ()  'freeze screen refresh
      For i = 0 to n -1
        oV=pst(oF.getByIndex(i).textParagraph, oV, oFr, oDsp,d)
      next i
      d.unlockControllers () ' enable screen refresh
   End If
end sub
torreone gravatar imagetorreone ( 2019-11-03 01:29:47 +0100 )edit

1 Answer

Sort by » oldest newest most voted
1

answered 2019-11-03 18:43:04 +0100

torreone gravatar image

updated 2019-11-03 18:45:13 +0100

It seems to me correct, I try to give it as an answer

This code creates a new section at the bottom of the document and copy the found paragraphs to it by adding for everyone a carriage return

If you protect the section from writing, deleting everything should keep the paragraphs saved in the section (But I haven't tried this)

function pst(o, oV, oFr, oDsp,d)
   d.CurrentController.select(o)     
   oDsp.executeDispatch(oFr, ".uno:Copy", "", 0, Array())
   oT=d.text
   oV.gotoRange(oT.getEnd(), False)
   oDsp.executeDispatch(oFr, ".uno:Paste", "", 0, Array()) 
   oV.gotoRange(oT.getEnd(), False)
   oV.text.InsertControlCharacter(oV,_
           com.sun.star.text.ControlCharacter.APPEND_PARAGRAPH, False)  
   oV.gotoRange(oT.getEnd(), False)
   pst=oV
end function

sub main()
   dim d, oCt, oDsp, oFr, oV, oS, n, i
   d=ThisComponent
   oDsp=  createUnoService("com.sun.star.frame.DispatchHelper")
   oCt= d.CurrentController
   oFr=oCt.Frame
   oV = oCt.getViewCursor() 
   oS = d.createInstance("com.sun.star.text.TextSection")
   oS.name="gg"
   d.text.insertTextContent(d.text.end,oS, false) 
   oV.gotoRange(oS.Anchor.Start,false)

   oFind = d.createSearchDescriptor()
   oFind.searchStyles=true 
   oFind.SearchString = "Heading 2" 
   oF = d.findAll(oFind)
   n=oF.count
   If n > 0 Then
      d.lockControllers ()  'freeze screen refresh

      For i = 0 to n -1
        oV=pst(oF.getByIndex(i).textParagraph, oV, oFr, oDsp,d)
      next i

      d.unlockControllers () ' enable screen refresh
   End If
end sub
edit flag offensive delete link more

Comments

Thank you, it works. One drawback of using your code was the black background of almost all the copied text that cannot be deleted or changed. Or maybe that had to do with the bungled mess the file was (although the file did not use backgrounds other than default white)? I'm not sure.

Tobruk gravatar imageTobruk ( 2019-11-03 22:13:47 +0100 )edit
1

I'm glad it works. I had noticed that problem of losing the carriage return when I tried copying and pasting. I have re-adapted the function I used to solve the problem.

To try to restore the background, any changes should still be made before protecting the section from writing (if this was the problem). If you copy paragraphs formatted with a single style, it should be enough to change the background of text in the style.

If it doesn't work, you can try to select the whole section and manually force the background to white At worst. you could also try to remove all the direct formatting in the copied text but menu format or with CTRL + M (if the problem were some previous manual formatting).

Clearly, you would lose all the manual formatting you would like to keep If you do not attach the file after deleting ...(more)

torreone gravatar imagetorreone ( 2019-11-03 22:36:34 +0100 )edit
1

Once you have deleted all the useless text, you could also try to copy the saved text, copy it elsewhere in the document as unformatted text and try to reapply the original style

torreone gravatar imagetorreone ( 2019-11-03 22:40:50 +0100 )edit

Thank you again for all useful suggestions. I have been struggling with this project for a couple days and your code was a godsent!

Tobruk gravatar imageTobruk ( 2019-11-04 14:35:44 +0100 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2019-11-02 17:53:45 +0100

Seen: 672 times

Last updated: Nov 03 '19