Difference between revisions of "Writer/API/TextRange"

From Apache OpenOffice Wiki
Jump to: navigation, search
m
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{Writer Project}}
+
{{Writer Project|Category=Writer/API}}
 
__TOC__
 
__TOC__
 
== Working with Text Ranges ==
 
== Working with Text Ranges ==
  
The [[ Current selection]], the [[ View cursor]] and not visible [[ Text cursor]]s all provide the [[ TextRange]] service.  
+
The [[Current selection]], the [[View cursor]] and the not-visible [[Text cursor]]s all provide the [[Writer/API/TextRange|TextRange]] service.  
  
As does <tt>thisComponent.text</tt>, and to make life more incestous text portion enumerations.  
+
As does <tt>thisComponent.text</tt>, and to make life more incestuous, so do text portion enumerations.  
  
It is this service which provides the methods and properties for changing the text.  
+
It is this service which provides the methods and properties for changing the text.
  
 
== Inserting text ==
 
== Inserting text ==
Line 14: Line 14:
 
The following routine inserts text at the view cursor.  
 
The following routine inserts text at the view cursor.  
  
<source lang="oobas">
+
<syntaxhighlight lang="oobas">
 
sub subInsertString(oDoc, sString, bReplace)
 
sub subInsertString(oDoc, sString, bReplace)
 
oVC = oDoc.getCurrentController.getViewCursor
 
oVC = oDoc.getCurrentController.getViewCursor
Line 20: Line 20:
 
oText.insertString(oVC, sString, bReplace)
 
oText.insertString(oVC, sString, bReplace)
 
end sub
 
end sub
</source>
+
</syntaxhighlight>
 
The following example inserts the text "Some sample text" to the left of the current selection, leaving the orginal selection still selected.  
 
The following example inserts the text "Some sample text" to the left of the current selection, leaving the orginal selection still selected.  
  
<source lang="oobas">
+
<syntaxhighlight lang="oobas">
 
subInsertString(thisComponent, "Some sample text", false)
 
subInsertString(thisComponent, "Some sample text", false)
</source>
+
</syntaxhighlight>
  
 
== Inserting control codes ==
 
== Inserting control codes ==
<source lang="oobas">
+
<syntaxhighlight lang="oobas">
 
sub subInsertParagraphMarker(oDoc)
 
sub subInsertParagraphMarker(oDoc)
 
oVC = oDoc.getCurrentController.getViewCursor
 
oVC = oDoc.getCurrentController.getViewCursor
Line 34: Line 34:
 
oText.insertControlCharacter(oVC, com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, False)
 
oText.insertControlCharacter(oVC, com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, False)
 
end sub
 
end sub
</source>
+
</syntaxhighlight>
 
The control codes are:  
 
The control codes are:  
  
Line 70: Line 70:
 
To change the properties of a piece of text, select the text e.g. with a cursor, and then use the <tt>setPropertyValue</tt> method.  
 
To change the properties of a piece of text, select the text e.g. with a cursor, and then use the <tt>setPropertyValue</tt> method.  
  
See [[ working with styles]] for an example.  
+
See [[working with styles]] for an example.  
  
 
== Paragraphs ==
 
== Paragraphs ==
 
It is possible to create an enumeration of paragraphs (and text tables) from an object with service [[ TextRange]].  
 
It is possible to create an enumeration of paragraphs (and text tables) from an object with service [[ TextRange]].  
  
<source lang="oobas">
+
<syntaxhighlight lang="oobas">
 
' Create enumeration object
 
' Create enumeration object
 
oTextElementEnum = thisComponent.getText.createEnumeration
 
oTextElementEnum = thisComponent.getText.createEnumeration
Line 90: Line 90:
 
         end if
 
         end if
 
wend
 
wend
</source>
+
</syntaxhighlight>
  
 
== Text Portions ==
 
== Text Portions ==
Line 97: Line 97:
 
The following function counts the number of portions in a paragraph.  
 
The following function counts the number of portions in a paragraph.  
  
<source lang="oobas">
+
<syntaxhighlight lang="oobas">
 
function fnNoPortions(oPara)
 
function fnNoPortions(oPara)
 
   
 
   
Line 108: Line 108:
 
fnNoPortions = i
 
fnNoPortions = i
 
end function
 
end function
</source>
+
</syntaxhighlight>
 
An example of calling this function is:  
 
An example of calling this function is:  
  
<source lang="oobas">
+
<syntaxhighlight lang="oobas">
 
thisComponent.getCurrentSelection.getByIndex(i).createEnumeration
 
thisComponent.getCurrentSelection.getByIndex(i).createEnumeration
 
while oTextElementEnum.hasMoreElements()
 
while oTextElementEnum.hasMoreElements()
Line 122: Line 122:
 
         end if
 
         end if
 
wend
 
wend
</source>
+
</syntaxhighlight>
For more routines see [[ Working with properties]].
+
For more routines see [[Working with properties]].
  
[[Category:Writer/API]]
 
 
[[Category:StarBasic]]
 
[[Category:StarBasic]]

Latest revision as of 13:46, 5 March 2021

Writer Icon.png

Writer Project

Please view the guidelines
before contributing.

Popular Subcategories:

Extension:DynamicPageList (DPL), version 2.3.0 : Warning: No results.

Internal Documentation:

Extension:DynamicPageList (DPL), version 2.3.0 : Warning: No results.

API Documentation:

Ongoing Efforts:

Extension:DynamicPageList (DPL), version 2.3.0 : Warning: No results.

Sw.OpenOffice.org

Working with Text Ranges

The Current selection, the View cursor and the not-visible Text cursors all provide the TextRange service.

As does thisComponent.text, and to make life more incestuous, so do text portion enumerations.

It is this service which provides the methods and properties for changing the text.

Inserting text

Inserting text is accomplished with the method insertString. The parameters to this function are the location in the text range to insert the string at, the string itself, and whether the text at the specified location gets replaced.

The following routine inserts text at the view cursor.

sub subInsertString(oDoc, sString, bReplace)
oVC = oDoc.getCurrentController.getViewCursor
oText = oVC.text
oText.insertString(oVC, sString, bReplace)
end sub

The following example inserts the text "Some sample text" to the left of the current selection, leaving the orginal selection still selected.

subInsertString(thisComponent, "Some sample text", false)

Inserting control codes

sub subInsertParagraphMarker(oDoc)
oVC = oDoc.getCurrentController.getViewCursor
oText = oVC.text
oText.insertControlCharacter(oVC, com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, False)
end sub

The control codes are:

0 com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK This control character starts a new paragraph.
1 com.sun.star.text.ControlCharacter.LINE_BREAK This control character starts a new line in a paragraph.
2 com.sun.star.text.ControlCharacter.HARD_HYPHEN This control character equals a dash but prevents this position from being hyphenated.
3 com.sun.star.text.ControlCharacter.SOFT_HYPHEN This control character defines a special position as a hyphenation point. If a word containing a soft hyphen must be split at the end of a line, then this position is preferred.
4 com.sun.star.text.ControlCharacter.HARD_SPACE This control character is used to link two words and prevents this concatenation from being hyphenated. It is printed as a space.
5 com.sun.star.text.ControlCharacter.APPEND_PARAGRAPH This control character appends a new paragraph at the end of the current paragraph.


Although you can insert control codes by inserting the equivalent string (e.g. chr(10) for LINE_BREAK), it is safer to use insertControlCharacter as the screen gets updated properly.

Changing properties

To change the properties of a piece of text, select the text e.g. with a cursor, and then use the setPropertyValue method.

See working with styles for an example.

Paragraphs

It is possible to create an enumeration of paragraphs (and text tables) from an object with service TextRange.

' Create enumeration object
oTextElementEnum = thisComponent.getText.createEnumeration
'or thisComponent.getCurrentSelection.getByIndex(i).createEnumeration
 
' loop over all text elements
while oTextElementEnum.hasMoreElements()
        oTextElement = oTextElementEnum.nextElement
        if oTextElement.supportsService("com.sun.star.text.TextTable") then
                MsgBox "The current block contains a table."
        end if
        if oTextElement.supportsService("com.sun.star.text.Paragraph") then
                MsgBox "The current block contains a paragraph."
        end if
wend

Text Portions

A paragraph consists of text portions. A text portions is a block of text within a paragraph with identical formatting.

The following function counts the number of portions in a paragraph.

function fnNoPortions(oPara)
 
oPortionEnum = oPara.createEnumeration
i = 0
while oPortionEnum.hasMoreElements
        i = i + 1
        oPortionEnum.nextElement
wend
fnNoPortions = i
end function

An example of calling this function is:

thisComponent.getCurrentSelection.getByIndex(i).createEnumeration
while oTextElementEnum.hasMoreElements()
        oTextElement = oTextElementEnum.nextElement
        if oTextElement.supportsService("com.sun.star.text.TextTable") then
                MsgBox "The current block contains a table."
        end if
        if oTextElement.supportsService("com.sun.star.text.Paragraph") then
                MsgBox "The paragraph contains " & fnNoPortions(oTextElement) & "text portions"
        end if
wend

For more routines see Working with properties.

Personal tools