跳到主要内容

使用数据创建数据表

使用该接口可以创建数据表并导入

请求

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"

请求使用表单的方式提交,表单中包含两个文件字段: coulumns 和 rows,分别代表的「数据表的列定义」和「数据行」。详情参考下面表格

请求体form-data示例

KeyValue必需备注
projectID264项目 ID
name数据表名称
skipFirstRowfalse是否跳过CSV首行
columnsJson 文件 create_table_columns.json列配置json文件:内容参考示例文件。一个更复杂的示例文件: create-table-columns-complex.json
rowsCsv 文件 create-table-data.csv行数据csv文件,内容参考示例文件。

columns 参数 json格式说明

名称类型描述必需
array所有列配置数组
∟ namestring列名称
∟ typestring列类型:multiLineText number datetime singleChoice coordinate
∟ typeOptionsobject列选项:number datetime singleChoice 时需要
∟ formatstring数值格式:number percentage commaNumber是:列类型为number时
∟ precisionstring数值精度: 0-5是:列类型为number时
∟ dateFormatstring日期格式: year/month/day month/day/year detail是:列类型为datatime时
∟ timeFormatstring时间格式: hidden 24-hour-clock 12-hour-clock是:列类型为datatime时
∟ choicesarray选项列表: 为空时自动创建否:列类型为singleChoice时
∟ namestring选项名: 当选项名和单元格值相等时使用该选项是:列类型为singleChoice时
∟ colorstring选项颜色: 为空时随机设置颜色否:列类型为singleChoice时
∟ isPrimaryboolean是否为主列:只能设置一个,且列类型为:multiLineText number datetime

Python 示例

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')

响应

响应体

部分主要信息说明

名称类型描述
detailobject新建数据表
∟ idint64数据表ID
∟ columnsarray列配置数据:可保存为json文件,追加数据时需要和更新时需要
∟ idstring列ID
∟ typestring列类型
∟typeOptionsobject列选项
∟ isPrimaryboolean是否为主列
∟ rowIDsmapCSV行号对应的数据表行ID:更新行数据时需要

响应体示例

{
"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
}