Calculated fields and button scripts
Calculated fields and button scripts use the Python programming language. The calculation or script is the implementation of a function whose signature is provided for you.
- B.1. Feldwerte
- B.2. Bezugsdatensätze
- B.3. Prüfen auf leere Werte
- B.4. Benutzung der vollen Pygda-API
B.1. Feldwerte
For instance,
record["name_first"]
B.2. Bezugsdatensätze
For instance,
record.related["location"]
- B.2.1. Single related records
- B.2.2. Datensätze mit mehrfachen Bezügen
B.3. Prüfen auf leere Werte
How you test for empty values depends on the type of field:
- B.3.1. Nicht-Text-Felder
- B.3.2. Textfelder
B.3.1. Nicht-Text-Felder
Non-text fields may be empty, indicating that the user has not entered any value in the field. For instance, Glom does not assume that an empty value in a numeric field should mean 0.
Sie können testen, ob ein Feld leer ist, indem Sie »None« aus Python verwenden. Zum Beispiel:
if(record["contact_id"] == None): return "No Contact" else: return record.related["contacts"]["name_full"]
You might also test whether there are any related records. For instance:
if(record.related["contacts"] == None): return "No Contact" else: return record.related["contacts"]["name_full"]
B.3.2. Textfelder
In Textfeldern sollten Sie prüfen, ob Zeichenketten der Länge Null vorhanden sind. Glom kann nicht zwischen Zeichenketten der Länge Null und fehlenden Zeichenketten unterscheiden, weil dies keinerlei Vorteile hätte. Zum Beispiel:
if(record["name_full"] == ""): return "No Name" else: return record["name_full"]
B.4. Benutzung der vollen Pygda-API
pygda is a python API for the libgda API. The record's connection attribute provides a gda.connection that can be used to access the current database directly. This allows you to run any SQL query, for instance to read data from the database with a SELECT, or to change values in the database with an UPDATE. Note that the connection is already open so you can avoid the difficult work of specifying the connection details.
Das Attribut table_name des Datensatzes stellt auch den Namen der aktuellen Tabelle zur Verfügung.
Dieses Beispiel liest alle Daten aus der aktuellen Tabelle und gibt deren Werte im Terminal aus:
# Use the current database's connection # to get all the data for the current table. # # record.connection is an already-open gda.connection object, # saving us the bother of opening the connection, # or even knowing the name of the database. query = "SELECT * FROM %s" % record.table_name data_model = gda.gda_execute_select_command(record.connection, query) rows = data_model.get_n_rows() columns = data_model.get_n_columns() print " Number of columns: ", columns for i in range(columns): print " column ", i; print " name=", data_model.get_column_title(i) # Find out whether it's the primary key: field = data_model.describe_column(i) if field.get_primary_key(): print " (primary key)" print "\n"; print " Number of rows: ", rows for row_index in range(rows): print " row ", row_index; for col_index in range(columns): print " value=", data_model.get_value_at(col_index, row_index).get() print "\n";