数据表追加行数据
使用该接口可以向已有数据表中批量添加行数据。如果需要增加数据列,也可以在 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示例
Key | Value | 必需 | 备注 |
---|---|---|---|
skipFirstRow | false | 否 | 是否跳过CSV首行 |
autoCreateColumn | false | 否 | 是否自动创建新的列:列ID为空,并提供创建列需要的参数 |
columns | read_table_columns.json | 是 | 列配置json文件:可以直接使用创建数据表接口返回的列配置另存为 json 文件(参考 Python 示例) 注:columns 顺序需要与 rows 中列顺序一一对应。对于数据表中已存在的列只要求列 ID ,不存在的列则需要列配置信息。可以支持csv数据中缺少某些列、列的顺序不同、csv有新的列等情况 |
rows | gd_append.csv | 是 | 行数据csv文件 |
columns 参数 json格式说明
Key | Type | Description | Required |
---|---|---|---|
array | column list | Y | |
∟ id | string | column ID | Y, blank if user want to add a new column |
∟ name | string | Column Name | Yes if new column |
∟ type | string | column type:multiLineText number datetime singleChoice coordinate | Yes if new column |
∟ typeOptions | object | column options:When column is number, datetime, or singleChoice | N |
∟ 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 | YES:When singleChoice type |
∟ name | string | option name | YES:When singleChoice type |
∟ color | string | option color: use a random color when empty | YES: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')
响应
响应体
部分主要信息说明
名称 | 类型 | 描述 |
---|---|---|
detail | object | 数据表信息 |
∟ id | int64 | 数据表ID |
∟addedColumns | array | 增加的列:当有新列时 |
∟ id | string | 列ID |
∟ type | string | 列类型 |
∟typeOptions | object | 列选项 |
∟ isPrimary | boolean | 是否为主列 |
∟appendRowIDs | map | CSV行号对应的数据表行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
}