用于CSV保存的openoffice calc宏

时间:2009-08-23 17:53:00

标签: vba openoffice.org

鉴于:我在Openoffice中录制了一个简单的宏,将我的工作表保存为CSV文件。这里是。

sub toCSV
dim document   as object
dim dispatcher as object
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
dim args1(2) as new com.sun.star.beans.PropertyValue
args1(0).Name = "URL"
args1(0).Value = "file:///path/csv/filename.csv"
args1(1).Name = "FilterName"
args1(1).Value = "Text - txt - csv (StarCalc)"
args1(2).Name = "FilterOptions"
args1(2).Value = "59,34,76,1"
dispatcher.executeDispatch(document, ".uno:SaveAs", "", 0, args1())
end sub

问题:我想为此功能添加一些功能。 1.我需要获取当前的XLS文件名,以便我可以将其放在静态路径的末尾。所以file:/// path / csv /将始终保持不变, filename.csv 将来自 filename.xls 。 2.好吧,我需要在 filename-revision01.xls 上更换一些正则表达式,以最终得到 filename.csv

我可以很好地进行正则表达式匹配,我只是寻找关于字符串连接的提示,如何获取当前文件名,以及如何在宏中编写正则表达式。

BTW,这种语言叫做什么!?

1 个答案:

答案 0 :(得分:1)

这是我提出的解决方案,只需点击一下即可帮助导出CSV(包括导出选项,文件名调整和文件位置)。
我在Mac上,所以文件路径将用于这样的操作系统。帮助我这样做的信息是here

REM  *****  BASIC  *****
sub toCSV
dim document   as object
dim dispatcher as object
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
FileURL = ThisComponent.URL
oMasterScriptProviderFactory = createUnoService("com.sun.star.script.provider.MasterScriptProviderFactory")
oMasterScriptProvider = oMasterScriptProviderFactory.createScriptProvider("")
oScriptReplace = oMasterScriptProvider.getScript("vnd.sun.star.script:Tools.Regex.js?language=JavaScript&location=user")
sReturn = RegExpReplace(oScriptReplace, FileURL, "(.*)/(\w*-\w*)(-revision\d*)+\.xls", "i", "$2.csv")
dim args1(2) as new com.sun.star.beans.PropertyValue
args1(0).Name = "URL"
args1(0).Value = "file:///Users/joe/Documents/mydocuments/trunk/my%20projects/dictionary/verbsXLS/proofed/csv/" + sReturn
args1(1).Name = "FilterName"
args1(1).Value = "Text - txt - csv (StarCalc)"
args1(2).Name = "FilterOptions"
args1(2).Value = "59,34,76,1"
dispatcher.executeDispatch(document, ".uno:SaveAs", "", 0, args1())
end sub

function RegExpReplace(oScriptReplace as Object, sSource as String, sRegExp as String, sGlobUpcase as String, sReplace as String) as String
RegExpReplace = oScriptReplace.invoke(Array(sSource, sRegExp, sGlobUpcase, sReplace ), Array(), Array())
end function

以上宏依赖的是一些javascript。此文件名为〜/ Library / Application \ Support / OpenOffice.org / 3 / user / Scripts / javascript / Tools / Regex.js,并且是硬编码的,并在上面引用。

sSource     = String(ARGUMENTS[0])
sRegExp     = String(ARGUMENTS[1])
sGlobUpcase = (ARGUMENTS[2])
sReplace    = String(ARGUMENTS[3])
myRe   = new RegExp(sRegExp, sGlobUpcase)
ret = sSource.replace(myRe, sReplace)

最后,this post提供了有关如何通过单击添加工具栏按钮来运行宏的详细信息。