Jump to content

Macro per l'inserimento di un commento con impostazioni personalizzate

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


Descrizione

Questo tutorial spiega come scrivere una macro per Writer che inserisce un commento personalizzato nel punto in cui si trova il cursore (allo scopo, per esempio, di valorizzare i campi Autore e/o Data).

Vengono usati i servizi com.sun.star.text.XTextViewCursor[1] e com.sun.star.text.textfield.Annotation services[2], così come la struttura com.sun.star.util.DateTime[3] al fine di ottenere una marcatura temporale dell'ora di esecuzione.

Codice

La macro seguente, che volendo può essere collegata ad una combinazione di tasti, inserire un commento personalizzato nella posizione in cui si trova il cursore:

Option Explicit

Sub InsertComment
    '''Inserisce un commento nella posizione in cui si trova il cursore'''

    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 = "Autore del commento"
        .Content = "Contenuto del commento"
        .DateTimeValue = oDateTime
    End With

    oAnnot.attach(oCurs.Start) ' Inserisce alla posizione iniziale del cursore

End Sub ' InsertComment

Usando Python:

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

import uno

def InsertComment():
    """Inserisce un commento nella posizione in cui si trova il cursore
    
    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 = "Autore del commento"
    annot.Content = 'Contenuto del commento'
    annot.DateTimeValue = dt

    annot.attach(curs.Start)  # Inserisce un commento nella posizione in cui si trova il cursore

In questi esempi non viene eseguita la marcatura temporale

Note