Rathapoom commited on
Commit
4458876
·
verified ·
1 Parent(s): ae51ba3

Create sheets_integration.py

Browse files
Files changed (1) hide show
  1. sheets_integration.py +53 -0
sheets_integration.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # sheets_integration.py
2
+ import os
3
+ import json
4
+ from google.oauth2.credentials import Credentials
5
+ from google.oauth2 import service_account
6
+ from googleapiclient.discovery import build
7
+ from google.oauth2.credentials import Credentials
8
+ import pandas as pd
9
+
10
+ class SheetsManager:
11
+ def __init__(self, credentials_path, spreadsheet_id):
12
+ self.credentials_path = credentials_path
13
+ self.spreadsheet_id = spreadsheet_id
14
+ self.credentials = None
15
+ self.service = None
16
+ self.initialize_service()
17
+
18
+ def initialize_service(self):
19
+ """Initialize Google Sheets API service"""
20
+ self.credentials = service_account.Credentials.from_service_account_file(
21
+ self.credentials_path,
22
+ scopes=['https://www.googleapis.com/auth/spreadsheets']
23
+ )
24
+ self.service = build('sheets', 'v4', credentials=self.credentials)
25
+
26
+ def append_row(self, row_data):
27
+ """Append a row to the spreadsheet"""
28
+ try:
29
+ body = {
30
+ 'values': [list(row_data.values())]
31
+ }
32
+ result = self.service.spreadsheets().values().append(
33
+ spreadsheetId=self.spreadsheet_id,
34
+ range='Sheet1!A:I', # Adjust range as needed
35
+ valueInputOption='RAW',
36
+ body=body
37
+ ).execute()
38
+ return True
39
+ except Exception as e:
40
+ print(f"Error appending to sheet: {str(e)}")
41
+ return False
42
+
43
+ def get_all_requests(self):
44
+ """Get all requests from the spreadsheet"""
45
+ try:
46
+ result = self.service.spreadsheets().values().get(
47
+ spreadsheetId=self.spreadsheet_id,
48
+ range='Sheet1!A:I' # Adjust range as needed
49
+ ).execute()
50
+ return pd.DataFrame(result.get('values', []))
51
+ except Exception as e:
52
+ print(f"Error reading sheet: {str(e)}")
53
+ return None