ai_member_xiaobian/tmp/test_rich_text.py
2026-05-16 08:10:01 +08:00

89 lines
2.3 KiB
Python

"""
Test script: write a single cell with rich text to feishu sheet
"""
import json, requests, os
CRED_DIR = "/root/.openclaw/credentials"
BASE_URL = "https://open.feishu.cn/open-apis"
def get_token(name="xiaobian"):
config_path = os.path.join(CRED_DIR, name, "config.json")
with open(config_path) as f:
config = json.load(f)
app_id = config["apps"][0]["appId"]
app_secret = config["apps"][0]["appSecret"]
resp = requests.post(
f"{BASE_URL}/auth/v3/tenant_access_token/internal",
json={"app_id": app_id, "app_secret": app_secret},
timeout=10,
)
return resp.json()["tenant_access_token"]
token = get_token()
spreadsheet_token = "NiajsPDjXhQHn8tURCeck8zlndd"
# Try textElements format for rich text
cell_data = {
"textElements": [
{
"textElementType": "TEXT_RUN",
"textRun": {
"text": "This is ",
"textElementStyle": {}
}
},
{
"textElementType": "TEXT_RUN",
"textRun": {
"text": "BOLD RED",
"textElementStyle": {
"bold": True,
"textColor": {
"red": 1.0,
"green": 0.0,
"blue": 0.0,
"alpha": 0.0
}
}
}
},
{
"textElementType": "TEXT_RUN",
"textRun": {
"text": " text!",
"textElementStyle": {}
}
}
]
}
values = [
["TEST RICH TEXT", cell_data, "", ""]
]
resp = requests.put(
f"{BASE_URL}/sheets/v2/spreadsheets/{spreadsheet_token}/values",
headers={
"Authorization": f"Bearer {token}",
"Content-Type": "application/json",
},
json={
"valueRange": {
"range": "3O2sso!A1:D1",
"values": values,
},
},
timeout=30,
)
result = resp.json()
print(json.dumps(result, indent=2, ensure_ascii=False))
# Also read back to see format
read_resp = requests.get(
f"{BASE_URL}/sheets/v2/spreadsheets/{spreadsheet_token}/values/3O2sso!A1:D1",
headers={"Authorization": f"Bearer {token}"},
timeout=10,
)
print("\n--- READ BACK ---")
print(json.dumps(read_resp.json(), indent=2, ensure_ascii=False))