使用 sheet 名称将特定 sheet 转换为 csv

Converting a specific sheet using sheet name to csv

使用的宏:

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 的完全初学者,所以这可能是一个非常简单的问题。

我正在尝试使用宏将指定为第一个参数的文档的特定 sheet 转换为 CSV。 sheet 根据也传递给宏的可变参数的第一个匹配项进行匹配。

现在没有失败,但没有选择正确的 sheet。第一个 sheet 总是被拾起。

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

  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

问题是 space 传递参数时

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

我在 sheet 名称和解析的参数中添加了一个 Trim,它起作用了。