web-dev-qa-db-ja.com

libre officeと互換性のあるvbaコードを作成する方法

最近、Windowsからpclinuxosに移行しましたが、気に入っているようです。私が直面している唯一の問題は、デフォルトのスプレッドシートパッケージであるlibreofficeがExcelマクロと互換性がないことです。以下は私が持っているVBAコードです:

Option VBASupport 
Sub DeleteToLeft()
    Selection.SpecialCells(xlBlanks).Delete shift:=xlToLeft
End Sub
Function SinceLastWash()
    Application.Volatile
    WashCount = 0
    WearCount = 0
    CurrentRow = Application.ThisCell.Row
    For i = 3 To 35
        If Range(Cells(CurrentRow, i), Cells(CurrentRow, i)).Value = "a" Then
            WearCount = WearCount + 1
        End If
        If Range(Cells(CurrentRow, i), Cells(CurrentRow, i)).Value = "q" Then
            WashCount = WashCount + 1
            WearCount = 0
        End If
    Next i
    SinceLastWash = WearCount
End Function
Function testhis()
testhis = Application.ThisCell.Row
End Function

Libreofficeと互換性を持たせるためにこのコードを変換する方法はありますか、それともPythonのような新しい言語をすべて習得する必要がありますか?学習pythonは問題になりませんが、多くのvbaコードを持つ作業関連ファイルがExcelにあり、Openを使用できないため、問題の解決策ではありません。職場のオフィス/ libreoffice ...

私は単に、関数SinceLastWashが使用する一部のセルで正しい値を提供し、他のセルではエラーを与えることを追加したいと思います。#NAME?

ありがとう

12
user3125707

UNO APIを使用するには、ドキュメントを操作する部分を翻訳する必要があります。悲しいことに、これはマクロの動作によっては難しい場合があります。基本的なステートメントは直接機能します。ドキュメントを変更しても、通常は変更されません。

Range(Cells(CurrentRow, i), Cells(CurrentRow, i)).Value = "a"

Cellsコマンドは、行と列に基づいて特定のセルを返します。したがって、現在の行が必要です。アクティブなセルを取得するためのいくつかの狂気は次のとおりです。

Sub RetrieveTheActiveCell()
  Dim oOldSelection 'The original selection of cell ranges
  Dim oRanges       'A blank range created by the document
  Dim oActiveCell   'The current active cell
  Dim oConv         'The cell address conversion service
  Dim oDoc
  oDoc = ThisComponent

  REM store the current selection
  oOldSelection = oDoc.CurrentSelection

  REM Create an empty SheetCellRanges service and then select it.
  REM This leaves ONLY the active cell selected.
  oRanges = oDoc.createInstance("com.Sun.star.sheet.SheetCellRanges")
  oDoc.CurrentController.Select(oRanges)

  REM Get the active cell!
  oActiveCell = oDoc.CurrentSelection

  oConv = oDoc.createInstance("com.Sun.star.table.CellAddressConversion")
  oConv.Address = oActiveCell.getCellAddress
  Print oConv.UserInterfaceRepresentation
  print oConv.PersistentRepresentation

  REM Restore the old selection, but lose the previously active cell
  oDoc.CurrentController.Select(oOldSelection)
End Sub

アクティブセルがある場合、セルアドレスを取得し、そこから行を取得します。範囲を使用する必要はありません。1つのセルのみを対象とするため、アクティブなシートを取得してから、シートから特定のセルを取得します。

このようなもの:ThisComponent.getCurrentController()。getActiveSheet()。getCellByPosition(nCol、nRow).getString()= "a"

私はこれが何をするのか理解する気がしません

Selection.SpecialCells(xlBlanks).Delete shift:=xlToLeft
1
Andrew

LibreOfficeのオンラインヘルプファイル: から

いくつかの例外を除き、Microsoft OfficeとLibreOfficeは同じマクロコードを実行できません。 Microsoft OfficeはVBA(Visual Basic for Applications)コードを使用し、LibreOfficeはLibreOffice API(Application Program Interface)環境に基づいたBasicコードを使用します。プログラミング言語は同じですが、オブジェクトとメソッドは異なります。

LibreOffice-PreferencesTools-Options-Load/Save-VBA Propertiesでこの機能を有効にすると、LibreOfficeの最新バージョンはいくつかのExcel Visual Basicスクリプトを実行できます。

実際には、 LibreOffice API で座って機能を書き換える必要があるでしょう。

7
RubberDuck

私が知っている唯一の自動ツールは Business Spreadsheets です(個人的または専門的な経験がなく、サイトと提携していないことに注意してください)。

OpenOfficeに固有のようですが、LibreOfficeでも動作すると思います。

しかし一般的には、ツールは完璧にはほど遠いので、自分でこれを行う方が良いでしょう...

2
djikay

LibreOffice 4.4では、最初のサブルーチンはまったく機能しません(「xl」で始まるすべての変数が原因であると思われます。ThisCellをActiveCellに変更すると、他の2つは完全に機能します。

のではなく

Option VBASupport 

私は使っている

Option VBASupport 1
Option Compatible
2
A. Harding

Selection.SpecialCells(xlBlanks).Delete shift:=xlToLeftは、間違えなければ空白セルを削除します

0
coleman984