---
name: Aitable 多维表操作指南
description: JoySpace 多维表 (Aitable) 的完整操作说明
---

# Aitable 多维表操作指南

## 多维表 (Aitable) 概览

多维表的字段类型、Schema 结构说明见 `../aitable.md`，各具体 action 的入参和 JSON 示例见 `../actions.md`；这里只保留核心约束与使用建议：

-   **数据写入方式选择（强制）**：

    -   数据来源是**文件**（用户提供了 CSV/Excel 文件路径，或明确要求从文件导入） → 使用 `aitable.importFromFile`，模型只需传文件路径和目标表信息
    -   数据来源是**对话**（用户在对话中直接给出数据，或模型自行生成的少量数据） → 使用 `aitable.createRecords`
    -   判断依据：只要存在本地文件路径，优先用 `importFromFile`，避免大量数据经过模型上下文

-   **字段/记录写操作前强制阅读**：执行 `aitable.createDbFields`、`aitable.updateDbFields`、`aitable.createRecords`、`aitable.updateRecords` 前，必须先完整读取 `../aitable.md`，确认字段类型、字段 `data` 结构、自动字段限制、记录值格式与过滤/分页约束；未完成该读取时禁止调用上述 action。
-   **Schema 预检强制**：任何涉及字段写入/更新或记录写入/更新的 action（如 `aitable.createDbFields`、`aitable.updateDbFields`、`aitable.createRecords`、`aitable.updateRecords`），都应先通过 `aitable.getSchema` 获取最新 Schema。
-   **自动字段不可写入**：AutoNumber、CreatedBy、CreatedTime、LastModifiedBy、LastModifiedTime、Formula、Lookup 等字段只能读取，禁止在 `createRecords`/`updateRecords` 中写入。
-   **CLI 参数兼容**：多维表 action 统一兼容 CLI 风格别名，例如 `pageId/page_id`、`sheetId/sheet_id`、`name/sheet_name`、`views/sheet_views`、`viewName/view_name`、`viewType/view_type`、`recordIds/record_ids`、`updateRecords/update_records`、`queryFields/query_fields`、`preferId/prefer_id`。
-   **记录写入格式**：`aitable.createRecords` 与 `aitable.updateRecords` 底层提交的 `fieldsValue` 为对象，不做 JSON 字符串序列化。
-   **分页读取**：列表查询统一通过 `aitable.listRecordsByPage`，结合 `page_num`、`page_size`、`filter` 等参数实现，过滤器枚举及完整示例见 `../aitable.md`。
-   **高阶封装建议**：在 TypeScript 中为多维表操作提供封装函数（如 `writeAitableRecordsWithSchemaCheck`），内部完成 Schema 校验和错误提示，Agent 优先调用封装而非直接拼底层参数。

## 多维表 (Aitable)

> 写入数据前必须先执行 schema 预检，详见「操作规则 › 多维表写入前置规则」。

### aitable.getSchema — 获取 Schema

通过 `page_id` 获取多维表结构定义（字段、类型等元数据）。

```json
{
    "action": "aitable.getSchema",
    "page_id": "<page_id>"
}
```

### aitable.createDbFields — 创建字段

> 执行前必须先读取 `../aitable.md`，再根据字段类型定义组装 `fields[].data`。

必填：`page_id`、`sheet_id`、`fields`（数组，元素至少含 `name`、`type`，可选 `data`）

```json
{
    "action": "aitable.createDbFields",
    "page_id": "<page_id>",
    "sheet_id": "<sheet_id>",
    "fields": [
        { "name": "日期", "type": "Date", "data": { "numberFormat": "yyyy/mm/dd" } },
        { "name": "金额", "type": "Currency" }
    ]
}
```

### aitable.updateDbFields — 更新字段

> 执行前必须先读取 `../aitable.md`，并通过 `aitable.getSchema` 确认字段 id、现有类型与目标类型/配置。

`fields` 每个元素的 `id` 为**必填**（用于定位字段）。

```json
{
    "action": "aitable.updateDbFields",
    "page_id": "<page_id>",
    "sheet_id": "<sheet_id>",
    "fields": [{ "id": "fldXXX", "name": "新字段名", "type": "MultiLineText" }]
}
```

### aitable.deleteDbFields — 删除字段

必填：`field_ids`（字段 ID 列表，从 `aitable.getSchema` 获取）

```json
{
    "action": "aitable.deleteDbFields",
    "page_id": "<page_id>",
    "sheet_id": "<sheet_id>",
    "field_ids": ["fldXXX", "fldYYY"]
}
```

### aitable.createDbSheet — 创建数据表

> 仅当用户明确要"多维表/aitable/table"时使用；若为"创建表格/sheet/sheets"，优先走 `create_doc_routing`（见「意图识别与路由优先级」）。
> 若建表后用户已明确提供目标表结构，必须按「操作前置规则 › 多维表建表后字段替换规则」执行：不保留原有默认字段，用新字段结构替换。
> 落地要求：原表首字段名称必须改为新表首字段名称；其他字段按目标结构执行删除、改名与类型变更。

必填：`page_id`、`sheet_name`、`sheet_views`（至少一个，含 `name` + `type`）、`fields`

`view type` 可选：`Grid` / `Kanban` / `Gallery` / `Form` / `Gantt`

```json
{
    "action": "aitable.createDbSheet",
    "page_id": "<page_id>",
    "sheet_name": "项目进展",
    "sheet_views": [{ "name": "表格视图", "type": "Grid" }],
    "fields": [
        { "name": "任务名称", "type": "MultiLineText" },
        { "name": "截止日期", "type": "Date" },
        { "name": "状态", "type": "SingleSelect" }
    ]
}
```

### aitable.deleteDbSheet — 删除数据表

```json
{
    "action": "aitable.deleteDbSheet",
    "page_id": "<page_id>",
    "sheet_id": "<sheet_id>"
}
```

### aitable.createDbView — 创建视图

`view_type` 可选：`Grid` / `Kanban` / `Gallery` / `Form` / `Gantt`

```json
{
    "action": "aitable.createDbView",
    "page_id": "<page_id>",
    "sheet_id": "<sheet_id>",
    "view_name": "<view_name>",
    "view_type": "<view_type>"
}
```

### aitable.listDbView — 列出视图

`sheet_id` / `view_id` 均可选；不传则返回文档下所有视图。

```json
{
    "action": "aitable.listDbView",
    "page_id": "<page_id>",
    "sheet_id": "<sheet_id>"
}
```

### aitable.createRecords — 创建记录

> 执行前必须先读取 `../aitable.md`，再按最新 Schema 校验每个字段的值格式。

单次最多 50 条。必填：`page_id`、`sheet_id`、`records`（每元素含 `fields`）。
可选：`prefer_id`（`true` 时 key 用字段 ID）。

> 自动生成字段（AutoNumber、CreatedBy、CreatedTime、LastModifiedBy、LastModifiedTime、Formula、Lookup）不允许填入。

```json
{
    "action": "aitable.createRecords",
    "page_id": "<page_id>",
    "sheet_id": "<sheet_id>",
    "records": [
        { "fields": { "姓名": "张三", "电话": "18800000001", "日期": "2025/03/01", "金额": "5000" } },
        { "fields": { "姓名": "李四", "单选项": "选项A", "复选框": true } }
    ]
}
```

### aitable.updateRecords — 更新记录

> 执行前必须先读取 `../aitable.md`，再按最新 Schema 校验每个字段的值格式。

每条需含 `id`（行 ID）+ `fields`，单次最多 50 条。

```json
{
    "action": "aitable.updateRecords",
    "page_id": "<page_id>",
    "sheet_id": "<sheet_id>",
    "update_records": [{ "id": "recXXX", "fields": { "状态": "已完成", "进度": "100%" } }]
}
```

### aitable.deleteRecords — 删除记录

```json
{
    "action": "aitable.deleteRecords",
    "page_id": "<page_id>",
    "sheet_id": "<sheet_id>",
    "record_ids": ["recXXX", "recYYY"]
}
```

### aitable.getRecordById — 查询单条记录

```json
{
    "action": "aitable.getRecordById",
    "page_id": "<page_id>",
    "sheet_id": "<sheet_id>",
    "record_id": "<record_id>"
}
```

### aitable.getRecordsByIds — 批量查询记录

```json
{
    "action": "aitable.getRecordsByIds",
    "page_id": "<page_id>",
    "sheet_id": "<sheet_id>",
    "record_ids": ["recXXX", "recYYY"]
}
```

### aitable.listRecordsByPage — 分页查询记录

必填：`page_id`、`sheet_id`、`page_num`（从 1 开始）

可选：`page_size`（1-20，默认 10）、`page_token`（首页传 `"FIRST_PAGE_TOKEN"`）、`query_fields`、`view_id`、`filter`

`filter.criteria[].operator` 可选值：
`Equals` / `NotEqu` / `Greater` / `GreaterEqu` / `Less` / `LessEqu` / `GreaterEquAndLessEqu` / `LessOrGreater` / `BeginWith` / `EndWith` / `Contains` / `NotContains` / `Intersected` / `Empty` / `NotEmpty`

```json
{
    "action": "aitable.listRecordsByPage",
    "page_id": "<page_id>",
    "sheet_id": "<sheet_id>",
    "page_num": 1,
    "page_size": 10,
    "query_fields": ["任务名称", "状态"],
    "filter": {
        "mode": "AND",
        "criteria": [{ "field": "状态", "operator": "Equals", "values": ["进行中"] }]
    }
}
```
