【问题标题】:libreOffice command line macro does not return output or errorlibreOffice 命令行宏不返回输出或错误
【发布时间】:2019-12-31 11:37:48
【问题描述】:

我想在 cli 中使用 libreOffice 宏将 ods 文档转换为 xls,但似乎我没有任何输出,并且 libreOffice 没有产生任何错误(至少不可见)。

这是我运行的命令:

libreoffice --norestore --invisible --headless --nologo "macro:///Standard.Convert.ConvertTo(\"MYFILE.ods\",\"OUTPUT.xls\",\"MS Excel 97\",\"\")"

输出未创建,我没有收到任何错误(我也尝试使用 2>&1 但没有效果)。

libreoffice --version
LibreOffice 6.0.7.3 00m0(Build:3)

我尝试了从 4.2 到 6.0 的不同版本,但没有什么不同,我认为这不是版本问题。

我也知道--convert-to 选项,但我需要为我的输出命名,并且这个宏在我正在调试的软件中随处使用,所以我继续使用它会更容易。

我做错了什么吗?

【问题讨论】:

    标签: command-line libreoffice libreoffice-basic


    【解决方案1】:

    Libre office作为服务运行是一种非常强大的系统运行方式。

    Universal Network ObjectsUNO / Basic 项目是这种能力的支柱。

    unoconv 答案是可靠且有效的,但是我猜您可能希望将 libre 作为服务运行。

    你问了几个问题。

    1. 如何在 Libre 或 Open Office 中调试宏

    1. 您能否运行宏并将文件另存为 xls(使用过滤器选项 MS Excel 97 / xls 格式)?

    这是命令

    soffice --headless "macro:///Library1.Module4.OdsToXls(~/Desktop/Test/test1.ods)"

    首先我从 libre 内部运行宏,然后将参数添加到过程中。

    您可以使用 MsgBox调试

    我已将调试信息留给您查看。

    sub OdsToXls(AbsoluteFileName as String)
    
    REM IN SpreadSheetPath is the FULL PATH and file
    REM AbsoluteFileName="/home/<user>/Desktop/Test/test1.ods"
    REM AbsoluteFileName="~/Desktop/Test/test1.ods"
    
    REM SaveAsFile 
    rem ----------------------------------------------------------------------
    rem define variables
    Dim Doc As Object  
    Dim Dummy()
    dim dispatcher as object
    dim PartURL as string
    dim args1(1) as new com.sun.star.beans.PropertyValue
    dim BaseFilename as string
    rem used for testing
    rem AbsoluteFileName="~/Desktop/Test/test1.ods"
    PartURL="file:///"
    
    rem ----------------------------------------------------------------------
    rem get access to the document
    
    If (Not GlobalScope.BasicLibraries.isLibraryLoaded("Tools")) Then
      GlobalScope.BasicLibraries.LoadLibrary("Tools")
    End If
    
    rem content of an opened window can be replaced with the help of the frame parameter and SearchFlags:
    SearchFlags = com.sun.star.frame.FrameSearchFlag.CREATE + com.sun.star.frame.FrameSearchFlag.ALL  
    
    
    Url=ConvertToUrl(PartURL+AbsoluteFileName) 
    
    rem MsgBox(Url)
    
    Doc = StarDesktop.loadComponentFromURL(Url, "MyFrame", _SearchFlags, Dummy)
    
    
    
    BaseFilename = Tools.Strings.GetFileNameWithoutExtension(Url)
    rem msgBox(BaseFilename)
    
    rem ----------------------------------------------------------------------
    
    args1(0).Name = "URL"
    args1(0).Value = BaseFilename+"."+"xls"
    args1(1).Name = "FilterName"
    args1(1).Value = "MS Excel 97"
    
    Doc.StoreAsURL( args1(0).Value , args1())
    
    Doc.close(true)
    
    end sub
    

    您可以通过在目录上使用 while 循环并具有输出目录来添加此内容。

    如果您有兴趣扫描目录并提取所有文件并处理它们,那么这可能会对您有所帮助Stackoverflow Question: Saving specific Excel sheet as .csv

    如果您有任何问题,请随时提出。

    参考Filter_Options

    一切顺利

    【讨论】:

    • 其实是宏安装问题(我在docker工作)。所以是的,当您的宏已经被考虑在内时,MsgBox 会非常有用。但是如果没有安装宏并且您尝试使用它,您将不会收到任何错误。很奇怪不是吗?
    • 嗨 PopHip 宏应该在 /home//.config/libreoffice/4/user/basic/Standard/
    • 是的,它是自定义安装,所以路径有点不同,但宏文件在这里。问题出在文件 script.xlb 中没有正确引用宏!
    • 很高兴您发现了 PopHip 的错误
    • 嗨 PopHip 你能帮助其他用户找到解决方案吗? meta.stackexchange.com/questions/5234/…
    【解决方案2】:

    我会安装 unoconv 并从命令行运行:

    unoconv -o OUTPUT.xls MYFILE.ods
    

    它非常快,至少比无头启动 LibreOffice 快,据我所知。

    【讨论】:

    • 在 Ubuntu 下安装 unoconv 非常简单:只需运行 sudo apt install unoconv
    猜你喜欢
    • 2021-02-26
    • 2011-12-11
    • 1970-01-01
    • 1970-01-01
    • 2018-12-04
    • 2015-10-18
    • 2013-02-19
    • 2020-04-04
    • 1970-01-01
    相关资源
    最近更新 更多