A comprehensive, async Rust library for interacting with Google Sheets API v4. This library provides both high-level and low-level APIs for reading, writing, and manipulating Google Sheets programmatically.
- Service Account Authentication: Secure authentication using Google service accounts
- Spreadsheet Operations: Create, read, and update spreadsheets
- Sheet Operations: Work with individual sheets within spreadsheets
- Batch Operations: Efficiently perform multiple operations in a single API call
- Type Safety: Strongly typed models for all Google Sheets data structures
- Async/Await: Modern async support using tokio
- A1 Notation Support: Parse and convert A1 notation ranges and cell references
Add this to your Cargo.toml:
[dependencies]
gsheet_api = "0.1.0"
tokio = { version = "1.0", features = ["full"] }- Go to the Google Cloud Console
- Create a new project or select an existing one
- Enable the Google Sheets API
- Create a service account and download the JSON key file
use gsheet_api::{auth::ServiceAccountAuthClient, client::GoogleSheetClient};
use std::sync::{Arc, Mutex};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Authenticate with service account
let auth_client = ServiceAccountAuthClient::builder()
.service_account_path("path/to/service-account.json")
.build()
.await?;
let auth_client = Arc::new(Mutex::new(auth_client));
// Create Google Sheets client
let gsheet_client = GoogleSheetClient::builder()
.auth_client(auth_client)
.build()?;
// Get spreadsheet operations
let spreadsheet = gsheet_client.spreadsheet("your-spreadsheet-id");
// Read all values from a sheet
let sheet = spreadsheet.sheet("Sheet1");
let values = sheet.get_all_value().execute().await?;
println!("Values: {:?}", values);
Ok(())
}This library supports authentication via Google service accounts. You'll need:
- A Google Cloud Project with the Google Sheets API enabled
- A service account with appropriate permissions
- A service account key JSON file
use gsheet_api::auth::ServiceAccountAuthClient;
let auth_client = ServiceAccountAuthClient::builder()
.service_account_path("keys.json")
.build()
.await?;- Keep service account key files secure and never commit them to version control
- Use environment variables or secure key management systems for key file paths
- Regularly rotate service account keys
- Limit service account permissions to only what's necessary
The library provides several ways to read data from sheets:
let spreadsheet = gsheet_client.spreadsheet("spreadsheet-id");
let values = spreadsheet.sheet("Sheet1")
.get_all_value()
.execute()
.await?;let spreadsheet = gsheet_client.spreadsheet("spreadsheet-id");
let cells = spreadsheet.sheet("Sheet1")
.get_all_cell()
.execute()
.await?;let spreadsheet = gsheet_client.spreadsheet("spreadsheet-id");
let cell_map = spreadsheet.sheet("Sheet1")
.get_hash_map_cell()
.execute()
.await?;let spreadsheet = gsheet_client.spreadsheet("spreadsheet-id");
let batch_values = spreadsheet.sheet("Sheet1")
.batch_get_value_range()
.range("A1:B10")
.range("C1:D10")
.execute()
.await?;let spreadsheet = gsheet_client.spreadsheet("spreadsheet-id");
let response = spreadsheet.sheet("Sheet1")
.batch_update_value_range()
.add_value_range("A1:B2", vec![
vec!["Name".to_string(), "Age".to_string()],
vec!["Alice".to_string(), "30".to_string()],
])
.execute()
.await?;let custom_client = reqwest::Client::builder()
.timeout(std::time::Duration::from_secs(30))
.build()?;
let gsheet_client = GoogleSheetClient::builder()
.auth_client(auth_client)
.client(&custom_client)
.build()?;use gsheet_api::utils::{parse_a1_cell, a1_to_grid_range};
// Parse cell references
let (col, row) = parse_a1_cell("B3")?;
assert_eq!(col, 2);
assert_eq!(row, 3);
// Convert ranges
let grid_range = a1_to_grid_range("A1:B10")?;use gsheet_api::error::GSheetError;
match operation.execute().await {
Ok(result) => println!("Success: {:?}", result),
Err(GSheetError::AuthError(e)) => println!("Authentication error: {}", e),
Err(GSheetError::HttpRequestError(e)) => println!("HTTP error: {}", e),
Err(e) => println!("Other error: {}", e),
}GoogleSheetClient- Main client for API interactionsSpreadsheetOperations- Operations on spreadsheetsSheetOperations- Operations on individual sheetsServiceAccountAuthClient- Service account authentication
Spreadsheet- Spreadsheet representationSheet- Sheet representationCell- Cell data structureValueRange- Range of cell values
See the examples directory for complete working examples:
basic_read.rs- Basic reading operationsbatch_operations.rs- Batch read/write operationsauthentication.rs- Different authentication methods
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
- Clone the repository
- Run tests:
cargo test - Run examples:
cargo run --example basic_read - Check documentation:
cargo doc --open
This project is licensed under the MIT License - see the LICENSE file for details.
- Built on top of the Google Sheets API v4
- Uses reqwest for HTTP requests
- Uses serde for JSON serialization
- Uses jsonwebtoken for JWT handling