Skip to main content

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

KeyValueRequiredNote
projectID264YESProject ID
nameTable nameYES
skipFirstRowfalseNOSkip the first row of csv file
columnsJson file create_table_columns.jsonYESColumn config json:A complex sample file: create-table-columns-complex.json
rowsCsv file create-table-data.csvYSERow csv file

columns parameter

The columns parameter is a json file whose content is a list of column information:

NameTypeDescriptionRequired
arrayAll parametersYES
∟ namestringcolumn nameYES
∟ typestringcolumn type:multiLineText, number, datetime, singleChoice, coordinateYES
∟ typeOptionsobjectcolumn options:When column is number, datetime, or singleChoiceNO
∟ formatstringnumber type:number, percentage, commaNumberYES:When number type
∟ precisionstringprecision: 0-5YES:When number type
∟ dateFormatstringdate format: year/month/day month/day/year detailYES:When datetime type
∟ timeFormatstringtime format: hidden 24-hour-clock 12-hour-clockYES:When datetime type
∟ choicesarrayoption list: created automatically when emptyNO:When singleChoice type
∟ namestringoption nameYES:When singleChoice type
∟ colorstringoption color: use a random color when emptyNO:When singleChoice type
∟ isPrimarybooleanIs the primary column:only zero or one primary column is allowed and the column type should be:multiLineText, number, or datetimeNO

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

NameTypeDescription
detailobjectTable object
∟ idint64Table ID
∟ columnsarrayColumn config file:Can be saved as a json file, use it as the columns parameter when updating or appending data.
∟ idstringColumn ID
∟ typestringColumn type
∟typeOptionsobjectColumn options
∟ isPrimarybooleanIs primary column
∟ rowIDsmapRow 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
}