All examples below use ${GOOGLE_ACCESS_TOKEN}. Before running, either:
- Set manually:
GOOGLE_ACCESS_TOKEN="ya29.xxx...", or - Replace
${GOOGLE_ACCESS_TOKEN} with $(cat /tmp/sheets_token.txt) in each command
> Important: In range notation like Sheet1!A1:D10, the ! must be URL encoded as %21 in the URL path (e.g., Sheet1%21A1:D10). All examples below use this encoding.
Base URL: https://sheets.googleapis.com/v4/spreadsheets
Finding your Spreadsheet ID:
The spreadsheet ID is in the URL: https://docs.google.com/spreadsheets/d/{SPREADSHEET_ID}/edit
---
1. Get Spreadsheet Metadata
Get information about a spreadsheet (sheets, properties). Replace with your actual spreadsheet ID:
```bash
bash -c 'curl -s "https://sheets.googleapis.com/v4/spreadsheets/" -H "Authorization: Bearer ${GOOGLE_ACCESS_TOKEN}"' | jq '{title: .properties.title, sheets: [.sheets[].properties | {sheetId, title}]}''
```
---
2. Read Cell Values
Read a range of cells. Replace with your actual spreadsheet ID:
```bash
bash -c 'curl -s "https://sheets.googleapis.com/v4/spreadsheets//values/Sheet1%21A1:D10" -H "Authorization: Bearer ${GOOGLE_ACCESS_TOKEN}"' | jq '.values'
```
---
3. Read Entire Sheet
Read all data from a sheet. Replace with your actual spreadsheet ID:
```bash
bash -c 'curl -s "https://sheets.googleapis.com/v4/spreadsheets//values/Sheet1" -H "Authorization: Bearer ${GOOGLE_ACCESS_TOKEN}"' | jq '.values'
```
---
4. Read with API Key (Public Sheets)
For publicly accessible sheets. Replace with your actual spreadsheet ID:
```bash
bash -c 'curl -s "https://sheets.googleapis.com/v4/spreadsheets//values/Sheet1%21A1:D10?key=${GOOGLE_API_KEY}"' | jq '.values'
```
---
5. Write Cell Values
Update a range of cells. Replace with your actual spreadsheet ID.
Write to /tmp/gsheets_request.json:
```json
{
"values": [
["Name", "Email", "Status"]
]
}
```
Then run:
```bash
bash -c 'curl -s -X PUT "https://sheets.googleapis.com/v4/spreadsheets//values/Sheet1%21A1:C1?valueInputOption=USER_ENTERED" -H "Authorization: Bearer ${GOOGLE_ACCESS_TOKEN}" -H "Content-Type: application/json" -d @/tmp/gsheets_request.json' | jq '.updatedCells'
```
valueInputOption:
RAW: Values are stored as-isUSER_ENTERED: Values are parsed as if typed by user (formulas evaluated)
---
6. Append Rows
Add new rows to the end of a sheet. Replace with your actual spreadsheet ID.
Write to /tmp/gsheets_request.json:
```json
{
"values": [
["John Doe", "john@example.com", "Active"]
]
}
```
Then run:
```bash
bash -c 'curl -s -X POST "https://sheets.googleapis.com/v4/spreadsheets//values/Sheet1%21A:C:append?valueInputOption=USER_ENTERED&insertDataOption=INSERT_ROWS" -H "Authorization: Bearer ${GOOGLE_ACCESS_TOKEN}" -H "Content-Type: application/json" -d @/tmp/gsheets_request.json' | jq '.updates | {updatedRange, updatedRows}'
```
---
7. Batch Read Multiple Ranges
Read multiple ranges in one request. Replace with your actual spreadsheet ID:
```bash
bash -c 'curl -s "https://sheets.googleapis.com/v4/spreadsheets//values:batchGet?ranges=Sheet1%21A1:B5&ranges=Sheet1%21D1:E5" -H "Authorization: Bearer ${GOOGLE_ACCESS_TOKEN}"' | jq '.valueRanges'
```
---
8. Batch Update Multiple Ranges
Update multiple ranges in one request. Replace with your actual spreadsheet ID.
Write to /tmp/gsheets_request.json:
```json
{
"valueInputOption": "USER_ENTERED",
"data": [
{
"range": "Sheet1!A1",
"values": [["Header 1"]]
},
{
"range": "Sheet1!B1",
"values": [["Header 2"]]
}
]
}
```
Then run:
```bash
bash -c 'curl -s -X POST "https://sheets.googleapis.com/v4/spreadsheets//values:batchUpdate" -H "Authorization: Bearer ${GOOGLE_ACCESS_TOKEN}" -H "Content-Type: application/json" -d @/tmp/gsheets_request.json' | jq '.totalUpdatedCells'
```
---
9. Clear Cell Values
Clear a range of cells. Replace with your actual spreadsheet ID.
Write to /tmp/gsheets_request.json:
```json
{}
```
Then run:
```bash
bash -c 'curl -s -X POST "https://sheets.googleapis.com/v4/spreadsheets//values/Sheet1%21A2:C100:clear" -H "Authorization: Bearer ${GOOGLE_ACCESS_TOKEN}" -H "Content-Type: application/json" -d @/tmp/gsheets_request.json' | jq '.clearedRange'
```
---
10. Create New Spreadsheet
Create a new spreadsheet:
Write to /tmp/gsheets_request.json:
```json
{
"properties": {
"title": "My New Spreadsheet"
},
"sheets": [
{
"properties": {
"title": "Data"
}
}
]
}
```
Then run:
```bash
bash -c 'curl -s -X POST "https://sheets.googleapis.com/v4/spreadsheets" -H "Authorization: Bearer ${GOOGLE_ACCESS_TOKEN}" -H "Content-Type: application/json" -d @/tmp/gsheets_request.json' | jq '.spreadsheetId, .spreadsheetUrl'
```
---
11. Add New Sheet
Add a new sheet to an existing spreadsheet. Replace with your actual spreadsheet ID.
Write to /tmp/gsheets_request.json:
```json
{
"requests": [
{
"addSheet": {
"properties": {
"title": "New Sheet"
}
}
}
]
}
```
Then run:
```bash
bash -c 'curl -s -X POST "https://sheets.googleapis.com/v4/spreadsheets/:batchUpdate" -H "Authorization: Bearer ${GOOGLE_ACCESS_TOKEN}" -H "Content-Type: application/json" -d @/tmp/gsheets_request.json' | jq '.replies[0].addSheet.properties'
```
---
12. Delete Sheet
Delete a sheet from a spreadsheet (use sheetId from metadata). Replace with your actual spreadsheet ID.
Write to /tmp/gsheets_request.json:
```json
{
"requests": [
{
"deleteSheet": {
"sheetId": 123456789
}
}
]
}
```
Then run:
```bash
bash -c 'curl -s -X POST "https://sheets.googleapis.com/v4/spreadsheets/:batchUpdate" -H "Authorization: Bearer ${GOOGLE_ACCESS_TOKEN}" -H "Content-Type: application/json" -d @/tmp/gsheets_request.json'
```
---
13. Search for Values
Find cells containing specific text (read all then filter). Replace with your actual spreadsheet ID:
```bash
bash -c 'curl -s "https://sheets.googleapis.com/v4/spreadsheets//values/Sheet1" -H "Authorization: Bearer ${GOOGLE_ACCESS_TOKEN}"' | jq '[.values[] | select(.[0] | ascii_downcase | contains("search_term"))]'
```
---