如何为libreoffice的所有xls / xlsx文件运行一个宏

时间:2018-10-03 09:04:46

标签: libreoffice libreoffice-calc libreoffice-basic

是否可以为所有xls / xlsx文件运行一个宏,如果可以,如何运行。下面显示的宏将excel文件缩放为适合单个页面的大小,这是必需的,因为列数为19,并且需要使用lo cli将其转换为pdf。

Libre Office版本:6.0.6

宏已在libreoffice中记录,可以在下面看到:

REM  *****  BASIC  *****

sub Main
rem ----------------------------------------------------------------------
rem define variables
dim document   as object
dim dispatcher as object
rem ----------------------------------------------------------------------
vrem get access to the document
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:PageFormatDialog", "", 0, Array())
end sub

请告诉我有关测试的任何信息。

1 个答案:

答案 0 :(得分:0)

从Libreoffice的一位开发人员那里得到了答案,它的工作原理很吸引人,因此请在此处共享。可以找到答案的链接here

迈克的解决方案

首先:您录制的宏将无法工作:它不会应用更改,只会打开一个对话框。请始终测试录制的宏:-)

您可以改用以下宏:

Sub FitToPage
  Dim document As Object, pageStyles As Object
  document   = ThisComponent
  pageStyles = document.StyleFamilies.getByName("PageStyles")
  For i = 0 To document.Sheets.Count - 1
    Dim sheet As Object, style As Object
    sheet = document.Sheets(i)
    style = pageStyles.getByName(sheet.PageStyle)
    style.ScaleToPagesX = 1
  Next
  On Error Resume Next
  document.storeSelf(Array())
  document.close(true)
End Sub

它在当前文档上运行,并在设置比例后保存(覆盖!)并关闭文档。

要从命令行使用此宏,您需要将其保存到某些库中,例如标准。在下面的示例中,我使用Module1进行存储。

您可以在单个文档中使用此宏,如下所示:

'path/to/LibreOffice/program/soffice' path/to/excelfile.ext macro:///Standard.Module1.FitToPage

要在多个文档上使用它,您需要循环执行(提及将多个文件名作为单个soffice调用的参数,例如在Linux上使用*的shell glob,将无法正常工作-实际上,它将仅运行最后一个文档的宏,保持其他文档的打开状态和未修改的状态)。 Windows的循环可能是这样的:

for %f in (*.xls) do start /wait "" "C:\Program Files\LibreOffice\program\soffice.exe" "%f" macro:///Standard.Module1.FitToPage