0

请注意,这是一个自我回答的问题,供参考。
大多数引用为com.sun.star.text.textfield.Annotation:: Datecom :: sun :: star :: util :: 引用,但没有结束对内容的摆弄实际上会创建一个带有日期的注释。
设置 Date.Year、Date.Month 和 Date.Day 会显示成功,但注释本身仍然显示没有日期,即

anno = model.createInstance("com.sun.star.text.textfield.Annotation")
anno.Content = "this is my annotation/comment"
anno.Author = doc.DocumentProperties.Author
anno.Date.Year = 2020
anno.Date.Month = 5
anno.Date.Day = 18

在此处输入图像描述

4

2 回答 2

0

Andrew 的宏文档的第 7.7.2 节给出了以下内容,虽然我没有测试过。

Sub AddNoteAtCursor
    Dim vDoc, vViewCursor, oCurs, vTextField
    Dim s$
    'Lets lie and say that this was added ten days ago!'
    Dim aDate As New com.sun.star.util.Date
    With aDate
        .Day = Day(Now - 10)
        .Month = Month(Now - 10)
        .Year = Year(Now - 10)
    End With
    vDoc = ThisComponent
    vViewCursor = vDoc.getCurrentController().getViewCursor()
    oCurs=vDoc.getText().createTextCursorByRange(vViewCursor.getStart())
    s = "com.sun.star.text.TextField.Annotation"
    vTextField = vDoc.createInstance(s)
    With vTextField
        .Author = "AP"
        .Content = "It sure is fun to insert notes into my document"
        'Ommit the date and it defaults to today!'
        .Date = aDate
    End With
    vDoc.Text.insertTextContent(oCurs, vTextField, False)
End Sub

API 文档包含与 IDL 文件相同的信息,但更易于阅读。 https://www.openoffice.org/api/docs/common/ref/com/sun/star/text/textfield/Annotation.html

于 2018-05-18T22:27:21.520 回答
0

文档并不总是完整的,或者并不明显,这取决于您查看的位置。
对于 LibreOffice 6.0 https://api.libreoffice.org/docs/idl/ref/Annotation_8idl_source.html
Annotation.idl 描述为:

 service  com::sun::star::text::TextField;
 [property]string Author;
 [optional, property]string Initials;
 [optional, property]string Name;
 [property]string Content;
 [property]com::sun::star::util::Date Date;
 [optional, property]com::sun::star::util::DateTime DateTimeValue;

这里的关键是DateTimeValue它会出现的可选项,它是需要设置以提供带有日期和时间的注释的项目。
DateTimeValue结构来自com.sun.star.util.DateTime
要使用 python 脚本在编写器文档中创建注释(带有日期和时间),请使用以下作为模板。

from uno import createUnoStruct
import time

def fs_Annotation(*args):
    #get the doc from the scripting context which is made available to all scripts
    desktop = XSCRIPTCONTEXT.getDesktop()
    model = desktop.getCurrentComponent()
    try:
        text = model.Text
    except:
        # The macro has been called externally but LibreOffice was not running at the time
        return None
    tRange = text.End
    cursor = desktop.getCurrentComponent().getCurrentController().getViewCursor()
    doc = XSCRIPTCONTEXT.getDocument()
    # you cannot insert simple text and text into a table with the same method
    # so we have to know if we are in a table or not.
    # oTable and oCurCell will be null if we are not in a table
    oTable = cursor.TextTable
    oCurCell = cursor.Cell

    anno = model.createInstance("com.sun.star.text.textfield.Annotation")

    anno.Content = "this is my annotation/comment"
    #Use documents author
    #anno.Author = doc.DocumentProperties.Author
    #Use hardcoded text
    anno.Author = "Joe Bloggs"

    t = time.localtime()
    dtv=createUnoStruct("com.sun.star.util.DateTime")
    dtv.Year = t.tm_year
    dtv.Month = t.tm_mon
    dtv.Day = t.tm_mday
    dtv.Hours = t.tm_hour
    dtv.Minutes= t.tm_min
    dtv.Seconds = t.tm_sec
    dtv.NanoSeconds = 0
    anno.DateTimeValue = dtv

    if oCurCell == None:    # Inserting into text
        text.insertTextContent(cursor, anno, True)
    else:                   # Inserting into a table
        oCurCell.insertTextContent(cursor, anno, False)
    return None

在此处输入图像描述

于 2018-05-18T15:43:22.403 回答