Usługa SFDocuments.Base

Usługa Base zapewnia szereg metod i właściwości ułatwiających zarządzanie i obsługę dokumentów LibreOffice Base.

Ta usługa jest ściśle powiązana z usługą Document, która zapewnia ogólne metody obsługi dokumentów LibreOffice, w tym dokumentów Base. W związku z tym usługa Base rozszerza usługę Document i udostępnia dodatkowe metody, specyficzne dla dokumentów Base, umożliwiające użytkownikom:

tip

Zapoznaj się z usługą Document, aby dowiedzieć się więcej o metodach i właściwościach, których można używać do zarządzania dokumentami LibreOffice .


Wywoływanie usługi

Przed użyciem usługi Base należy załadować lub zaimportować bibliotekę ScriptForge:

note

• Podstawowe makra wymagają załadowania biblioteki ScriptForge przy użyciu następującej instrukcji:
GlobalScope.BasicLibraries.loadLibrary("ScriptForge")

• Skrypty Pythona wymagają importu z modułu scriptforge:
from scriptforge import CreateScriptService


W języku Basic

Usługę Base można wywołać na różne sposoby. Poniższy fragment kodu używa metody CreateBaseDocument z usługi UI do utworzenia nowego pliku Base.

Zauważ, że we wszystkich przykładach obiekt oDoc jest instancją usługi Base.


    Dim ui As Object, oDoc As Object
    Set ui = CreateScriptService("UI")
    Set oDoc = ui.CreateBaseDocument("C:\Documents\MyFile.odb")
  

Usługę Base można również utworzyć podczas otwierania istniejącego pliku Base, jak pokazano poniżej:


    Set oDoc = ui.OpenBaseDocument("C:\Documents\MyFile.odb")
  

Jeśli dokument Base jest już otwarty, możliwe jest bezpośrednie utworzenie instancji usługi Base:


    Dim oDoc As Object
    Set oDoc = CreateScriptService("SFDocuments.Document", "MyFile.odb")
  
W języku Python

Powyższe przykłady można przetłumaczyć na język Python w następujący sposób:


    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")
  
note

Użycie podłańcucha "SFDocuments." w poprzednim przykładzie jest opcjonalne.


Lista metod w usłudze Base

CloseFormDocument
FormDocuments
Forms
GetDatabase

IsLoaded
OpenFormDocument
OpenQuery
OpenTable

PrintOut
SetPrinter



CloseFormDocument

Zamyka podany dokument formularza. Zwraca True, jeśli zamknięcie się powiodło.

Składnia:

svc.CloseFormDocument(formdocument: str): bool

Parametry:

formdocument: Nazwa FormDocument do zamknięcia, jako ciąg z rozróżnianiem wielkości liter.

Przykład:

Jeśli dokumenty formularza są zorganizowane w folderach, konieczne jest podanie nazwy folderu w celu określenia dokumentu formularza, który ma zostać otwarty, jak pokazano w poniższych przykładach:

W języku Basic

    oDoc.CloseFormDocument("Folder1/myFormDocument")
  
W języku Python

    doc.CloseFormDocument('Folder1/myFormDocument')
  

FormDocuments

Zwraca tablicę z pełnymi nazwami (ścieżka/nazwa) wszystkich dokumentów formularza w dokumencie Base jako tablicę ciągów liczoną od zera.

Składnia:

svc.FormDocuments(): str[0..*]

Przykład:

Poniższy fragment kodu wypisuje nazwy wszystkich dokumentów formularzy w bieżącym dokumencie Base.

W języku Basic

    Dim oDoc as Object, myForms as Object, formName as String
    Set oDoc = CreateScriptService("Document", ThisDataBaseDocument)
    Set myForms = oDoc.FormDocuments()
    For Each formName In myForms
        MsgBox formName
    Next formName
  
W języku Python

    bas = CreateScriptService("Basic")
    doc = CreateScriptService("Document", bas.ThisDataBaseDocument)
    myForms = doc.FormDocuments()
    for formName in myForms:
        bas.MsgBox(formName)
  
tip

Aby dowiedzieć się więcej o dokumentach formularzy, zapoznaj się ze stroną pomocy usługi Form.


Forms

W zależności od podanych parametrów ta metoda zwróci:

Składnia:

svc.Forms(formdocument: str): str[0..*]

svc.Forms(formdocument: str, form: str = ''): svc

svc.Forms(formdocument: str, form: int): svc

Parametry:

formdocument: nazwa prawidłowego dokumentu formularza jako ciąg z rozróżnianą wielkością liter.

form: nazwa lub numer indeksu formularza przechowywanego w dokumencie formularza. W przypadku braku tego argumentu metoda zwróci listę z nazwami wszystkich formularzy dostępnych w dokumencie formularza.

note

Chociaż możliwe jest odwoływanie się do formularzy za pomocą numerów indeksów, jest to zalecane tylko wtedy, gdy w dokumencie formularza znajduje się tylko jeden formularz. Jeśli istnieją dwa lub więcej formularzy, lepiej jest zamiast tego użyć nazwy formularza.


Przykład:

Pierwszy wiersz poniższego przykładu zwraca listę wszystkich formularzy w dokumencie formularza „myFormDocument”. Drugi wiersz zwraca instancję usługi Form reprezentującą formularz „myForm”.

W języku Basic

    Dim formsList as Object : formsList = oDoc.Forms("myFormDocument")
    Dim oForm as Object : oForm = oDoc.Forms("myFormDocument", "myForm")
  
W języku Python

    formsList = doc.Forms("myFormDocument")
    form = doc.Forms("myFormDocument", "myForm")
  

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

Składnia:

svc.GetDatabase(user: str = '', password: str = ''): svc

Parametry:

user, password: Optional login parameters as strings. The default value for both parameters is an empty string "".

Przykład:

W języku Basic

    Dim myDoc As Object, myDatabase As Object, ui As Object
    Set ui = CreateScriptService("UI")
    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()
  
W języku Python

    ui = CreateScriptService("UI")
    myDoc = ui.OpenBaseDocument(r"C:\Documents\myDb.odb")
    myDatabase = myDoc.GetDatabase()
    #   ... Run queries, SQL statements, ...
    myDatabase.CloseDatabase()
    myDoc.CloseDocument()
  

IsLoaded

Returns True if the specified FormDocument is currently open.

Składnia:

svc.IsLoaded(formdocument: str): bool

Parametry:

formdocument: The name of a FormDocument to be checked, as a case-sensitive string.

Przykład:

W języku Basic

    If Not oDoc.IsLoaded("myFormDocument") Then
        oDoc.OpenFormDocument("myFormDocument")
    End If
  
W języku Python

    if not doc.IsLoaded("myFormDocument"):
        doc.OpenFormDocument("myFormDocument")
  

OpenFormDocument

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.

Składnia:

svc.OpenFormDocument(formdocument: str, designmode: bool = False): bool

Parametry:

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.

Przykład:

W języku Basic

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")
  

If form documents are organized in folders, it becomes necessary to include the folder name to specify the form document to be opened, as illustrated in the following example:


    oDoc.OpenFormDocument("myFolder/myFormDocument")
  
W języku Python

    doc.OpenFormDocument("myFormDocument")
  

    doc.OpenFormDocument("myFolder/myFormDocument")
  

OpenQuery

Opens the Data View window of the specified query and returns an instance of the Datasheet service.

The query can be opened either in normal or design mode.

If the query is already open, its Data View window will be made active.

note

Closing the Base document will cause the Data View window to be closed as well.


Składnia:

svc.OpenQuery(queryname: str, designmode: bool = False): obj

Parametry:

queryname: The name of an existing query as a case-sensitive String.

designmode: If this argument is set to True the query is opened in design mode. Otherwise it is opened in normal mode (Default = False).

Przykład:

W języku Basic

      oDoc.OpenQuery("MyQuery", DesignMode := True)
    
W języku Python

      doc.OpenQuery("MyQuery", designmode=True)
    

OpenTable

Opens the Data View window of the specified table and returns an instance of the Datasheet service.

The table can be opened either in normal or design mode.

If the table is already open, its Data View window will be made active.

note

Closing the Base document will cause the Data View window to be closed as well.


Składnia:

svc.OpenTable(tablename: str, designmode: bool = False): obj

Parametry:

tablename: The name of an existing table as a case-sensitive String.

designmode: If this argument is set to True the table is opened in design mode. Otherwise it is opened in normal mode (Default = False).

Przykład:

W języku Basic

      oDoc.OpenTable("MyTable", DesignMode = False)
    
W języku Python

      doc.OpenTable("MyTable", designmode=False)
    

PrintOut

This method sends the content of the given form document to a default printer or a printer defined by the SetPrinter() method.

Returns True if the document was successfully printed.

Składnia:

svc.PrintOut(opt formdocument: str, pages: str = "", copies: num = 1): bool

Parametry:

formdocument: A valid document form name as a case-sensitive string. The form document must be open. It is activated by the method.

pages: The pages to print as a string, like in the user interface. Example: "1-4;10;15-18". Default is all pages.

copies: The number of copies. Default is 1.

Przykład:

W języku Basic

    If oDoc.PrintOut("myForm", "1-4;10;15-18", Copies := 2) Then
        ' ...
    End If
  
W języku Python

    if doc.PrintOut('myForm', copies=3, pages='45-88'):
        # ...
  

SetPrinter

Define the printer options for a form document. The form document must be open.

Returns True when successful.

Składnia:

svc.SetPrinter(opt formdocument: str, opt printer: str, opt orientation: str, paperformat: str): bool

Parametry:

formdocument: A valid document form name as a case-sensitive string.

printer: The name of the printer queue where to print to. When absent, the default printer is set.

orientation: Either PORTRAIT or LANDSCAPE. When absent, left unchanged with respect to the printer settings.

paperformat: Specifies the paper format as a string value that can be either A3, A4, A5, LETTER, LEGAL or TABLOID. Left unchanged when absent.

Przykład:

W języku Basic

    oDoc.SetPrinter("myForm", Orientation := "PORTRAIT")
  
W języku Python

    doc.SetPrinter('myForm', paperformat='TABLOID')
  
warning

Wszystkie podstawowe procedury lub identyfikatory ScriptForge poprzedzone znakiem podkreślenia „_” są zarezerwowane do użytku wewnętrznego. Nie należy ich używać w makrach Basic ani skryptach Pythona.