LibreOffice Calc Macro 不插入新工作表

LibreOffice Calc Macro 不插入新工作表

我有一个 LibreOffice Calc 电子表格,并使用录制宏函数将当前行复制到新工作表并从列转置为行。

我将宏分配给快捷键Ctrl++ ShiftA除了没有插入新工作表并且粘贴发生在当前工作表中之外,一切正常,这是灾难性的。

主表当前行突出显示的示例:

IG 主页

手动复制、插入表格、粘贴转置和格式化的结果示例:

IG 视图

当前宏无法插入新工作表并粘贴到当前工作表中:

sub ViewRowInNewSheet
rem ----------------------------------------------------------------------
rem define variables
dim document   as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

rem ----------------------------------------------------------------------
dim args1(0) as new com.sun.star.beans.PropertyValue
args1(0).Name = "Sel"
args1(0).Value = false

dispatcher.executeDispatch(document, ".uno:GoToStartOfRow", "", 0, args1())

rem ----------------------------------------------------------------------
dim args2(0) as new com.sun.star.beans.PropertyValue
args2(0).Name = "Sel"
args2(0).Value = true

dispatcher.executeDispatch(document, ".uno:GoToEndOfRow", "", 0, args2())

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:Copy", "", 0, Array())

rem ----------------------------------------------------------------------
'dim args4(1) as new com.sun.star.beans.PropertyValue
'args4(0).Name = "Name"
'args4(0).Value = "View"
'args4(1).Name = "Index"
'args4(1).Value = 4

'dispatcher.executeDispatch(document, ".uno:Insert", "", 0, args4())
'
' Replacement Code from Ask Ubuntu March 6/2017 :
oSheets = ThisComponent.Sheets
oSheets.insertNewByName("View",1)

rem ----------------------------------------------------------------------
dim args5(5) as new com.sun.star.beans.PropertyValue
args5(0).Name = "Flags"
args5(0).Value = "SVD"
args5(1).Name = "FormulaCommand"
args5(1).Value = 0
args5(2).Name = "SkipEmptyCells"
args5(2).Value = false
args5(3).Name = "Transpose"
args5(3).Value = true
args5(4).Name = "AsLink"
args5(4).Value = false
args5(5).Name = "MoveMode"
args5(5).Value = 4

dispatcher.executeDispatch(document, ".uno:InsertContents", "", 0, args5())

rem ----------------------------------------------------------------------
dim args6(0) as new com.sun.star.beans.PropertyValue
args6(0).Name = "ToPoint"
args6(0).Value = "$A$1"

dispatcher.executeDispatch(document, ".uno:GoToCell", "", 0, args6())

rem ----------------------------------------------------------------------
dim args7(2) as new com.sun.star.beans.PropertyValue
args7(0).Name = "FontHeight.Height"
args7(0).Value = 15
args7(1).Name = "FontHeight.Prop"
args7(1).Value = 100
args7(2).Name = "FontHeight.Diff"
args7(2).Value = 0

dispatcher.executeDispatch(document, ".uno:FontHeight", "", 0, args7())

rem ----------------------------------------------------------------------
dim args8(0) as new com.sun.star.beans.PropertyValue
args8(0).Name = "By"
args8(0).Value = 1

dispatcher.executeDispatch(document, ".uno:GoDownSel", "", 0, args8())

rem ----------------------------------------------------------------------
dim args9(0) as new com.sun.star.beans.PropertyValue
args9(0).Name = "By"
args9(0).Value = 1

dispatcher.executeDispatch(document, ".uno:GoDownSel", "", 0, args9())

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:DeleteRows", "", 0, Array())

rem ----------------------------------------------------------------------
dim args11(0) as new com.sun.star.beans.PropertyValue
args11(0).Name = "ToPoint"
args11(0).Value = "$A$3"

dispatcher.executeDispatch(document, ".uno:GoToCell", "", 0, args11())

rem ----------------------------------------------------------------------
dim args12(0) as new com.sun.star.beans.PropertyValue
args12(0).Name = "Sel"
args12(0).Value = true

dispatcher.executeDispatch(document, ".uno:GoToEndOfData", "", 0, args12())

rem ----------------------------------------------------------------------
dim args13(4) as new com.sun.star.beans.PropertyValue
args13(0).Name = "CharFontName.StyleName"
args13(0).Value = ""
args13(1).Name = "CharFontName.Pitch"
args13(1).Value = 1
args13(2).Name = "CharFontName.CharSet"
args13(2).Value = -1
args13(3).Name = "CharFontName.Family"
args13(3).Value = 0
args13(4).Name = "CharFontName.FamilyName"
args13(4).Value = "Andale Mono"

dispatcher.executeDispatch(document, ".uno:CharFontName", "", 0, args13())

rem ----------------------------------------------------------------------
dim args14(0) as new com.sun.star.beans.PropertyValue
args14(0).Name = "ToPoint"
args14(0).Value = "$A$3"

dispatcher.executeDispatch(document, ".uno:GoToCell", "", 0, args14())


end sub

笔记:插入新工作表时,默认是在当前工作表之前插入。我选择了插入新工作表的选项当前工作表。不过我认为这不会产生任何影响。

几周前,LibreOffice 自动更新至版本 5.1.6.2。如果能解决问题,我很乐意降级,但这是一个新的宏,升级之前我从未尝试过。

答案1

在我使用 LibreOffice 5.3.0.3 时,此代码有效。但是,在使用 Apache OpenOffice 4.1.3 时,此代码无效。因此,这似乎是版本相关的问题。

UNO API 调用通常比调度程序调用更好,正如此处类似问题所建议的那样:

https://forum.openoffice.org/en/forum/viewtopic.php?f=25&t=26612

因此,如果尚未创建“视图”表:

oSheets = ThisComponent.Sheets
oSheets.insertNewByName("View",1)

相关内容