Files
37duAutomation/libs/aitable/README.md
T

71 lines
3.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# aitable · 钉钉 AI 表格读写库
读写钉钉 AI 表格(notable OpenAPI)的独立 Python 库,附带月报 / 周报数据采集器。
与业务方向解耦:任何插件、脚本、agent 都可以直接引用,也可以单独 `pip install -e`。
## 安装 / 引用
```powershell
# 方式一:可编辑安装(推荐)
.venv\Scripts\python.exe -m pip install -e libs\aitable
# 方式二:直接跑文件,不需要安装
.venv\Scripts\python.exe libs\aitable\aitable\cli.py sheets workplan
```
## 凭证
按顺序读取(**不读已废弃的 `desktop/controlplane/.env`**):
1. 环境变量 `AITABLE_ENV_FILE` 指向的 .env
2. `desktop/controlplane-next/server/.env`(新中心机配置)
3. 仓库根 `.env`(兜底)
用到的键:`DINGTALK_APP_KEY`、`DINGTALK_APP_SECRET`、`AITABLE_OPERATOR_UNIONID`(可选)。
⚠️ notable 接口必须带 `operatorId`(操作人 unionId),且**该操作人必须是目标表格的协作者**;
个人空间文档默认不对应用开放,会返回 `forbidden.operationIllegal / permission deny`。
## 命令行
```powershell
cd d:\work\code\37duAutomation
$py = ".venv\Scripts\python.exe"
$cli = "libs\aitable\aitable\cli.py"
& $py $cli sheets workplan # 列出 sheet
& $py $cli fields workplan --sheet hERWDMS # 字段/类型/选项
& $py $cli list workplan --sheet hERWDMS --month 上线日期=2026-10 --json
& $py $cli summary workplan --sheet hERWDMS --by 状态
& $py $cli update workplan --sheet hERWDMS --record <id> --data '{\"状态\":\"上线\"}' --dry-run
```
base 支持三种写法:别名(`workplan` / `hr`)、baseId、`https://alidocs.dingtalk.com/i/nodes/<baseId>?sheetId=xxx`。
新增常用表在 `aitable/client.py` 的 `BASE_ALIASES` 里登记。
## 月报 / 周报数据采集
```powershell
& $py libs\aitable\aitable\report.py --month 2026-10 # 月报数据
& $py libs\aitable\aitable\report.py --week # 本周
& $py libs\aitable\aitable\report.py --week last --json # 上周,供 agent 组稿
```
输出 Git(提交数/代码量/类型分布/提交明细)+ 需求表(状态分布、本期上线/提测/在研、上线趋势)。
代码量默认排除构建产物与锁文件。写作流程见 `.trae/skills/37du-Monthly-Review/SKILL.md`。
## 自测
```powershell
.venv\Scripts\python.exe libs\aitable\_selftest.py # 离线:值归一/日期/区间/过滤
.venv\Scripts\python.exe libs\aitable\_selftest.py --online # 追加在线冒烟(需凭证与授权)
```
## 坑位
1. 记录列表接口必须是 **POST** `/records/list`(GET 返回 `resource.notFound 'list'`)。
2. 分页靠 `hasMore` + `nextToken`,单页上限 100,只取首页会漏数据。
3. 日期是**毫秒时间戳 + 北京时间**(UTC 16:00 = 次日 00:00),自行换算易整体差一天。
4. 写入:`singleSelect` 传选项名、`multipleSelect` 传数组、`date` 传毫秒、`user` 传 unionId 数组、`autoNumber` 只读。
5. 部分 Windows 机器 `api.dingtalk.com` 的 A 记录解析失败(11001),库内置 DoH 兜底,可用 `AITABLE_DNS_IP` 覆盖。