使用工作表名称将特定工作表转换为csv

问题描述 投票:0回答:1

使用宏:

option compatible
Sub ExportToCsv(URL as String, ParamArray sheetNames() As Variant)
  Dim saveParams(1) as New com.sun.star.beans.PropertyValue
  saveParams(0).Name = "FilterName"
  saveParams(0).Value = "Text - txt - csv (StarCalc)"
  saveParams(1).Name = "FilterOptions"
  saveParams(1).Value = "44,34,0,1,1" ' 44=comma, 34=double-quote

  GlobalScope.BasicLibraries.loadLibrary("Tools")
  URL = ConvertToURL(URL)
  document = StarDesktop.loadComponentFromUrl(URL, "_blank", 0,  Array())

  baseName = Tools.Strings.GetFileNameWithoutExtension(document.GetURL(), "/")
  directory = Tools.Strings.DirectoryNameoutofPath(document.GetURL(), "/")

  sheets = document.Sheets
  sheetCount = sheets.Count
  Dim x as Integer
  Dim requiredSheetIndex as Integer
  For x = 0 to sheetCount -1
    sheet = sheets.getByIndex(x)
    sheet.isVisible = True
    For i = LBound(sheetNames) To UBound(sheetNames)
      If StrComp(sheet.Name, sheetNames(i), vbTextCompare) = 0 Then
        requiredSheetIndex = x
      End If
    Next
  Next

  currentSheet = document.GetCurrentController.GetActiveSheet()
  sheet = sheets(requiredSheetIndex)
  document.GetCurrentController.SetActiveSheet(sheet)
  filename = directory + "/" + baseName + ".csv"
  fileURL = convertToURL(Filename)
  document.StoreToURL(fileURL, saveParams())
  document.close(True)
End Sub

我是VBA的入门者,所以这可能是一个非常简单的问题。

我正在尝试使用宏将指定为第一个参数的文档的特定图纸转换为CSV。该工作表是根据变量args的第一个匹配项进行匹配的,该变量也将传递给宏。

目前没有任何故障,但它没有选择正确的纸张。第一张纸总是被捡起来。

该错误最有可能在此嵌套循环结构中,但无法弄清楚它是什么。

  sheets = document.Sheets
  sheetCount = sheets.Count
  Dim x as Integer
  Dim requiredSheetIndex as Integer
  For x = 0 to sheetCount -1
    sheet = sheets.getByIndex(x)
    sheet.isVisible = True
    For i = LBound(sheetNames) To UBound(sheetNames)
      If StrComp(sheet.Name, sheetNames(i), vbTextCompare) = 0 Then
        requiredSheetIndex = x
      End If
    Next
  Next
vba libreoffice
1个回答
0
投票

问题是传递参数时的空格

soffice --headless "macro:///ExportToCsv.Module.ExportToCsv("path_to_file.xlsx", Data)"

我在工作表名称和已解析的参数中都添加了Trim,并且可以正常工作。

© www.soinside.com 2019 - 2024. All rights reserved.