A comprehensive bill management and tracking system that automates bill processing, categorization, and spreadsheet updates using Google Drive, Google Sheets, and Firestore.
The SmartFinance Bills system is composed of three main components that work together to provide an end-to-end bill management solution:
ββββββββββββββββββββββββββββββββββββββββββββββββββββ
β SmartFinance Bills Core System β
ββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β βββββββββββββββββββββββββββ β
β β sfin-bills Engine β β
β β β’ File Processing β β
β β β’ Category Mapping β β
β β β’ Spreadsheet Updates β β
β βββββββββββββββββββββββββββ β
β βΌ β
β ββββββββββββββββββββββββββββββββββββ β
β β Google Cloud & Sheets APIs β β
β β β’ Drive β’ Sheets β’ Firestore β β
β β β’ Pub/Sub β’ Auth β β
β ββββββββββββββββββββββββββββββββββββ β
β β
ββββββββββββββββββββββββββββββββββββββββββββββββββββ
The main server-side application that orchestrates bill processing and spreadsheet management.
Location: /sfin-bills
Key Responsibilities:
- Monitors Google Drive for bill-related files (
comprovante,pix,fatura) - Extracts and parses file metadata
- Updates Google Sheets with processed bill data
- Manages bill categorization and mapping
- Handles OAuth2 authentication with Google APIs
- Processes Pub/Sub messages from Google Cloud
Tech Stack:
- Node.js (ES Modules)
- Google Drive API v3
- Google Sheets API
- Google Cloud Firestore
- Google Cloud Pub/Sub
- Python (for PDF extraction)
Key Files:
sfin_bills_server.mjs- HTTP server and OAuth2 orchestrationfile_loader.mjs- Google Drive file processing and filteringspreadsheet.mjs- Google Sheets update logicpdf-extraction/main.py- PDF bill extraction
1. Google Drive Monitoring
βββΊ File detected (comprovante/pix/fatura)
βββΊ Filtered by modification date (current month)
βββΊ Processed by file_loader.mjs
2. File Processing
βββΊ Filename parsed for bill data
βββΊ Validation against naming standards
βββΊ Category mapping applied via Firestore
3. Spreadsheet Update
βββΊ Month reference identified in Google Sheets
βββΊ Working column determined
βββΊ Cell values updated with bill data
βββΊ Data persisted to Google Sheets
4. Configuration Management
βββΊ Firestore stores category mappings
βββΊ Dynamic configuration retrieved at runtime
- Node.js v14.21.3 or higher
- Google Cloud Project with:
- Google Drive API enabled
- Google Sheets API enabled
- Firestore database configured
- Pub/Sub topics created
- Google credentials JSON file
- Google OAuth2 token
cd sfin-bills
npm installEnvironment Variables:
HOME=/home/user # Home directory for credentials
credentials=path/to/credentials.json # Google Cloud service account
spreadsheet=SPREADSHEET_ID # Google Sheets document ID
category_column=2 # Column index for categories (0-based)
months_row=1 # Row index for months
settings_document_cfg=settings # Firestore settings document
document_cfg=bills_mapping # Firestore bills mapping document
collection=bills # Firestore bills registration collectionStart Server:
npm start
# Server runs on port 8321- Real-time monitoring of Google Drive for bill files
- Intelligent file filtering by modification date
- Support for multiple bill types (comprovante, pix, fatura)
- Automatic category mapping from filenames
- Configurable categorization rules
- Unmapped bill detection and reporting
- Automatic monthly tracking in Google Sheets
- Intelligent column and month detection
- Multi-currency support (factor: 100)
- Firestore-backed settings
- Dynamic category mapping
- Environment-based configuration
- Python-based PDF bill extraction
- Structured data parsing
- Category management endpoints
- Settings configuration API
- Integration webhook support
sfin-bills/
βββ sfin_bills_server.mjs # OAuth2 & HTTP server entry point
βββ file_loader.mjs # Google Drive file processing
βββ spreadsheet.mjs # Google Sheets update logic
βββ pdf-extraction/
β βββ main.py # PDF extraction script
β βββ requirements.txt # Python dependencies
βββ test/ # Unit tests (Mocha)
β βββ file_loader_test.mjs
β βββ spreadsheet_test.mjs
βββ scripts/ # Utility scripts
βββ Dockerfile # Container configuration
βββ cloudbuild.yaml # Google Cloud Build config
βββ package.json # Node.js dependencies
βββ README.md # This file
- OAuth2 flow for Google Drive and Sheets API access
- Token storage in
~/.credentials/token.json - Automatic token refresh on expiration
- Service account credentials for Firestore and Pub/Sub
- Environment-based credential file configuration
- Scoped API permissions
~/.credentials/
βββ client_secret.json # OAuth2 secrets
βββ token.json # OAuth2 token
βββ SmartFinance-Bills-Beta-*.json # Service account key
settings- Global application settingsbills_mapping_test- Example of Category to bill mappings
{
"bills_mapping_test": {
"categories": {
"housing": ["casa", "aluguel"],
"utilities": ["agua", "luz", "gas"],
"food": ["supermercado", "restaurante"],
"transportation": ["uber", "combustivel"]
}
}
}docker build -t sfin-bills .
docker run -e HOME=/root \
-e credentials=/path/to/credentials.json \
-v ~/.credentials:/root/.credentials \
sfin-bills- Uses
cloudbuild.yamlfor automated builds - Container-based deployment
- Environment configuration via Cloud Build secrets
cd sfin-bills
npm testTests use Mocha and expect.js framework.
Test Files:
test/file_loader_test.mjs- File processing and Google Drive integration teststest/spreadsheet_test.mjs- Spreadsheet update and data handling tests
Bills must follow naming convention for proper processing:
[TYPE]_[IDENTIFIER_SOURCE]_[VALUE]
Examples:
- comprovante_contaluz_10000
- pix_mercado_4500
- fatura_lojaderoupa_15559
Supported File Types:
comprovante- Bill proof/receiptpix- Instant payment receiptfatura- Invoice/bill
| Parameter | Default | Description |
|---|---|---|
category_column |
- | Spreadsheet column for categories |
months_row |
1 | Spreadsheet row containing months |
settings_document_cfg |
settings | Firestore settings document |
document_cfg |
bills_mapping_test | Firestore mapping document |
- Google Drive API v3 - File monitoring and retrieval
- Google Sheets API - Spreadsheet updates
- Google Cloud Firestore - Data persistence
- Google Cloud Pub/Sub - Event messaging
- Google Auth Library - OAuth2 & JWT authentication
- Moment.js for date/time handling
- Portuguese (pt-BR) locale support by default
- UTC timezone for API operations
google-auth-library- OAuth2 & JWT authenticationgoogleapis- Google APIs client librarygoogle-spreadsheet- Google Sheets API wrapper@google-cloud/firestore- Firestore database client@google-cloud/pubsub- Google Cloud Pub/Sub clientmoment- Date/time utilities and formattinglodash- Utility functions for data manipulationcollections- Data structures (Map, Dict, List)async- Asynchronous utilitiessprintf-js- String formatting
- Core Engine: v0.9.0
- Node.js: 14.21.3
- Angular: 17.2.3
- GitHub: manasouza/sfin-bills
- License: ISC
- Author: manasouza
Token Expiration Error:
Check token expiration in ~/.credentials/token.json
Re-authenticate via OAuth2 flow
Month Reference Not Found:
Ensure MONTHS_ROW environment variable is set correctly
Check spreadsheet structure matches expected format
No Files Found:
Verify Google Drive authentication
Check file naming convention
Ensure files were modified within current month
cd sfin-bills
npm startThe server will start on port 8321 and begin monitoring for files.
-
Upload a bill file to Google Drive with standard naming convention
- Example:
comprovante_condominio_98800.pdf - File must be modified within the current month
- Example:
-
Engine automatically detects the new file via Google Drive API
-
Category is applied based on Firestore mappings
-
Spreadsheet is updated in the configured Google Sheet
- Month column is auto-detected
- Data inserted in the appropriate category row
-
Logs show processing status
- Monitor console output for [INFO], [DEBUG], or [ERROR] messages
The system logs provide visibility into operations:
[INFO]- General information and status[DEBUG]- Detailed debug information[ERROR]- Error conditions requiring attention
Monitor logs for:
- File processing status
- Authentication token status
- API call results
- Spreadsheet update operations
https://developers.google.com/workspace/drive/api/guides/search-shareddrives?hl=pt-br
ISC License - See repository for details