使用BASIC在LibreOffice中复制表的最后一行

时间:2016-11-09 09:49:56

标签: vba libreoffice libreoffice-basic libreoffice-writer

我在ODT文档中有一个命名表,想要将包含所有内容的最后一行复制到一个新行(上面),然后替换此副本中的某个值。

我已经在Word / VBA中为DOCX完成了这项工作:

Dim tbl As Table
Dim rowNew As Row

Set tbl = ActiveDocument.Tables(1)
Set rowNew = tbl.Rows.Add(tbl.Rows(tbl.Rows.Count))
rowNew.Range.FormattedText = tbl.Rows(tbl.Rows.Count).Range.FormattedText
'~~~> This is required as the above code inserts a blank row in between
tbl.Rows(tbl.Rows.Count - 1).Delete
rowNew.Select
Selection.Find.Execute FindText:="xx*", ReplaceWith:="bar", MatchWildcards:=True
Selection.Collapse

这是否也可以在LibreOffice中使用?到目前为止,我有:

DIM tbl As Variant
DIM row As Variant
tbl =  ThisComponent.getTextTables().getByIndex(0)
row = tbl.getRows().getByIndex(tbl.getRows().getCount()-1)

如何选择并复制整个并在此新行上运行搜索和替换?提示:该行可能包含其他对象,如子表。

1 个答案:

答案 0 :(得分:1)

首先通过移动View Cursor来选择整行。然后使用调度程序复制并粘贴到新行。像这样:

oVC.goRight(3, True)  'Extend the selection.
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
dispatcher.executeDispatch(document, ".uno:Copy", "", 0, Array())
dispatcher.executeDispatch(document, ".uno:InsertRowsAfter", "", 0, Array())
oVC.goDown(1, False)  'Move to the new row.
oVC.goLeft(2, False)  'Move to the first column.
dispatcher.executeDispatch(document, ".uno:Paste", "", 0, Array())