跳到主要内容

数据表追加行数据

使用该接口可以向已有数据表中批量添加行数据。如果需要增加数据列,也可以在 columns 文件保存的 json 中,增加列对应的配置信息(参考创建数据表 API)。

请求

curl -vvv -X POST "$API_HOST/open/api/v1/tablenodes/$table_id/rows/append/" -H "Authorization: $access-token" -d "skipFirstRow=false&autoCreateColumn=true" -F "columns=@$file_path" -F "rows=@$csv_file_path"

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

请求体form-data示例

KeyValue必需备注
skipFirstRowfalse是否跳过CSV首行
autoCreateColumnfalse是否自动创建新的列:列ID为空,并提供创建列需要的参数
columnsread_table_columns.json列配置json文件:可以直接使用创建数据表接口返回的列配置另存为 json 文件(参考 Python 示例) 注:columns 顺序需要与 rows 中列顺序一一对应。对于数据表中已存在的列只要求列 ID ,不存在的列则需要列配置信息。可以支持csv数据中缺少某些列、列的顺序不同、csv有新的列等情况
rowsgd_append.csv行数据csv文件

columns 参数 json格式说明

KeyTypeDescriptionRequired
arraycolumn listY
∟ idstringcolumn IDY, blank if user want to add a new column
∟ namestringColumn NameYes if new column
∟ typestringcolumn type:multiLineText number datetime singleChoice coordinateYes if new column
∟ typeOptionsobjectcolumn options:When column is number, datetime, or singleChoiceN
∟ 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 emptyYES:When singleChoice type
∟ namestringoption nameYES:When singleChoice type
∟ colorstringoption color: use a random color when emptyYES:When singleChoice type

Python 示例

def table_append_data(access_token, table_id, data, columns_file, data_file):
url = '%s/open/api/v1/tablenodes/%d/rows/append/' % (API_HOST, table_id)
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 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')

table = resp['detail']
resp = ma.read_table(access_token, table['id'], '', '')
with open('data/read_table_columns.json', 'w') as f:
json.dump(resp['detail']['columns'], f)

data = {
'skipFirstRow': True,
'autoCreateColumn': False
}

resp = ma.table_append_data(access_token, table['id'],
data,
'data/read_table_columns.json',
'data/gd.append.csv')

响应

响应体

部分主要信息说明

名称类型描述
detailobject数据表信息
∟ idint64数据表ID
∟addedColumnsarray增加的列:当有新列时
∟ idstring列ID
∟ typestring列类型
∟typeOptionsobject列选项
∟ isPrimaryboolean是否为主列
∟appendRowIDsmapCSV行号对应的数据表行ID:更新行数据时需要

响应体示例

{
"code": 0,
"detail": {
"id": 980,
"name": "newName2",
"type": "table",
"order": 441,
"parentID":null,
"projectID": 264,
"subscribed":false,
"published":false,
"roleInherited":true,
"createTime": "2022-04-15T06:44:21.506295Z",
"updateTime": "2022-04-15T06:48:33.243726Z",
"addedColumns": [
{
"id": "625914457cff69c01aed2d71",
"name": "New-Title1",
"type": "multiLineText",
"typeOptions":null,
"isPrimary":true }
],
"appendRowIDs": {
"0": "625931df04b5540935aef0dd",
"1": "625931df04b5540935aef0de"
},
"importErr":null },
"extra":null,
"message": "OK",
"requestID":null
}