From fa92861a200b3716202bb859b6e5b7f116e59501 Mon Sep 17 00:00:00 2001 From: Rafael Lima Date: Tue, 8 Jun 2021 00:43:42 +0200 Subject: Python support for SF_Base service Change-Id: I4175aecd307d70367eea49cc7a8407d2bca60634 Reviewed-on: https://gerrit.libreoffice.org/c/help/+/116707 Tested-by: Jenkins Reviewed-by: Alain Romedenne --- source/text/sbasic/shared/03/sf_base.xhp | 106 ++++++++++++++++++++++++------- 1 file changed, 82 insertions(+), 24 deletions(-) diff --git a/source/text/sbasic/shared/03/sf_base.xhp b/source/text/sbasic/shared/03/sf_base.xhp index e59da8cbd8..23d0fb954d 100644 --- a/source/text/sbasic/shared/03/sf_base.xhp +++ b/source/text/sbasic/shared/03/sf_base.xhp @@ -42,6 +42,7 @@ Refer to the Document service to learn more about methods and properties that can be used to manage %PRODUCTNAME documents.

Service invocation

+ The Base service can be invoked in a variety of ways. The code snippet below uses the method CreateBaseDocument from the UI service to create a new Base file. Note that in all examples the object oDoc is an instance of the Base service. @@ -58,6 +59,19 @@ Dim oDoc As Object Set oDoc = CreateScriptService("SFDocuments.Document", "MyFile.odb") + + The examples above can be translated to Python as follows: + + from scriptforge import CreateScriptService + ui = CreateScriptService("UI") + doc = ui.CreateBaseDocument(r"C:\Documents\MyFile.odb") + + + doc = ui.OpenBaseDocument(r"C:\Documents\MyFile.odb") + + + doc = CreateScriptService("SFDocuments.Document", "MyFile.odb") + The use of the "SFDocuments." substring in the previous example is optional. @@ -95,11 +109,12 @@

FormDocuments

Returns an array with the full names (path/name) of all form documents in the Base document as an zero-based Array of strings. - - oDoc.FormDocuments() As Variant - + + svc.FormDocuments(): str[0..*] + The code snippet below prints the names of all form documents in the current Base document. + Dim oDoc as Object, myForms as Object, formName as String Set oDoc = CreateScriptService("Document", ThisDataBaseDocument) @@ -108,6 +123,14 @@ MsgBox formName Next formName + + + bas = CreateScriptService("Basic") + doc = CreateScriptService("Document", bas.ThisDataBaseDocument) + myForms = oDoc.FormDocuments() + for formName in myForms: + bas.MsgBox(formName) + To learn more about form documents, refer to the Form service help page. @@ -127,20 +150,31 @@ - - oDoc.Forms(FormDocument As String, [Form As String]) As Variant - + + svc.Forms(formdocument: str): str[0..*] + + + svc.Forms(formdocument: str, form: str = ''): svc + + + svc.Forms(formdocument: str, form: int): svc + - FormDocument: The name of a valid form document as a case-sensitive string. - Form: The name or index number of the form stored in the form document. If this argument is absent, the method will return a list with the names of all forms available in the form document. + formdocument: The name of a valid form document as a case-sensitive string. + form: The name or index number of the form stored in the form document. If this argument is absent, the method will return a list with the names of all forms available in the form document.Although it is possible to use index numbers to refer to forms, this is only recommended when there is just one form in the form document. If there are two or more forms, it is preferable to use the form name instead. + The first line of the example below returns a list of all forms in the form document "myFormDocument". The second line returns an instance of the Form service representing the form "myForm". + - ' Returns a list of all forms in the form document "myFormDocument" Dim formsList as Object : formsList = oDoc.Forms("myFormDocument") - ' Returns an instance of the Form service representing the form "myForm" Dim oForm as Object : oForm = oDoc.Forms("myFormDocument", "myForm") + + + formsList = doc.Forms("myFormDocument") + form = doc.Forms("myFormDocument", "myForm") +
@@ -151,22 +185,32 @@

GetDatabase

Returns an instance of the Database service that allows the execution of SQL commands on the database defined and/or stored in the current Base document - - oDoc.GetDatabase([User As String, [Password As String]]) As Object - + + svc.GetDatabase(user: str = '', password: str = ''): svc + - User, Password: Optional login parameters as strings. The default value for both parameters is an empty string "". + user, password: Optional login parameters as strings. The default value for both parameters is an empty string "". + Dim myDoc As Object, myDatabase As Object, ui As Object Set ui = CreateScriptService("UI") - Set myDoc = ui.OpenBaseDocument("myDb.odb") + Set myDoc = ui.OpenBaseDocument("C:\Documents\myDb.odb") ' User and password are supplied below, if needed Set myDatabase = myDoc.GetDatabase() ' ... Run queries, SQL statements, ... myDatabase.CloseDatabase() myDoc.CloseDocument() + + + ui = CreateScriptService("UI") + myDoc = ui.OpenBaseDocument(r"C:\Documents\myDb.odb") + myDatabase = myDoc.GetDatabase() + ' ... Run queries, SQL statements, ... + myDatabase.CloseDatabase() + myDoc.CloseDocument() +
@@ -177,17 +221,23 @@

IsLoaded

Returns True if the specified FormDocument is currently open. - - oDoc.IsLoaded([FormDocument As String]) As Boolean - + + svc.IsLoaded(formdocument: str): bool + - FormDocument: The name of a FormDocument to be checked, as a case-sensitive string. + formdocument: The name of a FormDocument to be checked, as a case-sensitive string. + If Not oDoc.IsLoaded("myFormDocument") Then oDoc.OpenFormDocument("myFormDocument") End If + + + if not doc.IsLoaded("myFormDocument"): + doc.OpenFormDocument("myFormDocument") +
@@ -199,13 +249,14 @@ Opens the specified FormDocument either in normal or in design mode. If the form document is already open, it is activated without changing its mode. The method returns True if the form document could be opened. - - oDoc.OpenFormDocument(FormDocument As String, [DesignMode As Boolean]) As Boolean - + + svc.OpenFormDocument(formdocument: str, designmode: bool = False): bool + - FormDocument: The name of the FormDocument to be opened, as a case-sensitive string. - DesignMode: If this argument is True the FormDocument will be opened in design mode. + formDocument: The name of the FormDocument to be opened, as a case-sensitive string. + designmode: If this argument is True the FormDocument will be opened in design mode. + Most form documents are stored in the root of the Base document and they can be opened simply using their names, as in the example below: oDoc.OpenFormDocument("myFormDocument") @@ -214,6 +265,13 @@ oDoc.OpenFormDocument("myFolder/myFormDocument") + + + doc.OpenFormDocument("myFormDocument") + + + doc.OpenFormDocument("myFolder/myFormDocument") +
-- cgit