Jump to content

マクロ/Writer/001

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


説明

このチュートリアルでは、カーソル位置にカスタムコメントを挿入する Writer 用のマクロの作成方法について説明します(たとえば、作成者日付 フィールドを事前に設定します)。

実行時からタイムスタンプを取得するために、com.sun.star.text.XTextViewCursor[1]com.sun.star.text.textfield.Annotation services[2]、および com.sun.star.util.DateTime structure[3] を使用します。

Code

たとえば、キーボードショートカットとリンクできる次のマクロは、現在のカーソル位置にカスタムコメントを挿入します。
Basicの場合:

Option Explicit

Sub InsertComment
    '''カーソル位置にコメントを挿入'''

    Dim oCurs As Object ' com.sun.star.text.text.XTextViewCursor
    Dim oAnnot As Object ' com.sun.star.text.textfield.Annotation
    Dim oDateTime As New com.sun.star.util.DateTime

    oCurs = ThisComponent.CurrentController.getViewCursor()
    oAnnot = ThisComponent.createInstance( _
        "com.sun.star.text.textfield.Annotation")

    With oDateTime ' com.sun.star.util.DateTime
        .Minutes = 20
        .Hours = 12
        .Day = 20
        .Month = 12
        .Year = 2012
    End With
   
    With oAnnot ' com.sun.star.text.textfield.Annotation
        .Author = "コメントの作成者"
        .Content = "コメントの内容"
        .DateTimeValue = oDateTime
    End With

    oAnnot.attach(oCurs.Start) ' カーソル位置に挿入

End Sub ' InsertComment

Pythonの場合:

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

import uno

def InsertComment():
    """カーソル位置にコメントを挿入
    
    curs    com.sun.star.text.text.XTextViewCursor
    annot   com.sun.star.text.textfield.Annotation
    """
    dt = uno.createUnoStruct("com.sun.star.util.DateTime")

    thisComponent = XSCRIPTCONTEXT.getDocument()
    curs = thisComponent.CurrentController.getViewCursor()
    annot = thisComponent.createInstance(
        "com.sun.star.text.textfield.Annotation")

    dt.Minutes = 20
    dt.Hours = 12
    dt.Day = 20
    dt.Month = 12
    dt.Year = 2012

    annot.Author = "コメントの作成者"
    annot.Content = 'コメントの内容'
    annot.DateTimeValue = dt

    annot.attach(curs.Start)  # カーソル位置に挿入

これらの例では、タイムスタンプ計算は実行されません。

メモ