This Google Apps Script automation processes emails with invoice/bill attachments, extracts key data using AI-powered OCR, saves files to Google Drive, and logs information to Google Sheets.
- Email Processing: Automatically processes emails with subject "Viable: Trial Document"
- AI-Powered Data Extraction: Uses Google Gemini API for intelligent invoice data extraction
- Multi-OCR Fallback: Falls back to Google Vision API and Drive OCR when needed
- Smart File Management: Saves attachments to Google Drive with structured naming
- Comprehensive Logging: Logs all data to Google Sheets with timestamps
- Automatic Email Management: Labels and marks emails as processed
- Flexible Processing: Can process files from emails or a docs folder
- Error Handling: Robust error handling with detailed logging
- Automated Triggers: Time-based automation (every 3 hours)
- Google Account with Gmail, Drive, and Sheets access
- Google Apps Script project
- Google Gemini API key (recommended for best results)
- Basic understanding of Google Workspace APIs
- Go to Google Drive
- Create these folders:
- "Viable_Test Documents" (for storing processed files)
- "OCR_Temp" (for temporary OCR processing)
- "docs" (optional - for testing with sample files)
- Copy the folder IDs from the URLs
- Go to Google Sheets
- Create a new spreadsheet
- Copy the sheet ID from the URL
- Go to script.google.com
- Click "New Project"
- Paste the code from
final_wokring.gsinto the script editor - Update the configuration constants at the top:
const DRIVE_FOLDER_ID = 'YOUR_DRIVE_FOLDER_ID_HERE';
const SHEET_ID = 'YOUR_SHEET_ID_HERE';
const OCR_TEMP_FOLDER_ID = 'YOUR_OCR_TEMP_FOLDER_ID_HERE';
const GEMINI_API_KEY = 'YOUR_GEMINI_API_KEY_HERE'; Go to Resources → Advanced Google Services and enable:
- Drive API
- Gmail API
- Sheets API
- Go to Google AI Studio
- Create a new API key
- Copy it to the
GEMINI_API_KEYconstant in your script
Run these functions in order:
setupScript()- Creates necessary sheets and validates setuptestGeminiAPI()- Tests API connectivity (if using Gemini)testProcessing()- Tests the main processing function
Run setupTrigger() to create a time-based trigger that runs every 3 hours.
The script creates two sheets automatically:
| Timestamp | Invoice/Bill Date | Invoice/Bill Number | Amount | Vendor/Company Name | Drive File URL | File Type |
|---|---|---|---|---|---|---|
| 2025-06-12 10:30:00 | 03.06.25 | INV-12345 | Rs 40,000 | Viable Ideas Pvt Ltd | [Link] |
| Timestamp | Message |
|---|---|
| 2025-06-12 10:30:00 | ✓ Processing started |
| 2025-06-12 10:30:05 | Found 2 emails to process |
Files are saved with the format: Date_Vendor_InvoiceNumber_Amount.extension
Example: 03.06.25_ViableIdeas_INV12345_Rs40000.pdf
processInvoices()- Main processing functionprocessAttachment()- Handles individual filesperformGeminiExtraction()- AI-powered data extraction
setupScript()- Initial configurationsetupTrigger()- Creates automation triggertestProcessing()- Tests main functionalitytestGeminiAPI()- Tests API connectivitytestDocsFolder()- Tests folder processing
performGeminiExtraction()- Gemini AI extraction (primary)performDriveOCR()- Google Drive OCR (fallback)fallbackToTraditionalOCR()- Traditional OCR methods
getDataSheet()/getLogSheet()- Sheet managementlogMessage()- Centralized loggingformatFilename()- File naming
If no emails are found, the script automatically processes files from the "docs" folder. Upload sample invoices/bills to this folder for testing.
- Images: JPEG, PNG, GIF, BMP, TIFF, WebP
- Documents: PDF
- Email files: EML
- Google Gemini API - AI-powered extraction (recommended)
- Google Drive OCR - Built-in Google functionality (free fallback)
Modify the email search query in processInvoices():
const query = 'subject:"Viable: Trial Document" -label:Processed';
BUT WE CAN MODIFY THIS PART EITHER TO EXTRACT A PARTICULAR PART OR EXTRACT RECENT MAIL .- Comprehensive error logging to Logs sheet
- Graceful fallbacks when APIs fail
- Automatic retry mechanisms
- Efficient batch processing
- Automatic cleanup of temporary files
- Optimized API usage
- API keys stored as constants (consider using Properties Service for production)
- File access restricted to specified folders
- Comprehensive audit trail
- Check email search query
- Verify emails have the correct subject line
- Use
testDocsFolder()to process sample files
- Verify Gemini API key is correct
- Check API quotas and billing
- Enable required Google APIs
- Re-authorize the script
- Check folder permissions
- Verify sheet access
The script extracts:
- Invoice Date: Converted to DD.MM.YY format
- Vendor Name: Company that issued the invoice (max 30 chars)
- Invoice Number: Invoice/bill/reference number
- Total Amount: Final amount in "Rs X,XXX" format
- Gemini AI analysis (most accurate)
- Vision API OCR + pattern matching
- Drive OCR + pattern matching
- Fallback to email metadata
- Store API keys in PropertiesService instead of constants
- Implement additional access controls
- Set up monitoring and alerting
- Adjust trigger frequency based on email volume
- Consider batch processing for high volumes
- Monitor API usage and costs
- Check the Logs sheet regularly for errors
- Monitor processing success rates
- Review API usage and costs
- Set up email notifications for critical errors