Jump to content

マクロ/Writer/007

From The Document Foundation Wiki
This page is a translated version of the page Macros/Writer/007 and the translation is 100% complete.


説明

Writer 用のこのBasicマクロは、2つの境界文字列(「開始タグ」と「終了タグ」)の間に含まれるテキストを識別します。たとえば、説明の最初と最後の間にあるテキストなどです。

その原理は、「終了タグ」を検索するための開始位置を与える一意の「開始タグ」を検索することです。これらの2つの位置は、「開始タグ」の末尾から「終了タグ」の先頭まで、表示されているカーソルによって選択されます。

検索は、com.sun.star.util.XSearchable[1] インタフェースの createSearchDescriptor メソッドと com.sun.star.util.SearchDescriptor service[2] を使用して実行され、検索のプロパティ(検索テキスト、大文字と小文字の区別、正規表現の使用など)が定義されます。

com.sun.star.text.XTextViewCursor[3] service は、検索されたテキストを表示するために使用されます。

Code

以下のコードでは、開始タグと終了タグはそれぞれ「説明の開始」と「説明の終了」です。
Basicの場合ː

Option Explicit

Sub SearchText()
    '''2つのタグの間にあるテキストを検索して強調表示'''

    Dim oDoc As Object ' com.sun.star.text.GenericTextDocument, com.sun.star.util.XSearchable
    Dim oSearchZone As Object ' com.sun.star.util.SearchDescriptor
    Dim oFoundZone As Object ' com.sun.star.uno.XInterface
    Dim oFoundAfterZone As Object ' com.sun.star.uno.XInterface
    Dim oCursVisible As Object ' com.sun.star.text.XTextViewCursor

    oDoc = ThisComponent ' com.sun.star.util.XSearchable compliant document
    oSearchZone = oDoc.createSearchDescriptor()
    With oSearchZone ' com.sun.star.util.SearchDescriptor
        .SearchString = "説明の開始"
    End With
    oFoundZone = oDoc.findFirst(oSearchZone)

    If Not(IsNull(oFoundZone)) Then
        With oSearchZone ' com.sun.star.util.SearchDescriptor
            .SearchString = "説明の終了"
        End With
        oFoundAfterZone = oDoc.findNext(oFoundZone.End, oSearchZone)
        If Not(IsNull(oFoundAfterZone)) Then
            oCursVisible = oDoc.currentcontroller.viewCursor
            With oCursVisible ' com.sun.star.text.text.XTextViewCursor
                .gotoRange(oFoundZone.End, false)
                .gotoRange(oFoundAfterZone.Start, True)
                MsgBox .String, MB_ICONINFORMATION, "テキストが見つかりました!"
            End With
        Else
            MsgBox "終了タグが見つかりませんでした", MB_ICONINFORMATION, "テキスト検索"
        End If
    Else
        MsgBox "開始タグが見つかりませんでした", MB_ICONINFORMATION, "テキスト検索"
    End If

End Sub ' 検索テキスト

Pythonの場合ː

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

MB_ICONINFORMATION = 64

def SearchText():
    """2つのタグの間にあるテキストを検索して強調表示
    
    oSearchZone         com.sun.star.util.SearchDescriptor
    oFound(After)Zone   com.sun.star.uno.XInterface
    oCursVisible        com.sun.star.text.XTextViewCursor
    """
    oDoc = XSCRIPTCONTEXT.getDocument()  # com.sun.star.util.XSearchable 準拠ドキュメント
    oSearchZone = oDoc.createSearchDescriptor()
    oSearchZone.SearchString = "説明の開始"
    oFoundZone = oDoc.findFirst(oSearchZone)
    if oFoundZone:
        oSearchZone.SearchString = '説明の終了'
        oFoundAfterZone = oDoc.findNext(oFoundZone.End, oSearchZone)
        if oFoundAfterZone:
            oCursVisible = oDoc.CurrentController.ViewCursor
            oCursVisible.gotoRange(oFoundZone.End, False)
            oCursVisible.gotoRange(oFoundAfterZone.Start, True)
            MsgBox(oCursVisible.String,MB_ICONINFORMATION, "テキストが見つかりました!")
        else:
            MsgBox('終了タグが見つかりませんでした',MB_ICONINFORMATION, "テキスト検索")
    else:
        MsgBox("開始タグが見つかりませんでした",MB_ICONINFORMATION, 'テキスト検索')

def MsgBox(prompt="", buttons=MB_ICONINFORMATION, title=""):  # テキスト検索
    pass

Pythonの標準出力ファイルは、Pythonマクロの実行時には使用できません。ヘルプページ Input/Output to Screen では、別の方法を紹介しています。

マクロテスト用の ODT ファイル

メモ