Create a table using existing data
Create a new table and import data into the table.
Request
curl -vvv -X POST "$API_HOST/open/api/v1/tablenodes/$table_id/rows/append/" -H "Authorization: $access-token" -d "skipFirstRow=false&projectID=$project_id&name=$name" -F "columns=@$file_path" -F "rows=@$csv_file_path"
The request is submitted using a form, which contains two document fields: columns and rows, representing "table column definition" and "rows" respectively. Refer to the following table for details
Request form-data example
Key | Value | Required | Note |
---|---|---|---|
projectID | 264 | YES | Project ID |
name | Table name | YES | |
skipFirstRow | false | NO | Skip the first row of csv file |
columns | Json file create_table_columns.json | YES | Column config json:A complex sample file: create-table-columns-complex.json |
rows | Csv file create-table-data.csv | YSE | Row csv file |
columns parameter
The columns
parameter is a json file whose content is a list of column information:
Name | Type | Description | Required |
---|---|---|---|
array | All parameters | YES | |
∟ name | string | column name | YES |
∟ type | string | column type:multiLineText, number, datetime, singleChoice, coordinate | YES |
∟ typeOptions | object | column options:When column is number, datetime, or singleChoice | NO |
∟ format | string | number type:number, percentage, commaNumber | YES:When number type |
∟ precision | string | precision: 0-5 | YES:When number type |
∟ dateFormat | string | date format: year/month/day month/day/year detail | YES:When datetime type |
∟ timeFormat | string | time format: hidden 24-hour-clock 12-hour-clock | YES:When datetime type |
∟ choices | array | option list: created automatically when empty | NO:When singleChoice type |
∟ name | string | option name | YES:When singleChoice type |
∟ color | string | option color: use a random color when empty | NO:When singleChoice type |
∟ isPrimary | boolean | Is the primary column:only zero or one primary column is allowed and the column type should be:multiLineText, number, or datetime | NO |
Python example
def create_table(access_token, data, columns_file, data_file):
url = '%s/open/api/v1/tablenodes/import/' % API_HOST
files = {'rows': open(data_file, 'rb'),
'columns': open(columns_file, 'rb')}
headers = {'Authorization': access_token}
req = requests.post(url, data=data, headers=headers, files=files)
return req.json()
# create project
resp = ma.create_project(access_token, 'proj-name', 'proj-desc')
time.sleep(3) # wait for backend jobs finish
ws_resp = ma.get_workspace_detail(access_token, workspace)
project = ws_resp['detail']['detail']['projects'][0]
# create table using csv
data = {
'projectID': project['id'],
'name': 'test-csv-upload',
'skipFirstRow': True
}
resp = ma.create_table(access_token, data,
'data/create_table_columns.json',
'data/gd.csv')
Response
Response body
Name | Type | Description |
---|---|---|
detail | object | Table object |
∟ id | int64 | Table ID |
∟ columns | array | Column config file:Can be saved as a json file, use it as the columns parameter when updating or appending data. |
∟ id | string | Column ID |
∟ type | string | Column type |
∟typeOptions | object | Column options |
∟ isPrimary | boolean | Is primary column |
∟ rowIDs | map | Row IDs of the data corresponding the ID in the Maptable table, the ID can be used for updating data |
Response body example
{
"code": 0,
"detail": {
"id": 981,
"name": "test",
"type": "table",
"order": 442,
"parentID":null,
"projectID": 264,
"subscribed":false,
"published":false,
"roleInherited":true,
"createTime": "2022-04-15T07:51:38.699027Z",
"updateTime": "2022-04-15T07:51:38.699027Z",
"columns": [
{
"id": "6259240a04b5540935aef03d",
"name": "Title",
"type": "multiLineText",
"typeOptions":null,
"isPrimary":true },
{
"id": "6259240a04b5540935aef03e",
"name": "Region",
"type": "singleChoice",
"typeOptions": {
"choices": [
{
"color": "redLight",
"id": "61320ba43bcee9f811eef77d",
"name": "Gongye District"
},
{
"color": "salmonLight",
"id": "61320ba43bcee9f811eef77e",
"name": "Xiacheng District"
},
{
"color": "greyLight",
"id": "61320ba43bcee9f811eef786",
"name": "Linan District"
}
]
},
"isPrimary":false },
{
"id": "6259240a04b5540935aef03f",
"name": "Location",
"type": "multiLineText",
"typeOptions":null,
"isPrimary":false },
{
"id": "6259240a04b5540935aef040",
"name": "FAR",
"type": "number",
"typeOptions": {
"format": "number",
"precision": 1
},
"isPrimary":false },
{
"id": "6259240a04b5540935aef041",
"name": "Geo",
"type": "coordinate",
"typeOptions":null,
"isPrimary":false }
],
"rowIDs": {
"0": "6259240a04b5540935aef04c",
"1": "6259240a04b5540935aef04d"
},
"importErr":null },
"extra":null,
"message": "OK",
"requestID":null
}