Chat with your Google Sheets using natural language powered by Google's Gemini AI!
Transform your spreadsheet data into conversational insights! Ask questions about your Google Sheets data in plain English and get intelligent responses powered by Google's Gemini AI.
π Natural Language Queries - Ask questions like "Who are the engineers?" or "What's the average salary in Sales?"
π€ AI-Powered Analysis - Leverages Google's Gemini 1.5 Flash for intelligent data interpretation
π Two-Step RAG Pipeline - Ensures AI answers are grounded in real data from your Google Sheets
π§ Auto-Setup - Automatically creates and populates sample data in your Google Sheets
π‘οΈ Secure Authentication - Uses Google Cloud service account authentication
β‘ Real-time Processing - Instant responses with live Google Sheets data
π Cloud-Based - Shareable data via Google Sheets with immediate updates
The application follows a simple, two-step RAG (Retrieval-Augmented Generation) pattern:
graph TD
A[π€ User Question] --> B[π§ Gemini AI Analysis]
B --> C[π Query Description]
C --> D[π Google Sheets API]
D --> E[π Data Filtering]
E --> F[π€ Answer Generation]
F --> G[π¬ Natural Language Response]
- Step 1: Query Analysis - Converts your question into a filtering strategy using your sheet schema
- Step 2: Data Retrieval & Response - Filters the data and generates a friendly, natural language answer
Here's what a typical session looks like:
β Google Sheet 'Employees' populated with sample data.
Google Sheets Schema:
Sheet Name: Employees
Columns:
- A: Id (INTEGER) - Employee ID number
- B: Name (TEXT) - Employee full name
- C: Department (TEXT) - Department (Engineering, Sales, HR)
- D: Salary (INTEGER) - Annual salary in USD
- E: HireDate (TEXT) - Date hired (YYYY-MM-DD format)
Chat with your Google Sheet! Type 'exit' to quit.
> Who are the engineers?
π§ Query Logic: Filter employees where Department column contains 'Engineering' and return their names.
π Sheet Result:
Id Name Department Salary HireDate
1 Alice Johnson Engineering 95000 2022-01-15
3 Charlie Brown Engineering 110000 2020-05-20
π¬ Answer: The engineers are Alice Johnson and Charlie Brown.
> What is the average salary in the Sales department?
π§ Query Logic: Filter employees where Department is 'Sales' and calculate average of Salary column.
π Sheet Result:
Id Name Department Salary HireDate
2 Bob Smith Sales 82000 2021-11-30
4 Diana Prince Sales 78000 2022-08-01
π¬ Answer: The average salary for the Sales department is $80,000.Before you begin, ensure you have:
- .NET 9 SDK
- Google AI Studio API Key
- Google Cloud Project with Sheets API enabled
- Service Account JSON credentials
git clone https://github.com/donpotts/GeminiSheetsChat.git
cd GeminiSheetsChat
dotnet restoredotnet user-secrets init
dotnet user-secrets set "GEMINI_API_KEY" "your-gemini-api-key-here"dotnet user-secrets set "GOOGLE_SPREADSHEET_ID" "your-spreadsheet-id-here"-
Create Google Cloud Project
- Go to Google Cloud Console
- Create project and enable Google Sheets API
-
Create Service Account
- Go to APIs & Services > Credentials
- Create Service Account and download JSON key
- Rename to
geminisheetschat.jsonand place in project directory
-
Setup Google Sheet
- Create a new Google Sheet
- Copy the Spreadsheet ID from URL
- Share with service account email (from JSON file)
- Grant "Editor" permissions
No additional configuration needed! The spreadsheet ID is now managed through user secrets for security.
dotnet runTry these natural language questions:
- π Department Queries: "Who are the engineers?", "Show me the Sales team"
- π° Salary Analysis: "Who earns more than $90,000?", "What's the average salary?"
- π Date Filtering: "Who was hired in 2022?", "Show recent hires"
- π Statistics: "How many people work in HR?", "What's the highest salary?"
- π₯ General: "List all employees", "Show me everyone"
| Component | Technology | Purpose |
|---|---|---|
| AI Framework | Microsoft Semantic Kernel 1.64.0 | AI orchestration and prompt management |
| Language Model | Google Gemini 1.5 Flash | Natural language understanding and generation |
| Data Source | Google Sheets API v4 | Cloud-based spreadsheet integration |
| Runtime | .NET 9.0 | Modern, high-performance application platform |
| Authentication | Google Cloud Service Account | Secure API access |
- Semantic Kernel: Microsoft's AI orchestration framework for building AI applications
- Google Gemini: Advanced language model for query understanding and response generation
- Google Sheets API: Direct integration with Google Sheets for real-time data access
- Smart Filtering: Context-aware data filtering based on AI analysis
builder.AddGoogleAIGeminiChatCompletion(
modelId: "gemini-1.5-pro", // More capable but slower
// or "gemini-1.5-flash", // Faster and more cost-effective
apiKey: apiKey
);Extend FilterDataBasedOnQuery method:
// Example: Age-based filtering
else if (query.Contains("age") && query.Contains("older than"))
{
// Extract age threshold from query
var ageMatches = Regex.Matches(query, @"\d+");
if (ageMatches.Count > 0 && int.TryParse(ageMatches[0].Value, out int ageThreshold))
{
// Implement age filtering logic
includeRow = CalculateAge(row[5]?.ToString()) > ageThreshold;
}
}- Update sample data in
SetupGoogleSheet - Modify
GetSheetSchema()description - Adjust data range in
ExecuteSheetQueryAndFormatResults - Update filtering logic as needed
The application creates this sample employee dataset:
| Id | Name | Department | Salary | HireDate |
|---|---|---|---|---|
| 1 | Alice Johnson | Engineering | $95,000 | 2022-01-15 |
| 2 | Bob Smith | Sales | $82,000 | 2021-11-30 |
| 3 | Charlie Brown | Engineering | $110,000 | 2020-05-20 |
| 4 | Diana Prince | Sales | $78,000 | 2022-08-01 |
| 5 | Eve Adams | HR | $65,000 | 2023-02-10 |
- π Keep
geminisheetschat.jsonsecure and never commit to version control - π Add credential files to
.gitignore - π Use user secrets for API keys in development
- π Use environment variables for production deployments
- π₯ Grant minimal necessary permissions to service accounts
- π Keep spreadsheet IDs in configuration, not source code
| Issue | Solution |
|---|---|
| "GEMINI_API_KEY is not set" | Run: dotnet user-secrets set "GEMINI_API_KEY" "your-key" |
| "GOOGLE_SPREADSHEET_ID is not set" | Run: dotnet user-secrets set "GOOGLE_SPREADSHEET_ID" "your-id" |
| "credentials.json not found" | Download service account JSON from Google Cloud Console |
| "Unable to parse range" | Ensure sheet name exists and matches configuration |
| "The caller does not have permission" | Share Google Sheet with service account email |
| Authentication errors | Verify JSON file location and service account permissions |
- Verify API Keys: Check user secrets with
dotnet user-secrets list - Test Sheet Access: Ensure service account can access your Google Sheet
- Check Logs: Review console output for specific error messages
- Validate JSON: Ensure service account JSON file is valid
- Microsoft Semantic Kernel - AI orchestration framework
- Google AI - Gemini language models
- Google Sheets API - Spreadsheet integration
- .NET Foundation - .NET ecosystem and tools
If you find this project helpful, please consider:
- β Starring this repository
- π Reporting bugs or suggesting features
- π’ Sharing with others who might benefit
Don Potts - Don.Potts@DonPotts.com
π Transform your spreadsheets into intelligent conversations today! π