用于 CSV 保存的 openoffice calc 宏

发布于 2024-08-02 05:23:17 字数 944 浏览 4 评论 0原文

给定:我在 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

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

顺便说一句,这种语言叫什么!?

Given: I recorded a simple macro in Openoffice to save my worksheet as a CSV file. Here it is.

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

Problem: I want to add some features to this function. 1. I need to get the current XLS filename so that I can put that at the end of my static path. So file:///path/csv/ is going to always remain the same, and filename.csv will be coming from filename.xls. 2. Well, I'll need to do some regex replacement on that filename-revision01.xls to ultimately get filename.csv.

I can do regex matching well, I'm simply looking for hints on string concatenation, how to get the current filename, and how to write a regex expression within a macro.

BTW, what is this language called!?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撧情箌佬 2024-08-09 05:23:17

这是我想出的解决方案,可帮助我一键导出 CSV(包含导出选项、文件名调整和文件位置)。
我使用的是 Mac,因此文件路径适用于此类操作系统。帮助我做到这一点的信息位于此处

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 is the solution I came up with to help in exporting a CSV (with my export options, filename adjustment, and file location) with one click.
I'm on a Mac, so the file paths will be for such an operating system. The information that helped me do this is 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

Here is a bit of javascript that the above macro relies upon. This file is named ~/Library/Application\ Support/OpenOffice.org/3/user/Scripts/javascript/Tools/Regex.js and is hardcoded and referenced above.

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)

Finally, this post gives details on how to add a toolbar button to run your macro with one click.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文