File size: 1,414 Bytes
021e065
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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