0


我在 Calc 中创建了以下宏,它遍历指定的行,搜索预定义的标题,然后删除它们的整个列(包括标题),代码有效,相关列确实被删除,但是

我发现了一个漏洞。如果文件中缺少预定义列表中的任何标题,我会收到一条错误消息“找不到搜索键”,然后删除其他一些不相关的列(例如,代码正在搜索“Text1”,“ Text2"、"Text3" 标题,但是如果没有找到 "Text2",则会弹出错误消息并删除一些不相关的列)。

如果此文件中不存在某个值,则应该有某种验证检查会自动跳到循环中的下一个值。

条件:

  • 标题行始终为 7。
  • 列表项 相关列可以出现在提到的行中的任何位置。
  • 如果找到了标题,则应删除整个列(
    之后不应保留空白列),如果缺少某些标题,则代码应自动转到下一个
    搜索值。

我找到了一些 Excel 代码示例,它们应该可以满足我的需要,但是它们在我的情况下无法使用,因为 Calc/Excel 代码略有不同。

任何帮助是极大的赞赏。

sub DeleteSystemFields
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 switch to active cell A7

dim args1(0) as new com.sun.star.beans.PropertyValue
args1(0).Name = "ToPoint"
args1(0).Value = "$A$7"

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

rem search conditions

dim args4(17) as new com.sun.star.beans.PropertyValue
args4(0).Name = "SearchItem.StyleFamily"
args4(0).Value = 2
args4(1).Name = "SearchItem.CellType"
args4(1).Value = 0
args4(2).Name = "SearchItem.RowDirection"
args4(2).Value = true
args4(3).Name = "SearchItem.AllTables"
args4(3).Value = false
args4(4).Name = "SearchItem.Backward"
args4(4).Value = false
args4(5).Name = "SearchItem.Pattern"
args4(5).Value = false
args4(6).Name = "SearchItem.Content"
args4(6).Value = false
args4(7).Name = "SearchItem.AsianOptions"
args4(7).Value = false
args4(8).Name = "SearchItem.AlgorithmType"
args4(8).Value = 1
args4(9).Name = "SearchItem.SearchFlags"
args4(9).Value = 65536

rem search criteria parameters - corresponds to args4(10) in the next section

args4(10).Name = "SearchItem.SearchString"
args4(10).Value = ""
args4(11).Name = "SearchItem.Locale"
args4(11).Value = 255
args4(12).Name = "SearchItem.ChangedChars"
args4(12).Value = 2
args4(13).Name = "SearchItem.DeletedChars"
args4(13).Value = 2
args4(14).Name = "SearchItem.InsertedChars"
args4(14).Value = 2
args4(15).Name = "SearchItem.TransliterateFlags"
args4(15).Value = 1280
args4(16).Name = "SearchItem.Command"
args4(16).Value = 3
args4(17).Name = "Quiet"
args4(17).Value = true

rem search values start

args4(10).Name = "SearchItem.SearchString"

args4(10).Value = "Text1" 

dispatcher.executeDispatch(document, ".uno:ExecuteSearch", "", 0, args4())

dispatcher.executeDispatch(document, ".uno:DeleteColumns", "", 0, Array())


args4(10).Name = "SearchItem.SearchString"

args4(10).Value = "Text2"

dispatcher.executeDispatch(document, ".uno:ExecuteSearch", "", 0, args4())

dispatcher.executeDispatch(document, ".uno:DeleteColumns", "", 0, Array())

args4(10).Name = "SearchItem.SearchString"


args4(10).Value = "Text3" 

dispatcher.executeDispatch(document, ".uno:ExecuteSearch", "", 0, args4())

dispatcher.executeDispatch(document, ".uno:DeleteColumns", "", 0, Array())


end sub
4

1 回答 1

0

不确定,但试试这个

found = dispatcher.executeDispatch(document, ".uno:ExecuteSearch", "", 0, args4())

If found.result = True  then
    dispatcher.executeDispatch(document, ".uno:DeleteColumns", "", 0, Array())
End If

跟进

尝试这个

args4(10).Name = "SearchItem.SearchString"
args4(10).Value = "Text1"

found = dispatcher.executeDispatch(document, ".uno:ExecuteSearch", "", 0, args4())

If found.result = True  then

    dispatcher.executeDispatch(document, ".uno:ExecuteSearch", "", 0, args4())

    dispatcher.executeDispatch(document, ".uno:DeleteColumns", "", 0, Array())

End If
于 2012-05-27T18:12:39.800 回答