microsoft-excel

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese
Drive live Excel workbooks via the Microsoft Graph workbook API with
curl + jq
. The user's OAuth bearer token is in
$MICROSOFT_EXCEL_TOKEN
; every call needs
Authorization: Bearer $MICROSOFT_EXCEL_TOKEN
. Base URL:
https://graph.microsoft.com/v1.0
. The workbook API operates on a drive item (an .xlsx in OneDrive/SharePoint), so you need its
ITEM_ID
.
Failures are
{"error":{"code","message"}}
— show
message
verbatim.
401
= token expired (re-install).
403
on a write = the user granted read-only.
bash
G="https://graph.microsoft.com/v1.0"; AUTH="Authorization: Bearer $MICROSOFT_EXCEL_TOKEN"
通过Microsoft Graph workbook API,使用
curl + jq
操作在线Excel工作簿。用户的OAuth承载令牌存储在
$MICROSOFT_EXCEL_TOKEN
中;每次调用都需要携带
Authorization: Bearer $MICROSOFT_EXCEL_TOKEN
请求头。基础URL为:
https://graph.microsoft.com/v1.0
。工作簿API针对驱动器项(OneDrive/SharePoint中的.xlsx文件)进行操作,因此你需要获取其
ITEM_ID
错误响应格式为
{"error":{"code","message"}}
—— 需原样展示
message
字段内容。
401
表示令牌过期(需重新安装)。写入操作返回
403
表示用户仅被授予只读权限。
bash
G="https://graph.microsoft.com/v1.0"; AUTH="Authorization: Bearer $MICROSOFT_EXCEL_TOKEN"

Find the workbook by name (then take .id as ITEM_ID)

按名称查找工作簿(然后取.id作为ITEM_ID)

curl -sS -H "$AUTH" "$G/me/drive/root/search(q='budget.xlsx')"
| jq '.value[] | {id, name, webUrl}'
curl -sS -H "$AUTH" "$G/me/drive/root/search(q='budget.xlsx')"
| jq '.value[] | {id, name, webUrl}'

Worksheets in the workbook

获取工作簿中的工作表

curl -sS -H "$AUTH" "$G/me/drive/items/ITEM_ID/workbook/worksheets"
| jq '.value[] | {id, name, position}'
undefined
curl -sS -H "$AUTH" "$G/me/drive/items/ITEM_ID/workbook/worksheets"
| jq '.value[] | {id, name, position}'
undefined

Read & write ranges

读取与写入单元格区域

bash
ITEM="ITEM_ID"; WS="Sheet1"
bash
ITEM="ITEM_ID"; WS="Sheet1"

Used range (values + formulas + formats)

已使用区域(包含值、公式和格式)

curl -sS -H "$AUTH"
"$G/me/drive/items/$ITEM/workbook/worksheets/$WS/usedRange"
| jq '{address, values: .values}'
curl -sS -H "$AUTH"
"$G/me/drive/items/$ITEM/workbook/worksheets/$WS/usedRange"
| jq '{address, values: .values}'

Read a specific range

读取指定区域

curl -sS -H "$AUTH"
"$G/me/drive/items/$ITEM/workbook/worksheets/$WS/range(address='A1:C5')" | jq '.values'
curl -sS -H "$AUTH"
"$G/me/drive/items/$ITEM/workbook/worksheets/$WS/range(address='A1:C5')" | jq '.values'

Update a range (confirm first). values is a 2-D array matching the address shape.

更新指定区域(请先确认)。values是与目标区域形状匹配的二维数组。

curl -sS -X PATCH -H "$AUTH" -H "Content-Type: application/json"
-d '{"values":[["Q3",1200],["Q4",1580]]}'
"$G/me/drive/items/$ITEM/workbook/worksheets/$WS/range(address='A2:B3')" | jq '.address'
undefined
curl -sS -X PATCH -H "$AUTH" -H "Content-Type: application/json"
-d '{"values":[["Q3",1200],["Q4",1580]]}'
"$G/me/drive/items/$ITEM/workbook/worksheets/$WS/range(address='A2:B3')" | jq '.address'
undefined

Append a row to a table

向表格追加行

bash
curl -sS -X POST -H "$AUTH" -H "Content-Type: application/json" \
  -d '{"values":[["2026-06-21","Acme",4200]]}' \
  "$G/me/drive/items/$ITEM/workbook/tables/Table1/rows/add" | jq '.index'
bash
curl -sS -X POST -H "$AUTH" -H "Content-Type: application/json" \
  -d '{"values":[["2026-06-21","Acme",4200]]}' \
  "$G/me/drive/items/$ITEM/workbook/tables/Table1/rows/add" | jq '.index'

Gotchas

注意事项

  • This is for cloud workbooks (OneDrive/SharePoint). A local .xlsx the user uploaded into chat is a different job (parse the file directly), not this skill.
  • Range
    address
    is a string like
    Sheet1!A1:C5
    or just
    A1:C5
    when scoped to a worksheet;
    values
    must be a 2-D array matching its dimensions.
  • For long-running edits Graph supports a workbook session header (
    workbook-session-id
    ); for a few cells the default sessionless mode is fine.
  • 此功能仅适用于云端工作簿(OneDrive/SharePoint)。用户上传到聊天中的本地.xlsx文件属于另一种场景(需直接解析文件),不适用此技能。
  • 单元格区域
    address
    是类似
    Sheet1!A1:C5
    的字符串,或在指定工作表范围内仅使用
    A1:C5
    values
    必须是与区域维度匹配的二维数组。
  • 对于长时间运行的编辑操作,Graph支持工作簿会话请求头(
    workbook-session-id
    );若仅编辑少量单元格,默认的无会话模式即可满足需求。