Writer/API/TextRange

From Apache OpenOffice Wiki
< Writer(Redirected from TextRange)
Jump to: navigation, search

Writer Icon.png

Writer Project

Please view the guidelines
before contributing.

Popular Subcategories: The DPL extension (version 2.3.0) produced a SQL statement which lead to a Database error.
The reason may be an internal error of DPL or an error which you made,
especially when using DPL options like titleregexp.
Query text is:
SELECT DISTINCT `page`.page_namespace AS page_namespace,`page`.page_title AS page_title,`page`.page_id AS page_id, rev_user, rev_user_text, rev_comment, rev_timestamp FROM `revision` AS rev, `page` WHERE 1=1 AND `page`.page_namespace IN (14) AND `page`.page_is_redirect=0 AND `page`.page_id=rev.rev_page AND rev.rev_timestamp=( SELECT MAX(rev_aux.rev_timestamp) FROM `revision` AS rev_aux WHERE rev_aux.rev_page=rev.rev_page ) ORDER BY rev_timestamp DESC LIMIT 5 OFFSET 0

Error message is:
Unknown column 'rev_user' in 'field list' (127.0.0.1)


Internal Documentation: The DPL extension (version 2.3.0) produced a SQL statement which lead to a Database error.
The reason may be an internal error of DPL or an error which you made,
especially when using DPL options like titleregexp.
Query text is:
SELECT DISTINCT `page`.page_namespace AS page_namespace,`page`.page_title AS page_title,`page`.page_id AS page_id, rev_user, rev_user_text, rev_comment, rev_timestamp FROM `revision` AS rev, `page` WHERE 1=1 AND `page`.page_namespace IN (0) AND `page`.page_is_redirect=0 AND `page`.page_id=rev.rev_page AND rev.rev_timestamp=( SELECT MAX(rev_aux.rev_timestamp) FROM `revision` AS rev_aux WHERE rev_aux.rev_page=rev.rev_page ) ORDER BY rev_timestamp DESC LIMIT 5 OFFSET 0

Error message is:
Unknown column 'rev_user' in 'field list' (127.0.0.1)


API Documentation:

Ongoing Efforts: The DPL extension (version 2.3.0) produced a SQL statement which lead to a Database error.
The reason may be an internal error of DPL or an error which you made,
especially when using DPL options like titleregexp.
Query text is:
SELECT DISTINCT `page`.page_namespace AS page_namespace,`page`.page_title AS page_title,`page`.page_id AS page_id, rev_user, rev_user_text, rev_comment, rev_timestamp FROM `revision` AS rev, `page` WHERE 1=1 AND `page`.page_namespace IN (0) AND `page`.page_is_redirect=0 AND `page`.page_id=rev.rev_page AND rev.rev_timestamp=( SELECT MAX(rev_aux.rev_timestamp) FROM `revision` AS rev_aux WHERE rev_aux.rev_page=rev.rev_page ) ORDER BY rev_timestamp DESC LIMIT 5 OFFSET 0

Error message is:
Unknown column 'rev_user' in 'field list' (127.0.0.1)


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