Contingut

# Servei SFDatabases.Datasheet

The Datasheet service allows to visualize the contents of database tables as well as the results of queries and SQL statements using Base's Data View. Additionally, this service allows to:

- Add custom menus to the data view.

- Access values in specific positions of the data view.

- Position the cursor in a specific cell of the data view.

## Invocació del servei

Before using the Datasheet service the ScriptForge library needs to be loaded or imported:

• Per a carregar macros BASIC cal fer servir la biblioteca ScriptForge mitjançant aquesta expressió:
GlobalScope.BasicLibraries.loadLibrary("ScriptForge")
• Els scripts Python requereixen una importació del mòdul scriptforge:
from scriptforge import CreateScriptService

##### En Basic

The Datasheet service can be invoked in two different ways depending on whether the database file is open.

The example below considers that the database file is open, hence the UI service can be used to retrieve the document and the OpenTable method from the Database service is used to get a Datasheet service instance.

        Dim ui As Object, oBase As Object, oSheet As Object
        Set ui = CreateScriptService("UI")
        ' Object oBase is an instance of the Base service
        Set oBase = ui.GetDocument("C:\Documents\myDB.odb")
        ' Object oSheet is an instance of the Datasheet service
        Set oSheet = oBase.OpenTable("Customers")

In the example above it is also possible to use the method OpenQuery from the Base service to get a Datasheet instance.

To invoke the Datasheet service when the database file is not open, use the OpenTable, OpenQuery or OpenSql methods from the Database service. The example below uses the OpenTable method to open an existing table in the database file:

        Dim oDatabase As Object, oSheet As Object
        ' Object oDatabase is an instance of the Database service
        Set oDatabase = CreateScriptService("Database", "C:\Documents\myDB.odb")
        ' Object oSheet is an instance of the Datasheet service
        Set oSheet = oDatabase.OpenTable("Customers")

##### En Python

The examples above can be translated to Python as follows:

        from scriptforge import CreateScriptService
        ui = CreateScriptService("UI")
        base_doc = ui.GetDocument(r"C:\Documents\MyFile.odb")
        sheet = base_doc.OpenTable("Customers")

        database = CreateScriptService("Database", r"C:\Documents\myDB.odb")
        sheet = database.OpenTable("Customers")

## Propietats

The following properties are available in the Datasheet service:

Nom | Només de lectura | Tipus | Descripció
--- | --- | --- | ---
ColumnHeaders | Sí | Matriu de cadenes | Returns an Array with the names of column headers in the datasheet.
CurrentColumn | Sí | String | Retorna el nom de la columna seleccionada actualment.
CurrentRow | Sí | Integer | Returns the number of the currently selected row, starting at 1.
DatabaseFileName | Sí | String | Returns the file name of the Base file in FSO.FileNaming format.
Filter | No | String | Specifies a filter to be applied to the datasheet expressed as the WHERE clause of a SQL query without the WHERE keyword. If an empty string is specified then the active Filter is removed.
LastRow | Sí | Integer | Retorna el nombre de files al full de dades.
OrderBy | No | String | Specifies the order in which records are shown expressed as the ORDER BY clause of a SQL query without the ORDER BY keyword. If an empty string is specified then the active OrderBy is removed.
ParentDatabase | Sí | Objecte | Returns the Database service instance to which the datasheet belongs.
Source | Sí | String | Returns a String that represents the data source, which can be a SQL statement, a table name or a query name.
SourceType | Sí | String | Returns the type of the data source, which can be one of the following values: "SQL", "TABLE" or "QUERY".
XComponent | Sí | Objecte UNO | Returns the com.sun.star.lang.XComponent UNO object that represents the datasheet.
XControlModel | Sí | Objecte UNO | Returns the com.sun.star.awt.XControl UNO object that represents the datasheet.
XTabControllerModel | Sí | Objecte UNO | Returns the com.sun.star.awt.XTabControllerModel UNO object that represents the datasheet.

## Mètodes

Llista de mètodes del servei Datasheet
Activate CloseDatasheet CreateMenu | GetText GetValue GoToCell | RemoveMenu Toolbars

## Activate

Brings to front the data view window referred to by the Datasheet instance.

### Sintaxi:

svc.Activate()

### Exemple:

##### En Basic

          oSheet.Activate()

##### En Python

          sheet.Activate()

## CloseDatasheet

Closes the data view window referred to by the Datasheet instance.

### Sintaxi:

svc.CloseDatasheet()

### Exemple:

##### En Basic

          oSheet.CloseDatasheet()

##### En Python

          sheet.CloseDatasheet()

## CreateMenu

Creates a new menu entry in the data view window and returns a SFWidgets.Menu service instance, with which menu items can be programmatically added.

Menus added using the CreateMenu method are lost as soon as the data view window is closed.

### Sintaxi:

svc.CreateMenu(menuheader: str, opt before: any, opt submenuchar: str): obj

### Paràmetres:

menuheader: The name of the new menu.

before: This argument can be either the name of an existing menu entry before which the new menu will be placed or a number expressing the position of the new menu. If this argument is left blank the new menu is placed as the last entry.

submenuchar: The delimiter used in menu trees (Default = ">")

### Exemple:

##### En Basic

          Dim oMenu As Object
          Set oMenu = oSheet.CreateMenu("My Menu", Before := "Data")
          With oMenu
              .AddItem("Item 1", Command := ".uno:About")
              ' ...
              .Dispose()
          End With

##### En Python

          menu = sheet.CreateMenu("My Menu", before="Data")
          menu.AddItem("Item 1", command=".uno:About")
          # ...
          menu.Dispose()

Read the Menu service help page to learn more about how to create menu and submenu entries and associate commands.

## GetText

Returns the text in a given column of the current row.

This method does not change the position of the cursor in the data view window.

### Sintaxi:

svc.GetText(column: any): str

### Paràmetres:

column: The name of the column as a String or the column position (starting at 1). If a position greater than the number of columns is given, the last column is returned.

### Exemple:

##### En Basic

          oSheet.GetText("FirstName")

##### En Python

          sheet.GetText("FirstName")

## GetValue

Returns the value in a given column of the current row as a valid Basic type.

The types that can be returned are: String, Integer, Long, Single, Double, Date and Null.

Binary types are returned as a Long value indicating the length of the binary field.

An Empty value is returned if the required value could not be retrieved.

This method does not change the position of the cursor in the data view window.

### Sintaxi:

svc.GetValue(column: any): any

### Paràmetres:

column: The name of the column as a String or the column position (starting at 1). If a position greater than the number of columns is given, the last column is returned.

### Exemple:

##### En Basic

          oSheet.GetValue("Address")

##### En Python

          sheet.GetValue("Address")

## GoToCell

Moves the cursor to the specified row and column.

### Sintaxi:

svc.GoToCell(opt row: int, opt column: any): bool

### Paràmetres:

row: The row number as a numeric value starting at 1. If the requested row exceeds the number of existing rows, the cursor is moved to the last row. If this argument is not specified, then the row is not changed.

column: The name of the column as a String or the column position (starting at 1). If the requested column exceeds the number of existing columns, the cursor is moved to the last column. If this argument is not specified, then the column is not changed.

### Exemple:

##### En Basic

          ' Moves the cursor to the column "LastName" in row 4
          oSheet.GoToCell(4, "LastName")
          ' Moves the cursor to the third column of the current row
          oSheet.GoToCell(Column := 3)
          ' Moves cursor one row down leaving it in the same column
          oSheet.GoToCell(Row := oSheet.CurrentRow + 1)
          ' Moves to the last column of the last row
          Dim LastColumn As Integer : LastColumn = UBound(oSheet.ColumnHeaders) + 1
          oSheet.GoToCell(oSheet.LastRow, LastColumn)

##### En Python

          sheet.GoToCell(4, "LastName")
          sheet.GoToCell(column=3)
          sheet.GoToCell(row=sheet.CurrentRow + 1)
          sheet.GoToCell(sheet.LastRow, len(sheet.ColumnHeaders))

## RemoveMenu

Removes a menu entry from the data view by its name.

This method can remove menus that belong to the standard user interface as well as menus that were programmatically added with the CreateMenu method. The removal of standard menus is not permanent and they will reappear after the window is closed and reopened.

### Sintaxi:

svc.RemoveMenu(menuheader: str): bool

### Paràmetres:

menuheader: The case-sensitive name of the menu to be removed. The name must not include the tilde ("~") character.

### Exemple:

##### En Basic

          oSheet.RemoveMenu("Data")

##### En Python

          sheet.RemoveMenu("Data")

### Toolbars

This method returns either a list of the available toolbar names in the actual document or an instance SFWidgets.Toolbar service.

### Sintaxi:

svc.Toolbars(opt ToolbarName: str): uno
svc.Toolbars(): str[0..]

### Paràmetres:

ToolbarName: The usual name of one of the available toolbars.

### Exemple:

##### En Basic

        Dim oToolbar As Object
        Set oToolbar = oDoc.Toolbars("myToolbar")

##### En Python

        a_list = doc.Toolbars()

Totes les rutines o identificadors bàsics de ScriptForge que tenen el prefix de guió baix "_" estan reservats per a ús intern. No estan pensats per utilitzar-los en macros de Basic o scripts de Python.

Temes relacionats

SFDatabases.Database service

ScriptForge.UI service