LibreOffice Macro mac 通过下划线错误查找和替换空格
Posted
技术标签:
【中文标题】LibreOffice Macro mac 通过下划线错误查找和替换空格【英文标题】:LibreOffice Macro mac find and replace spaces by underscore bug 【发布时间】:2015-03-29 05:09:02 【问题描述】:我想在 libreoffice 中创建一个快捷方式来用下划线替换空格。
我录制了一个宏
我执行了一个简单的查找和替换。
但每次我尝试运行宏 libreoffice 时都会退出 ;(
这是代码
REM ***** BASIC *****
sub replacespaces
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(17) as new com.sun.star.beans.PropertyValue
args1(0).Name = "SearchItem.StyleFamily"
args1(0).Value = 2
args1(1).Name = "SearchItem.CellType"
args1(1).Value = 0
args1(2).Name = "SearchItem.RowDirection"
args1(2).Value = true
args1(3).Name = "SearchItem.AllTables"
args1(3).Value = false
args1(4).Name = "SearchItem.Backward"
args1(4).Value = false
args1(5).Name = "SearchItem.Pattern"
args1(5).Value = false
args1(6).Name = "SearchItem.Content"
args1(6).Value = false
args1(7).Name = "SearchItem.AsianOptions"
args1(7).Value = false
args1(8).Name = "SearchItem.AlgorithmType"
args1(8).Value = 1
args1(9).Name = "SearchItem.SearchFlags"
args1(9).Value = 71680
args1(10).Name = "SearchItem.SearchString"
args1(10).Value = " "
args1(11).Name = "SearchItem.ReplaceString"
args1(11).Value = "_"
args1(12).Name = "SearchItem.Locale"
args1(12).Value = 255
args1(13).Name = "SearchItem.ChangedChars"
args1(13).Value = 2
args1(14).Name = "SearchItem.DeletedChars"
args1(14).Value = 2
args1(15).Name = "SearchItem.InsertedChars"
args1(15).Value = 2
args1(16).Name = "SearchItem.TransliterateFlags"
args1(16).Value = 1280
args1(17).Name = "SearchItem.Command"
args1(17).Value = 3
dispatcher.executeDispatch(document, ".uno:ExecuteSearch", "", 0, args1())
rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:SearchResultsDialog", "", 0, Array())
end sub
谁能告诉我怎么了?
【问题讨论】:
【参考方案1】:openoffice 和 libreoffice 的宏记录器并没有 Microsoft Office 的帮助那么大。我建议不要使用它。而不是记录宏,而是使用 XRAY (https://wiki.documentfoundation.org/Macros) 之类的工具来检查您使用 API 的方法找到的对象。首先检查thisComponent。这样做,使用 Calc,您会发现一个模型具有多个工作表模型,每个模型实现一个接口 XReplaceable (http://api.libreoffice.org/docs/idl/ref/interfacecom_1_1sun_1_1star_1_1util_1_1XReplaceable.html)。
例子:
Sub findAndReplace()
oModel = thisComponent ' at first examine thisComponent
' xray oModel
oSpreadSheet = oModel.getCurrentController().getActiveSheet()
' oSpreadSheet = oModel.getSheets().getByIndex(0)
' xray oSpreadSheet
xReplaceDescr = oSpreadSheet.createReplaceDescriptor()
' xray xReplaceDescr
xReplaceDescr.SearchString = " "
xReplaceDescr.ReplaceString = "_"
lFound = oSpreadSheet.replaceAll(xReplaceDescr)
' xray lFound
MsgBox lFound & " replacements done."
End Sub
【讨论】:
好的,谢谢它帮了我很多;) 对于选定的单元格,我将第 4 行替换为 oSpreadSheet = oModel.getCurrentSelection() 要添加正则表达式,我在 xReplaceDescr.SearchString 之前添加了 xReplaceDescr.SearchRegularExpression = TRUE以上是关于LibreOffice Macro mac 通过下划线错误查找和替换空格的主要内容,如果未能解决你的问题,请参考以下文章
LibreOffice Macro总是显示#NULL!重新打开文件后
LibreOffice SDK:如何在 mac 中安装和配置 LibreOffice SDK
在终端中使用 LibreOffice 从 Mac 命令将 HTML 转换为 PDF?