Kreipiamasi į Basic kalbos makrokomandas iš Python kalbos

Galite kreiptis į LibreOffice Basic kalbos makrokomandas iš Python kalbos skriptų ir tada pastebimos galimybės gali būti įgautos, tokios kaip:

LibreOffice Aplikacijų programavimo sąsajos (angl. API) skripto struktūra palaiko vidinį kalbos skripto vykdymą tarp Python ir Basic kalbų arba kitų palaikomų programavimo kalbų. Argumentai gali būti perduoti per iškvietas, numatant, kad jie reprezentuoja pirminius duomenų tipus, kuriuos abi kalbos atpažįsta ir numatant, kad skripto struktūra juos tinkamai konvertuoja.

tip

Yra rekomenduojama turėti supratimą Python kalbos standartiniams moduliams ir LibreOffice API funkcijoms, kad atlikti vidinės kalbos iškvietas iš Python į Basic, JavaScript kalbas arba bet kokią kitą skripto vykdyklę.


warning

Kai Python kalbos skriptas yra vykdomas iš integruotos kūrimo aplinkos (angl. IDE), LibreOffice įdėtinė Basic kalbos vykdyklės gali nebūti. Venkite Python kalbos iškvietų į LibreOffice Basic kalbą tokiame kontekste. Vis dėlto aplinka ir universaliųjų tinklų objektai (angl. UNO) yra pilnai galimi. Eikite Įdiegiamas integruotas IDE Python kalbai kad rastumėte daugiau informacijos.


Atgaunami LibreOffice Basic kalbos skriptai

LibreOffice Basic kalbos makrokomandos gali būti asmeninės, bendros arba įterptos į dokumentus. Norint juos vykdyti, Python kalbos laikas turi būti aprūpintas Basic kalbos makrokomandų vietomis. Realizuojant com.sun.star.script.provider.XScriptProvider sąsają, paleidžiama užklausa vykdomų skriptų:


		 import uno
		 from com.sun.star.script.provider import Xscript
		     
		 def getBasicScript(macro='Main', module='Module1', library='Standard',
		         isEmbedded=False) -> XScript:
		     „Paimkite Basic kalbos skripto objektą prieš iškvietą.“
		     ctx = uno.getComponentContext()
		     smgr = ctx.ServiceManager
		     if isEmbedded:
		         desktop = smgr.createInstanceWithContext('com.sun.star.frame.Desktop', ctx)
		         scriptPro = desktop.CurrentComponent.getScriptProvider()
		         location = "document"
		     else:
		         mspf = smgr.createInstanceWithContext(
		             "com.sun.star.script.provider.MasterScriptProviderFactory", ctx)
		         scriptPro = mspf.createScriptProvider("")
		         location = "application"
		     scriptName = "vnd.sun.star.script:"+library+"."+module+"."+macro+ \
		                  "?language=Basic&location="+location
		     xScript = scriptPro.getScript(scriptName)
		     return xScript
		 

Vykdomi LibreOffice Basic kalbos skriptai

The LibreOffice Software Development Kit (SDK) documentation for com.sun.star.script.provider.XScript interface details the calling convention for inter-language calls. Invocation of functions requires three arrays:

Python Syntax

results = script.invoke((prompt,buttons,title), (), ())

script.invoke((message,), tuple, ())

script.invoke((args), (), results)

Examples of Personal or Shared Scripts

Examples in Input/Output to Screen detail Python to Basic invocation calls. Monitoring Document Events illustrates the usage of *args Python idiom to print a variable number of parameters to Access2Base logging console dialog.

tip

At time of development you can interrupt Python script execution using Xray extension in order to inspect properties and methods of UNO objects. The APSO extension debugger allows object introspection using either Xray either MRI extensions.



	  def xray(myObject):
	  	  script = getBasicScript(library="XrayTool", module="_Main", macro="Xray")
	  	  script.invoke((myObject,), (), ())
	  

Examples of Embedded Scripts in Documents

*argsPython simplified syntax can be used in conjunction with LibreOffice Basic routines that accept a variable number of arguments. Below Print and SUM Python functions call their Basic Print and SUM counterparts, using aforementioned getBasicScript function. Exception handling is not detailed.


	  # -*- coding: utf-8 -*-
	  from __future__ import unicode_literals
	      
	  def Print(*args):
	      """Outputs the specified strings or numeric expressions in a dialog box."""
	      xScript = getBasicScript("Print", "Scripting", embedded=True)
	      xScript.invoke((args), (), ())
	      
	  def SUM(*args):
	      """SUM the specified number expression."""
	      xScript = getBasicScript("SUM", "Scripting", embedded=True)
	      res = xScript.invoke((args), (), ())
	      return res[0]
	      
	  # def getBasicScript()  # see above
	      
	  def playWithArgs():
	      Print("Fun with *args ", -9.81, 297864.681974, 8762E-137)
	      Print(SUM(45, -9.81, 297864.681974))
	      Print(SUM(45, -9.81, 297864.681974, 8762E+137))
	      
	  g_exportedScripts = (playWithArgs,)
	  

The LibreOffice Basic Print and SUM document-based routines accept a variable number of arguments. The Private or Public attributes have no effect. The arguments type checking is skipped for clarity.


	  Option Compatible ' "Standard.Scripting" module
	  Option Explicit
	      
	  Private Sub Print(ParamArray args() As Variant, Optional sep As String = " ")
	      ''' Print item list of variable number '''
	      ' all CStr() convertible args are accepted
	      Dim str As String, i As Integer
	      If UBound(args) >= 0 Then
	          For i = 0 To UBound(args)
	              str = str + Cstr(args(i))+ sep 
	          Next i
	      End If
	      Print str
	      End Sub ' Standard.Scripting.Print()
	      
	  Public Function SUM(ParamArray args() As Variant) As Variant
	      ''' SUM a variable list of numbers '''
	      Dim ndx As Integer
	      If UBound(args) >= 0 Then
	          For ndx = 0 To UBound(args)
	              SUM = SUM + args(ndx)
	          Next ndx
	      End If
	  End Function ' Standard.Scripting.SUM()