google-sheets
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseDrive Google Sheets via . The user's OAuth bearer token is in
; every call needs . Base URL: .
The token carries (+ identity); writes need the broader
scope.
curl + jq$GOOGLE_SHEETS_TOKENAuthorization: Bearer $GOOGLE_SHEETS_TOKENhttps://sheets.googleapis.com/v4/spreadsheetsspreadsheets.readonlyspreadsheetsFailures are — show verbatim. =
re-install. on a write = read-only scope → re-connect
with read+write.
{"error":{"code","message","status"}}401403 PERMISSION_DENIEDThe spreadsheet id is the segment of the URL.
…/spreadsheets/d/<ID>/editbash
S="https://sheets.googleapis.com/v4/spreadsheets"; AUTH="Authorization: Bearer $GOOGLE_SHEETS_TOKEN"通过操作Google Sheets。用户的OAuth bearer token存储在中;每次调用都需要携带请求头。基础URL:。
该token包含(+身份验证)权限;写入操作需要更广泛的权限范围。
curl + jq$GOOGLE_SHEETS_TOKENAuthorization: Bearer $GOOGLE_SHEETS_TOKENhttps://sheets.googleapis.com/v4/spreadsheetsspreadsheets.readonlyspreadsheets失败响应格式为 — 需原样展示。错误表示需要重新安装授权。写入时出现错误表示当前为只读权限范围 → 需要重新连接并获取读写权限。
{"error":{"code","message","status"}}401403 PERMISSION_DENIED电子表格ID是URL中部分的。
…/spreadsheets/d/<ID>/edit<ID>bash
S="https://sheets.googleapis.com/v4/spreadsheets"; AUTH="Authorization: Bearer $GOOGLE_SHEETS_TOKEN"Tabs + title
标签页 + 标题
curl -sS -H "$AUTH" "$S/SPREADSHEET_ID?fields=properties.title,sheets.properties(title,sheetId)"
| jq '{title: .properties.title, tabs: [.sheets[].properties.title]}'
| jq '{title: .properties.title, tabs: [.sheets[].properties.title]}'
undefinedcurl -sS -H "$AUTH" "$S/SPREADSHEET_ID?fields=properties.title,sheets.properties(title,sheetId)"
| jq '{title: .properties.title, tabs: [.sheets[].properties.title]}'
| jq '{title: .properties.title, tabs: [.sheets[].properties.title]}'
undefinedRead / append / update values
读取 / 追加 / 更新数据
bash
ID="SPREADSHEET_ID"bash
ID="SPREADSHEET_ID"Read a range (A1 notation; values are rows of cells)
读取指定区域(A1表示法;返回值为单元格组成的行数组)
curl -sS -H "$AUTH" "$S/$ID/values/Sheet1!A1:D20" | jq '.values'
curl -sS -H "$AUTH" "$S/$ID/values/Sheet1!A1:D20" | jq '.values'
Append rows (confirm first). valueInputOption=USER_ENTERED parses formulas/dates.
追加行(请先确认)。valueInputOption=USER_ENTERED会解析公式/日期。
curl -sS -X POST -H "$AUTH" -H "Content-Type: application/json"
-d '{"values":[["2026-06-21","Acme",4200]]}'
"$S/$ID/values/Sheet1!A1:append?valueInputOption=USER_ENTERED" | jq '.updates'
-d '{"values":[["2026-06-21","Acme",4200]]}'
"$S/$ID/values/Sheet1!A1:append?valueInputOption=USER_ENTERED" | jq '.updates'
curl -sS -X POST -H "$AUTH" -H "Content-Type: application/json"
-d '{"values":[["2026-06-21","Acme",4200]]}'
"$S/$ID/values/Sheet1!A1:append?valueInputOption=USER_ENTERED" | jq '.updates'
-d '{"values":[["2026-06-21","Acme",4200]]}'
"$S/$ID/values/Sheet1!A1:append?valueInputOption=USER_ENTERED" | jq '.updates'
Update a fixed range: PUT /values/{range}?valueInputOption=USER_ENTERED
更新固定区域:PUT /values/{range}?valueInputOption=USER_ENTERED
curl -sS -X PUT -H "$AUTH" -H "Content-Type: application/json"
-d '{"values":[["done"]]}'
"$S/$ID/values/Sheet1!E2?valueInputOption=USER_ENTERED" | jq '.updatedCells'
-d '{"values":[["done"]]}'
"$S/$ID/values/Sheet1!E2?valueInputOption=USER_ENTERED" | jq '.updatedCells'
undefinedcurl -sS -X PUT -H "$AUTH" -H "Content-Type: application/json"
-d '{"values":[["done"]]}'
"$S/$ID/values/Sheet1!E2?valueInputOption=USER_ENTERED" | jq '.updatedCells'
-d '{"values":[["done"]]}'
"$S/$ID/values/Sheet1!E2?valueInputOption=USER_ENTERED" | jq '.updatedCells'
undefinedCreate a spreadsheet / add a tab
创建电子表格 / 添加标签页
bash
undefinedbash
undefinedNew spreadsheet
创建新电子表格
curl -sS -X POST -H "$AUTH" -H "Content-Type: application/json"
-d '{"properties":{"title":"Report 2026"}}' "$S" | jq '{spreadsheetId, spreadsheetUrl}'
-d '{"properties":{"title":"Report 2026"}}' "$S" | jq '{spreadsheetId, spreadsheetUrl}'
curl -sS -X POST -H "$AUTH" -H "Content-Type: application/json"
-d '{"properties":{"title":"Report 2026"}}' "$S" | jq '{spreadsheetId, spreadsheetUrl}'
-d '{"properties":{"title":"Report 2026"}}' "$S" | jq '{spreadsheetId, spreadsheetUrl}'
Add a tab via batchUpdate (addSheet request)
通过batchUpdate添加标签页(addSheet请求)
curl -sS -X POST -H "$AUTH" -H "Content-Type: application/json"
-d '{"requests":[{"addSheet":{"properties":{"title":"Q3"}}}]}'
"$S/$ID:batchUpdate" | jq '.replies'
-d '{"requests":[{"addSheet":{"properties":{"title":"Q3"}}}]}'
"$S/$ID:batchUpdate" | jq '.replies'
undefinedcurl -sS -X POST -H "$AUTH" -H "Content-Type: application/json"
-d '{"requests":[{"addSheet":{"properties":{"title":"Q3"}}}]}'
"$S/$ID:batchUpdate" | jq '.replies'
-d '{"requests":[{"addSheet":{"properties":{"title":"Q3"}}}]}'
"$S/$ID:batchUpdate" | jq '.replies'
undefinedGotchas
注意事项
- A1 notation: ; quote tab names with spaces as
Sheet1!A1:D20.'My Tab'!A1 - stores strings as-is;
valueInputOption=RAWinterprets them like the UI (numbers, dates,USER_ENTERED). Pick deliberately.=FORMULA - is row-major (
values); a missing trailing cell just comes back short — don't assume rectangular.[[row1...],[row2...]]
- A1表示法:格式为;若标签页名称包含空格,需用单引号包裹,如
Sheet1!A1:D20。'My Tab'!A1 - 会按原样存储字符串;
valueInputOption=RAW会像UI一样解析内容(数字、日期、USER_ENTERED公式)。请根据需求选择。=FORMULA - 采用行优先顺序(
values);缺失的末尾单元格会缩短返回结果 — 不要假设返回的是矩形结构。[[行1...],[行2...]]