Spaces:
Running
Running
| class GoogleSheetsExporter: | |
| """ | |
| Export Senti business data to Google Sheets. | |
| Useful for SMEs who want spreadsheet access. | |
| """ | |
| def export_transactions( | |
| self, | |
| transactions: list, | |
| spreadsheet_id: str, | |
| credentials_path: str | |
| ) -> bool: | |
| try: | |
| from google.oauth2.service_account import ( | |
| Credentials | |
| ) | |
| from googleapiclient.discovery import build | |
| creds = Credentials.from_service_account_file( | |
| credentials_path, | |
| scopes=['https://www.googleapis.com/auth/spreadsheets'] | |
| ) | |
| service = build('sheets', 'v4', credentials=creds) | |
| # Prepare data | |
| headers = [ | |
| 'Date', 'Description', 'Amount', | |
| 'Direction', 'Category', 'Balance' | |
| ] | |
| rows = [headers] | |
| for tx in transactions: | |
| rows.append([ | |
| tx.get('timestamp', '')[:10] if 'timestamp' in tx else tx.get('date', '')[:10], | |
| tx.get('description', ''), | |
| tx.get('amount', 0), | |
| tx.get('direction', ''), | |
| tx.get('category', ''), | |
| tx.get('balance', 0) | |
| ]) | |
| service.spreadsheets().values().update( | |
| spreadsheetId=spreadsheet_id, | |
| range='Sheet1!A1', | |
| valueInputOption='RAW', | |
| body={'values': rows} | |
| ).execute() | |
| return True | |
| except Exception as e: | |
| print(f"Sheets export failed: {e}") | |
| return False | |