LibreOffice 如何获取文件选择器的多个文件选择数据

Posted

技术标签:

【中文标题】LibreOffice 如何获取文件选择器的多个文件选择数据【英文标题】:LibreOffice how to get filepicker multiple file selection data 【发布时间】:2015-09-05 11:01:53 【问题描述】:

在 LibreOffice 4.2 中,我试图打开文件选择器并选择多个文件(我成功了),然后将这些文件的名称(和路径)传输到变量(或数组,没关系)。

虽然我可以打开文件选择器并选择多个文件,但我只能获取一个文件(第一个)的文件名和路径。而且我找不到其他方法。

我正在使用以下代码:

Sub TakeFile()
   Dim FileNames(0 to 100) as String
   FileNames() = fImportLocalFile()
   Msgbox FileNames
End Sub

Function fImportLocalFile() 'as String
' FJCC: Can't define the function as returning a String because now it returns an array
   'this function opens a system file open dialog box and allows the
   '   user to pick a file from thier computer to open into the
   '   document for processing

   'stores the filedialog object
   Dim oFileDialog as Object
   'stores the returned result of the activation of the dialog box
   Dim iAccept as Integer
   'stores the returned file name/path from the file dialog box
   Dim sPath as String
   'stores the set default path for the dialog box
   Dim InitPath as String

   'stores the types of files allowed in the filedialog
   Dim sFilterNames as String

   'setup the filters for the types of files to allow in the dialog
   sFilterNames = "*.csv; *.txt; *.odt; *.ods; *.xls; *.xlt; *.xlsx"

   'create the dialog box as a Windows File Dialog
   oFileDialog = CreateUnoService("com.sun.star.ui.dialogs.FilePicker")

   'set the filters for the dialog
   oFileDialog.AppendFilter("Supported files", sFilterNames)

   'set the path as blank
   InitPath = ""

   'add the default path to the dialog
   oFileDialog.setDisplayDirectory(InitPath)

   'setup the dialog to allow multiple files to be selected
   oFileDialog.setMultiSelectionMode(True)

   'set iAccept as the execution of the dialog
   iAccept = oFileDialog.Execute()

   'execute and test if dialog works
   If iAccept = 1 Then
      'set sPath as the chosen file from the dialog
      'sPath = oFileDialog.Files(0)
      FileArray = oFileDialog.getFiles() 'added by FJCC
      'set the function as sPath for returning to the previous sub
      fImportLocalFile = FileArray  'modified by FJCC
   'end current if statement
   End If

End Function

【问题讨论】:

【参考方案1】:

您的错误是您将所选文件的数组分配给函数名称本身!选择其他名称。

这适用于我的 LO 5.0.0.5

SUB TakeFile()
'   Dim FileNames(0 to 100) as String
'   Dont limit yourself!

   FileNames = fImportLocalFile()

   path = FileNames(0)
    FOR i = 1 TO Ubound(FileNames)
       print path + FileNames(i)
    Next 

End Sub

在函数内:

path = FileArray(0)
FOR i = 1 TO Ubound(FileArray)
   print path + FileArray(i)
Next 

fImportLocalFile    = FileArray

【讨论】:

【参考方案2】:

有一个接口XFilePicker2“扩展文件选择器接口以解决一些设计问题。”这个接口有一个方法getSelectedFiles

见https://www.openoffice.org/api/docs/common/ref/com/sun/star/ui/dialogs/XFilePicker2.html

使用此方法代替XFilePicker.getFiles

以下应该有效:

Sub TakeFile()

   Dim FileNames() as String
   FileNames = fImportLocalFile()

   Msgbox Join(FileNames, Chr(10))

End Sub

Function fImportLocalFile() as Variant

   Dim oFileDialog as Object
   Dim iAccept as Integer
   Dim sPath as String
   Dim InitPath as String

   Dim sFilterNames as String

   sFilterNames = "*.csv; *.txt; *.odt; *.ods; *.xls; *.xlt; *.xlsx"

   oFileDialog = CreateUnoService("com.sun.star.ui.dialogs.FilePicker")

   oFileDialog.AppendFilter("Supported files", sFilterNames)

   InitPath = ""

   oFileDialog.setDisplayDirectory(InitPath)

   oFileDialog.setMultiSelectionMode(True)

   iAccept = oFileDialog.Execute()

   If iAccept = 1 Then
      fImportLocalFile = oFileDialog.getSelectedFiles() 
   Else
      fImportLocalFile = Array()
   End If

End Function

【讨论】:

以上是关于LibreOffice 如何获取文件选择器的多个文件选择数据的主要内容,如果未能解决你的问题,请参考以下文章

Ubuntu 14.x/LibreOffice 4.2 - 如何打印到 PS 文件(不是 PDF)

LibreOffice:如何制作单选按钮组

如何使用获取的变量作为材料 UI 日期选择器的初始状态?

如何获取选择器的输入参数类型?

如何将 jQuery 选择器的表达式作为文本获取?

SK了解如何获取 LinearSVC 分类器的决策概率