1
1
from google .oauth2 .service_account import Credentials
2
2
from googleapiclient .discovery import build
3
3
import pandas as pd
4
+ import logging
5
+
6
+ # Configure logging
7
+ logging .basicConfig (
8
+ level = logging .INFO ,
9
+ format = '%(asctime)s - %(levelname)s - %(message)s' ,
10
+ handlers = [
11
+ logging .FileHandler ('app.log' ),
12
+ logging .StreamHandler ()
13
+ ]
14
+ )
15
+ logger = logging .getLogger (__name__ )
4
16
5
17
def get_google_sheet_data ():
6
- # Define the scope and credentials
7
- SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly' ]
8
- creds = Credentials .from_service_account_file ('credentials.json' , scopes = SCOPES )
18
+ try :
19
+ # Define the scope and credentials
20
+ SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly' ]
21
+ creds = Credentials .from_service_account_file ('credentials.json' , scopes = SCOPES )
22
+ logger .info ("Successfully loaded credentials" )
9
23
10
- # Create the service
11
- service = build ('sheets' , 'v4' , credentials = creds )
24
+ # Create the service
25
+ service = build ('sheets' , 'v4' , credentials = creds )
26
+ logger .info ("Successfully created Google Sheets service" )
12
27
13
- # Spreadsheet ID from the URL
14
- SPREADSHEET_ID = '15FMeidgU2Dg7Q4JKPkLAdJmQ3IxWCWJXjhCo9UterCE'
15
- RANGE_NAME = 'POD 5!A1:CE1000' # We'll fetch all columns and filter later
28
+ # Spreadsheet ID from the URL
29
+ SPREADSHEET_ID = '15FMeidgU2Dg7Q4JKPkLAdJmQ3IxWCWJXjhCo9UterCE'
30
+ RANGE_NAME = 'POD 5!A1:CE1000'
16
31
17
- try :
18
32
# Call the Sheets API
19
33
sheet = service .spreadsheets ()
20
34
result = sheet .values ().get (
@@ -25,37 +39,54 @@ def get_google_sheet_data():
25
39
values = result .get ('values' , [])
26
40
27
41
if not values :
28
- print ('No data found. ' )
42
+ logger . error ('No data found in the sheet ' )
29
43
return None
30
44
31
45
# Convert to DataFrame
32
- df = pd .DataFrame (values [1 :], columns = values [0 ]) # First row as headers
33
-
34
- # Select required columns
35
- required_columns = [
36
- 'Email address' ,
37
- 'Tool used' ,
38
- 'Feature Used' ,
39
- 'Context Awareness' ,
40
- 'Autonomy' ,
41
- 'Experience' ,
42
- 'Output Quality' ,
43
- 'Overall Rating' ,
44
- 'Unique ID' ,
45
- 'Pod'
46
- ]
46
+ df = pd .DataFrame (values [1 :], columns = values [0 ])
47
+ logger .info (f"Retrieved { len (df )} rows of data" )
48
+
49
+ # Log available columns for debugging
50
+ logger .debug (f"Available columns in sheet: { list (df .columns )} " )
51
+
52
+ # Map the required columns to actual column names in the sheet
53
+ column_mapping = {
54
+ 'Email Address' : 'Email address' ,
55
+ 'Tool Used' : 'Tool being used' ,
56
+ 'Feature' : 'Feature used' ,
57
+ 'Context Awareness Rating' : 'Context Awareness' ,
58
+ 'Autonomy Rating' : 'Autonomy' ,
59
+ 'Experience Rating' : 'Experience' ,
60
+ 'Output Quality Rating' : 'Output Quality' ,
61
+ 'Overall Satisfaction' : 'Overall Rating' ,
62
+ 'Unique ID' : 'Unique ID' ,
63
+ 'POD' : 'Pod'
64
+ }
65
+
66
+ # Select required columns with flexible naming
67
+ required_columns = []
68
+ for sheet_col , mapped_col in column_mapping .items ():
69
+ if sheet_col in df .columns :
70
+ df [mapped_col ] = df [sheet_col ]
71
+ required_columns .append (mapped_col )
72
+ else :
73
+ logger .warning (f"Column '{ sheet_col } ' not found in sheet" )
47
74
48
75
# Filter only required columns
49
76
filtered_df = df [required_columns ]
77
+ logger .info ("Successfully filtered required columns" )
50
78
51
79
return filtered_df
52
80
53
81
except Exception as e :
54
- print (f"An error occurred: { e } " )
82
+ logger . error (f"An error occurred: { str ( e ) } " , exc_info = True )
55
83
return None
56
84
57
85
if __name__ == "__main__" :
58
86
data = get_google_sheet_data ()
59
87
if data is not None :
60
- print ("Successfully retrieved data:" )
61
- print (data .head ()) # Display first 5 rows
88
+ logger .info ("Data retrieval successful" )
89
+ print ("\n First 5 rows of retrieved data:" )
90
+ print (data .head ())
91
+ else :
92
+ logger .error ("Failed to retrieve data" )
0 commit comments