Basic structure¶
The JavaScript script runs directly in the current browser and is suitable for simple data processing. JavaScript does not require any authentication.
Two JavaScript APIs in SeaTable
SeaTable offers two different ways to use JavaScript with SeaTable. You can executing a JS-Script directly in SeaTable and there is a JavaScript Client API. The functions are similar but not identical. If you want to use a JavaScript in SeaTable, stay at this section, otherwise switch to the Client APIs.
Interact with your base¶
JavaScript provide pre-defined objects, corresponding methods of this objects and utilities. On top, normal JavaScript operations like console.log
or calculations are working as usual.
Let's beginn¶
Let's make this concrete and let us look at some basic examples.
- Jump to your seatable webinterface
- Create a new Script of the type
Javascript
- Copy the following code
- Run the script
You will learn from these examples, that it is quite easy to read, output and even manipulate the data of a base inside SeaTable with the predefined objects and the corresponding methods.
-
base
is the predefined-object provided by SeaTable containing all bases of a base.getTables()
is the function to get all bases from the objectbase
.
-
output
is also a predefined-object provided by SeaTable.length
is just a normal operation in JavaScript.
As you can see, the script will output the number of tables in your base. Read the comments behind the two lines to get more information about the difference between a predefined object, a method of this object and an ordinary JavaScript function.
const table = base.getTableByName('Table1');
const columns = base.getColumns(table);
for (var i=0; i<columns.length; i++) {
output.text(columns[i].name + " (" + columns[i].type + ")")
}
This will return all column names and the column types.
const table = base.getTableByName('Table1'); // (1)!
const view = base.getViewByName(table, 'Default View'); // (2)!
const rows = base.getRows(table, view); // (3)!
for (var i=0; i<rows.length; i++) { // (4)!
const row = rows[i];
output.text('>>> new row <<<')
output.text(row);
output.text(row['Name']);
}
- get the content of the table
Table1
. - get the content of the view
Default View
. - get the row of this view
Default View
in this tableTable1
. - iterate over all rows and print them
This time, we will get the Name
of all columns in the table Table1
and the view Default View
.
const table = base.getTableByName('Table1'); // (1)!
const newRow = { // (2)!
'Name': 'Hugo',
'Age': 3,
};
try {
const row = base.addRow(table, newRow);
output.text(`New row added with _id: ${row._id}`);
} catch (error) {
output.text(`Error adding row: ${error}`);
}
- Replace
Table1
with your actual table name - Update column names
Name
andAge
and the values you would like to add.
// Get the table
const table = base.getTableByName('Table1');
// Specify the row_id you want to update
const rowId = 'KDW9PZMkTOuwtx71pmAMxA';
// Define the updates you want to make
// Replace 'Name' with the actual column name you want to update
// and 'NewValue' with the new value you want to set
const updates = {
'Name': 'NewValue'
};
base.updateRow(table, rowId, updates);