Endring av tekstdokument
Man har altså tilgang til deltekstene i dokumentet, hvorav noen er avsnitt som igjen har sine deltekster.
I dette avsnittet beskrives en "cursor" som lar en markere områder som skal endres.
TextCursor
Først: Den programstyrete "cursor" har intet med den synlige "cursor" som brukeren ser! Man kan ha flere cursorer. Under er et eksempel der man lager en cursor som flyttes ti tegn til venstre, så tre til høyre. Derved markeres (utvelges) syv tegn:
Cursor.goLeft(10, False) Cursor.goRight(3, True)
Som en ser er andre parameter True hvis en vil endre utvalget. Andre metoder er:
- goLeft (Count, Expand)
- jumps Count characters to the left.
- goRight (Count, Expand)
- jumps Count characters to the right.
- gotoStart (Expand)
- jumps to the start of the text document.
- gotoEnd (Expand)
- jumps to the end of the text document.
- gotoRange (TextRange, Expand)
- jumps to the specified TextRange-Object.
- gotoStartOfWord (Expand)
- jumps to the start of the current word.
- gotoEndOfWord (Expand)
- jumps to the end of the current word.
- gotoNextWord (Expand)
- jumps to the start of the next word.
- gotoPreviousWord (Expand)
- jumps to the start of the previous word.
- isStartOfWord ()
- returns True if the TextCursor is at the start of a word.
- isEndOfWord ()
- returns True if the TextCursor is at the end of a word.
- gotoStartOfSentence (Expand)
- jumps to the start of the current sentence.
- gotoEndOfSentence (Expand)
- jumps to the end of the current sentence.
- gotoNextSentence (Expand)
- jumps to the start of the next sentence.
- gotoPreviousSentence (Expand)
- jumps to the start of the previous sentence.
- isStartOfSentence ()
- returns True if the TextCursor is at the start of a sentence.
- isEndOfSentence ()
- returns True if the TextCursor is at the end of a sentence.
- gotoStartOfParagraph (Expand)
- jumps to the start of the current paragraph.
- gotoEndOfParagraph (Expand)
- jumps to the end of the current paragraph.
- gotoNextParagraph (Expand)
- jumps to the start of the next paragraph.
- gotoPreviousParagraph (Expand)
- jumps to the start of the previous paragraph.
- isStartOfParagraph ()
- returns True if the TextCursor is at the start of a paragraph.
- isEndOfParagraph ()
- returns True if the TextCursor is at the end of a paragraph.
Teksten deles inn i setninger atskilt av "setnings-symbol" som punktum. En ser at det andre parameter (ved navn Expand) er boolsk og True hvis området som passeres skal markeres (utheves). Alle metoder returnerer False hvis flyttingen ikke lot seg gjøre p.g.a. mangel av tekst og område.
Andre viktige metoder:
- 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.
Endring av utvalgt område kan gjøres som under, der første ord i hver setning blir uthevet.
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
Vil en endre det markerte området kan en gå via cursorens metode string(). Under blir første ordet i en setning endret:
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
Hvis cursor ikke har noe markert område (null lengde) blir teksten satt inn istedet for å erstatte. Kontrollkoder (diverse usynlige tegn) er blant andre:
- 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.
Vil en ha et nytt avsnitt etter tyve posisjoner:
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)
Parametret False gjør at det markerte området i cursor ikke endres etter innsettingen. Var den False ble det markerte området erstattet med kontrollkoden.
Leting etter tekst
Vil en lete etter tekst lages en letebeskrivelse som under:
Dim SearchDesc As Object SearchDesc = Doc.createSearchDescriptor SearchDesc.searchString="Stoltenberg"
Andre egenskaper (se com.sun.star.util.SearchDescriptor):
- 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.
Likhetssøk
Hvis en tåler "nesten" kan en bruke disse egenskapene i letebeskrivelsen:
- 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.
Under vises en gjennomleting basert på en letebeskrivelse:
Found = Doc.findFirst (SearchDesc) Do Until IsNull(Found) ' Edit search results... Found = Doc.findNext( Found.End, SearchDesc) Loop
Et funn er et objekt av typen TextRange.
Under er et eksempel der en leter etter "turnover" og lignende ("turnovers", "turnover's").
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
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). |