Tekstdocumenten bewerken

From Apache OpenOffice Wiki
< NL‎ | Documentation‎ | BASIC Guide
Revision as of 17:13, 9 February 2013 by DiGro (Talk | contribs)

Jump to: navigation, search
Book.png


In het vorige gedeelte is al een groot bereik aan opties besproken voor het bewerken van tekstdocumenten, geconcentreerd op de services com.sun.star.text.TextPortion en com.sun.star.text.Paragraph, welke toegang verlenen tot zowel de alinea's als de alinea-gedeelten. Deze services zijn nuttig voor toepassingen waarin de inhoud van een tekst moet worden bewerkt in één doorloop via een lus. Dit is echter voor veel problemen niet voldoende. Apache OpenOffice verschaft de service com.sun.star.text.TextCursor voor meer gecompliceerde taken, inclusief achterwaartse navigatie binnen een document of navigatie gebaseerd op zinnen en woorden in plaats van op TekstDelen.

De TextCursor

Een TextCursor in de API van Apache OpenOffice is te vergelijken met de zichtbare cursor die wordt gebruikt in een document van Apache OpenOffice. Het markeert een bepaald punt binnen een tekst en waar vandaan kan worden genavigeerd in diverse richtingen met behulp van opdrachten. De beschikbare objecten tt>TextCursor</tt> in Apache OpenOffice BASIC moeten echter niet verward worden met de zichtbare cursor. Dit zijn twee heel verschillende dingen.

Documentation note.png VBA : Terminologie verschilt van die in VBA gebruikt wordt: In termen van bereik van functie, het object Range van VBA kan worden vergeleken met het object TextCursor in Apache OpenOffice en niet – zoals de naam misschien suggereert– met het object Range in Apache OpenOffice.


Het object TextCursor in Apache OpenOffice, bijvoorbeeld, verschaft methoden om te navigeren en wijzigen van tekst die zijn opgenomen in het object Range in VBA (bijvoorbeeld, MoveStart, MoveEnd, InsertBefore, InsertAfter). De corresponderende onderdelen van het TextCursor object in Apache OpenOffice worden beschreven in het volgende gedeelte.

Navigeren binnen een tekst

Het object TextCursor in Apache OpenOffice BASIC werkt onafhankelijk van de zichtbare cursor in een tekstdocument. Een programma-gecontroleerde positie-verandering van een TextCursor object heeft geen enkel effect op de zichtbare cursor. Diverse objecten TextCursor kunnen zelfs geopend zijn voor hetzelfde document en gebruikt op verschillende posities, welke onafhankelijk van elkaar zijn.

Een object TextCursor wordt gemaakt met behulp van de aanroep createTextCursor:

Dim Doc As Object
Dim Cursor As Object
 
Doc = ThisComponent
Cursor = Doc.Text.createTextCursor()

Het op deze manier gemaakte object Cursor ondersteunt de service com.sun.star.text.TextCursor, welke op zijn beurt een groot scala aan methoden voor het navigeren binnen tekstdocumenten verschaft. Het volgende voorbeeld verplaatst eerst de TextCursor tien tekens naar links en dan drie tekens naar rechts:

Cursor.goLeft(10, False)
Cursor.goRight(3, False)

Een TextCursor kan een compleet gebied accentueren. Dit kan worden vergeleken met het accentueren van een punt in de tekst, met behulp van de muis. De parameter False in de vorige aanroep van de functie specificeert of het gebied waar de cursor zich over verplaatst, wordt geaccentueerd. Bijvoorbeeld, de TextCursor in het volgende voorbeeld

Cursor.goRight(10, False)
Cursor.goLeft(3, True)

verplaatst eerst tien tekens naar links zonder accentuering en gaat dan drie tekens terug en accentueert dat. Het geaccentueerde gebied door de TextCursor begint daarom na het zevende teken in de tekst en eindigt na het tiende teken.

Hier zijn de centrale methoden die de service com.sun.star.text.TextCursor verschaft voor navigatie:

goLeft (Aantal, Uitgebreid)
springt Aantal tekens naar links.
goRight (Aantal, Uitgebreid)
springt Aantal tekens naar rechts.
gotoStart (Uitgebreid)
springt naar het begin van het tekstdocument.
gotoEnd (Uitgebreid)
springt naar het einde van het tekstdocument.
gotoRange (TekstBereik, Uitgebreid)
springt naar het opgegeven object TekstBereik.
gotoStartOfWord (Uitgebreid)
springt naar het begin van het huidige woord.
gotoEndOfWord (Uitgebreid)
springt naar het einde van het huidige woord.
gotoNextWord (Uitgebreid)
springt naar het begin van het volgende woord.
gotoPreviousWord (Uitgebreid)
springt naar het begin van het vorige woord.
isStartOfWord ()
geeft True terug als de TextCursor aan het begin van een woord staat.
isEndOfWord ()
geeft True terug als de TextCursor aan het einde van een woord staat.
gotoStartOfSentence (Uitgebreid)
springt naar het begin van de huidige zin.
gotoEndOfSentence (Uitgebreid)
springt naar het einde van de huidige zin.
gotoNextSentence (Uitgebreid)
springt naar het begin van de volgende zin.
gotoPreviousSentence (Uitgebreid)
springt naar het begin van de vorige zin.
isStartOfSentence ()
geeft True terug als de TextCursor aan het begin van een zin staat.
isEndOfSentence ()
geeft True als de TextCursor aan het einde van een zin staat.
gotoStartOfParagraph (Uitgebreid)
springt naar het begin van de huidige alinea.
gotoEndOfParagraph (Uitgebreid)
springt naar het einde van de huidige alinea.
gotoNextParagraph (Uitgebreid)
springt naar het begin van de volgende alinea.
gotoPreviousParagraph (Uitgebreid)
springt naar het begin van de vorige alinea.
isStartOfParagraph ()
geeft True terug als de TextCursor aan het begin van een alinea staat.
isEndOfParagraph ()
geeft True terug als de TextCursor aan het einde van een alinea staat.

The text is divided into sentences on the basis of sentence symbols. Periods are, for example, interpreted as symbols indicating the end of sentences. (In English, at least, they must be followed by a space, tab, or return for this to work.)

The Expand parameter is a Boolean value which specifies whether the area passed over during navigation is to be highlighted. All navigation methods furthermore return a Boolean parameter which specifies whether the navigation was successful or whether the action was terminated for lack of text.

The following is a list of several methods for editing highlighted areas using a TextCursor and which also support the com.sun.star.text.TextCursor service:

collapseToStart ()
resets the highlighting and positions the TextCursor at the start of the previously highlighted area.
collapseToEnd ()
resets the highlighting and positions the TextCursor at the end of the previously highlighted area.
isCollapsed ()
returns True if the TextCursor does not cover any highlighting at present.

Formatting Text with TextCursor

The com.sun.star.text.TextCursor service supports all the character and paragraph properties that were presented at the start of this chapter.

The following example shows how these can be used in conjunction with a TextCursor. It passes through a complete document and formats the first word of every sentence in bold type.

Dim Doc As Object   
Dim Cursor As Object
Dim Proceed As Boolean
 
Doc = ThisComponent
Cursor = Doc.Text.createTextCursor
 
Do 
  Cursor.gotoEndOfWord(True)
  Cursor.CharWeight = com.sun.star.awt.FontWeight.BOLD
  Proceed = Cursor.gotoNextSentence(False)
  Cursor.gotoNextWord(False)
Loop While Proceed

The example first creates a document object for the text that has just been opened. Then it iterates through the entire text, sentence by sentence, and highlights each of the first words and formats this in bold.

Retrieving and Modifying Text Contents

If a TextCursor contains a highlighted area, then this text is available by means of the String property of the TextCursor object. The following example uses the String property to display the first words of a sentence in a message box:

Dim Doc As Object   
Dim Cursor As Object
Dim Proceed As Boolean
 
Doc = ThisComponent
Cursor = Doc.Text.createTextCursor
 
Do 
  Cursor.gotoEndOfWord(True)
  MsgBox Cursor.String
  Proceed = Cursor.gotoNextSentence(False)
  Cursor.gotoNextWord(False)
Loop While Proceed

The first word of each sentence can be modified in the same way using the String property:

Dim Doc As Object   
Dim Cursor As Object
Dim Proceed As Boolean
 
Doc = ThisComponent
Cursor = Doc.Text.createTextCursor
 
Do 
  Cursor.gotoEndOfWord(True)
  Cursor.String = "Ups"
  Proceed = Cursor.gotoNextSentence(False)
  Cursor.gotoNextWord(False)
Loop While Proceed

If the TextCursor contains a highlighted area, an assignment to the String property replaces this with the new text. If there is no highlighted area, the text is inserted at the present TextCursor position.

Inserting Control Codes

In some situations, it is not the actual text of a document, but rather its structure that needs modifying. Apache OpenOffice provides control codes for this purpose. These are inserted in the text and influence its structure. The control codes are defined in the com.sun.star.text.ControlCharacter group of constants. The following control codes are available in Apache OpenOffice:

PARAGRAPH_BREAK
paragraph break.
LINE_BREAK
line break within a paragraph.
SOFT_HYPHEN
possible point for syllabification.
HARD_HYPHEN
obligatory point for syllabification.
HARD_SPACE
protected space that is not spread out or compressed in justified text.

To insert the control codes, you need not only the cursor but also the associated text document objects. The following example inserts a paragraph after the 20th character of a text:

Dim Doc As Object   
Dim Cursor As Object
Dim Proceed As Boolean
 
Doc = ThisComponent
Cursor = Doc.Text.createTextCursor
Cursor.goRight(20, False)
Doc.Text.insertControlCharacter(Cursor, _
    com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, False)

The False parameter in the call of the insertControlCharacter method ensures that the area currently highlighted by the TextCursor remains after the insert operation. If the True parameter is passed here, then insertControlCharacter replaces the current text.

Searching for Text Portions

In many instances, it is the case that a text is to be searched for a particular term and the corresponding point needs to be edited. All Apache OpenOffice documents provide a special interface for this purpose, and this interface always functions in accordance with the same principle: Before a search process, what is commonly referred to as a SearchDescriptor must first be created. This defines what Apache OpenOffice searches for in a document. A SearchDescriptor is an object which supports the com.sun.star.util. SearchDescriptor service and can be created by means of the createSearchDescriptor method of a document:

Dim SearchDesc As Object
SearchDesc = Doc.createSearchDescriptor

Once the SearchDescriptor has been created, it receives the text to be searched for:

SearchDesc.searchString="any text"

In terms of its function, the SearchDescriptor is best compared with the search dialog from Apache OpenOffice. In a similar way to the search window, the settings needed for a search can be set in the SearchDescriptor object.

The properties are provided by the com.sun.star.util.SearchDescriptor service:

SearchBackwards (Boolean)
searches through the text backward rather than forward.
SearchCaseSensitive (Boolean)
takes uppercase and lowercase characters into consideration during the search.
SearchRegularExpression (Boolean)
treats the search expression like a regular expression.
SearchStyles (Boolean)
searches through the text for the specified paragraph template.
SearchWords (Boolean)
only searches for complete words.

The Apache OpenOffice SearchSimilarity (or “fuzzy match”) function is also available in Apache OpenOffice Basic. With this function, Apache OpenOffice searches for an expression that may be similar to but not exactly the same as the search expression. The number of additional, deleted and modified characters for these expressions can be defined individually. Here are the associated properties of the com.sun.star.util.SearchDescriptor service:

SearchSimilarity (Boolean)
performs a similarity search.
SearchSimilarityAdd (Short)
number of characters which may be added for a similarity search.
SearchSimilarityExchange (Short)
number of characters which may be replaced as part of a similarity search.
SearchSimilarityRemove (Short)
number of characters which may be removed as part of a similarity search.
SearchSimilarityRelax (Boolean)
takes all deviation rules into consideration at the same time for the search expression.

Once the SearchDescriptor has been prepared as requested, it can be applied to the text document. The Apache OpenOffice documents provide the findFirst and findNext methods for this purpose:

Found = Doc.findFirst (SearchDesc)
 
Do Until IsNull(Found)
  ' Edit search results...
  Found = Doc.findNext( Found.End, SearchDesc)
Loop

The example finds all matches in a loop and returns a TextRange object, which refers to the found text passage.

Example: Similarity Search

This example shows how a text can be searched for the word "turnover" and the results formatted in bold type. A similarity search is used so that not only the word “turnover”, but also the plural form "turnovers" and declinations such as "turnover's" are found. The found expressions differ by up to two letters from the search expression:

Dim SearchDesc As Object
Dim Doc As Object
 
Doc = ThisComponent
SearchDesc = Doc.createSearchDescriptor
SearchDesc.SearchString="turnover"
SearchDesc.SearchSimilarity = True
SearchDesc.SearchSimilarityAdd = 2
SearchDesc.SearchSimilarityExchange = 2
SearchDesc.SearchSimilarityRemove = 2
SearchDesc.SearchSimilarityRelax = False
Found = Doc.findFirst (SearchDesc)
 
Do Until IsNull(Found)
  Found.CharWeight = com.sun.star.awt.FontWeight.BOLD
  Found = Doc.findNext( Found.End, SearchDesc)
Loop
Documentation note.png VBA : The basic idea of search and replace in Apache OpenOffice is comparable to that used in VBA. Both interfaces provide you with an object, through which the properties for searching and replacing can be defined. This object is then applied to the required text area in order to perform the action. Whereas the responsible auxiliary object in VBA can be reached through the Find property of the Range object, in Apache OpenOffice Basic it is created by the createSearchDescriptor or createReplaceDescriptor call of the document object. Even the search properties and methods available differ.


As in the old API from Apache OpenOffice, searching and replacing text in the new API is also performed using the document object. Whereas previously there was an object called SearchSettings especially for defining the search options, in the new object searches are now performed using a SearchDescriptor or ReplaceDescriptor object for automatically replacing text. These objects cover not only the options, but also the current search text and, if necessary, the associated text replacement. The descriptor objects are created using the document object, completed in accordance with the relevant requests, and then transferred back to the document object as parameters for the search methods.

Replacing Text Portions

Just as with the search function, the replacement function from Apache OpenOffice is also available in Apache OpenOffice Basic. The two functions are handled identically. A special object which records the parameters for the process is also first needed for a replacement process. It is called a ReplaceDescriptor and supports the com.sun.star.util.ReplaceDescriptor service. All the properties of the SearchDescriptor described in the previous paragraph are also supported by ReplaceDescriptor. For example, during a replacement process, case sensitivity can also be activated and deactivated, and similarity searches can be performed.

The following example demonstrates the use of ReplaceDescriptors for a search within a Apache OpenOffice document.

Dim I As Long
Dim Doc As Object
Dim Replace As Object
Dim BritishWords(5) As String
Dim USWords(5) As String
 
BritishWords() = Array("colour", "neighbour", "centre", "behaviour", _
   "metre", "through")
USWords() = Array("color", "neighbor", "center", "behavior", _
   "meter", "thru")
 
Doc = ThisComponent
Replace = Doc.createReplaceDescriptor
 
For I = 0 To 5
  Replace.SearchString = BritishWords(I)
  Replace.ReplaceString = USWords(I)
  Doc.replaceAll(Replace)
Next I

The expressions for searching and replacing are set using the SearchString and ReplaceString properties of the ReplaceDescriptors. The actual replacement process is finally implemented using the replaceAll method of the document object, which replaces all occurrences of the search expression.

Example: searching and replacing text with regular expressions

The replacement function of Apache OpenOffice is particularly effective when used in conjunction with regular expressions. These provide the option of defining a variable search expression with place holders and special characters rather than a fixed value.

The regular expressions supported by Apache OpenOffice are described in detail in the online help section for Apache OpenOffice. Here are a few examples:

  • A period within a search expression stands for any character. The search expression sh.rt therefore can stand for both for shirt and for short.
  • The character ^ marks the start of a paragraph. All occurrences of the name Peter that are at the start of a paragraph can therefore be found using the search expression ^Peter.
  • The character $ marks a paragraph end. All occurrences of the name Peter that are at the end of a paragraph can therefore be found using the search expression Peter$.
  • A * indicates that the preceding character may be repeated any number of times. It can be combined with the period as a place holder for any character. The temper.*e expression, for example, can stand for the expressions temperance and temperature.

The following example shows how all empty lines in a text document can be removed with the help of the regular expression ^$:

Dim Doc As Object
Dim Replace As Object
Dim I As Long
 
Doc = ThisComponent
Replace = Doc.createReplaceDescriptor
Replace.SearchRegularExpression = True
Replace.SearchString = "^$"
Replace.ReplaceString = ""
 
Doc.replaceAll(Replace)


Content on this page is licensed under the Public Documentation License (PDL).
Personal tools