LibreOfficeの質問コーナーを翻訳

Pythonマクロ(関数)をベースと戻り値からベースに実行する

 

と言う質問を、Google翻訳で翻訳したものです。質問は翻訳していません。

内容

LibreOffice BasicからPythonマクロを動かすというものです。

 

https://ask.libreoffice.org/en/question/52125/execute-a-python-macro-function-from-base-and-return-value-to-base/

Execute a Python Macro (Function) From Base and Return Value to Base
Pythonマクロ(関数)をベースと戻り値からベースに実行する

回答1

The way to do this is to call a macro using the UNO construct getScript(sScriptURI) and then call the invoke method for the object.
これを行う方法は、UNOコンストラクトgetScript(sScriptURI)を使用してマクロを呼び出し、オブジェクトのinvokeメソッドを呼び出すことです。
That is the easy part. The hard part is figuring out the sScriptURI, which is a universal location for the script. There is a paucity of documentation about what, exactly, this is.
それは簡単な部分です。難しい部分は、スクリプトの普遍的な場所であるsScriptURIを理解することです。正確には、これが何であるかについての文書が不足しています。
By trial and error you may find that a sScriptURI for a Basic macro looks like this:
試行錯誤の結果、BasicマクロのsScriptURIは次のようになります。
"vnd.sun.star.script:Standard.Module1.SubName?language=Basic&location=application"
You can see the different elements. By further trial and error, you may find that a Python macro call_me.py with a method of the same name (call_me) saved in the following location:
さまざまな要素を見ることができます。さらに試行錯誤すると、次の場所に保存された同じ名前のメソッドcall_me.pyを持つPythonマクロcall_me.pyが見つかります。
/home/user/.config/libreoffice/{LOversionnumber}/user/Scripts/python/
Is accessible as follows:次のようにアクセス可能です:
"vnd.sun.star.script:call_me.py$call_me?language=Python&location=user"
Thus, if I have the following Python macro at the above location saved as call_me.py:
したがって、上記の場所にある次のPythonマクロをcall_me.pyとして保存した場合、

def call_me(*args ):
    return "call me"
Then if I invoke the following in Basic, it will return to the Basic macro the value set forth in the Python macro ("call me"):
次に、Basicで次のコードを呼び出すと、Pythonマクロ( "call me")に設定されている値がBasicマクロに返されます。
Dim a(0),b(0),c(0) As Variant
scpr = ThisComponent.getScriptProvider
scmod = scpr.getScript("vnd.sun.star.script:call_me.py$call_me?language=Python&location=user")

returnFromPython = scmod.invoke(a,b,c)
MsgBox returnFromPython
location=share also is a valid location, I think, depending on where, exactly, the script is saved.
場所=シェアも有効な場所です、私は、スクリプトが保存されている場所によると思います。
For documentation of what are the arrays a, b, c, see the API reference.
配列a、b、cについては、APIリファレンスを参照してください。
To make your Python macro work, you may need to redefine those.
Pythonマクロを動作させるには、それらを再定義する必要があります。
(if this answers your question, please accept the answer by clicking the check mark () to the left)
(これがあなたの質問に答える場合は、左側のチェックマーク(画像の説明)をクリックして回答を受け入れてください)

This appears to answer the question. The logic looks correct. My knowledge of uno Basic is minimal. It's going to take me a while to parse through this. I hope that others will find your solution useful too. Thanks
これは質問に答えているようです。ロジックが正しいように見えます。私の知識は基本的ではありません。これを解析するには、しばらく時間がかかります。私は、他の人があなたのソリューションも役立つことを願っています。ありがとう

回答2

The "real" answer is the answer posted by Doug above.
「本当の」答えは上記のDougによって投稿された答えです。
The purpose of this response is to show rudimentary code that functions based on the advice provided by Doug.
この応答の目的は、Dougによって提供されたアドバイスに基づいて機能する初歩的なコードを表示することです。
While the code below works, modifications to the basic code structure have tended to fail.
以下のコードは動作しますが、基本的なコード構造の変更は失敗する傾向があります。
As the rudimentary minimally altered code works, these shortcomings can eventually be overcome through further research and trial and error.
初歩的な最小限に変更されたコードが機能するので、これらの欠点は、さらなる研究と試行錯誤を通じて最終的に克服することができます。
For now, this is usable code for conducting further experimentation.
今のところ、これはさらなる実験を行うためのコードです。
Uno Basic function below:   Uno以下の基本機能:
Function dblTestPython(dblValue) As Double
        On Error Goto HandleError1001
        dim a(0), b(0), c(0) as variant
        a(0)= Forms("MainForm").Controls("Field1").value
        scpr = ThisComponent.getScriptProvider
        scmod = scpr.getScript("vnd.sun.star.script:call_me.py$call_me?language=Python&location=user")
        returnFromPython = scmod.invoke(a,b,c)
        print returnFromPython     
        Exit Function
    HandleError1001:
        resume next
End Function
Python script invoked by the Basic function above:
上記のBasic関数によって呼び出されるPythonスクリプト:
import os
def call_me(a):
    z = int(a) * 5
    return z


ーーーーーーーー実施例

Clacで、
REM  *****  BASIC  *****

Function dblTestPython(dblValue) As Double
        On Error Goto HandleError1001
        dim a(0), b(0), c(0) as variant
        oSheet = ThisComponent.Sheets.getByName("Sheet2")
        oCell = oSheet.getCellRangeByName("A2")
        'a(0)= Forms("MainForm").Controls("Field1").value
        a(0)= 10
        scpr = ThisComponent.getScriptProvider
        scmod = scpr.getScript("vnd.sun.star.script:call_me.py$call_me?language=Python&location=user")
        returnFromPython = scmod.invoke(a,b,c)
       'print returnFromPython          'メッセージボックス
        oCell.value =  returnFromPython
        Exit Function
    HandleError1001:
        resume next
End Function

のマクロを作成する。

Pythonのマクロを作成して指定のフォルダに保存する。
call_me.py

import os
def call_me(a):
    z = int(a) * 5
    return z

Clacにフォームボタンを作成します。(これは、何処かのサイトを参考に<そのままです。すみません。>しました。 どこのサイトかもわかりません。)
表示 ー> ツールバー ー> フォームコントロール
フォームコントロールが表示されます。
ボタンコントロールでCalc上にボタンを配置します。
ボタンをクリックした際にマクロを実行するには、ボタンを右クリックして、コントロールを選択し、
の横にある「...」のボタンをクリックし、「マクロ」をクリックします。
マクロセレクターで実行したいマクロを選択して、「OK」とします。
dblTestPythonを選択

これで作業完了ですが、このままではボタンが押せなくなっています。フォームコントロールからデザインモードを「オフ」に設定することでボタンが押せるようになります。

ボタンをクリックするとA2セルに50と表示される。