1212GOOGLE_PRIVATE_KEY = os .getenv ("GOOGLE_PRIVATE_KEY" , "" ).replace ('\\ n' , '\n ' )
1313
1414SCOPES = ['https://www.googleapis.com/auth/spreadsheets' ]
15- SHEET_NAME = "Sheet1 " # Assuming the sheet name is static
15+ SHEET_NAME = "Sheet2 " # Assuming the sheet name is static
1616
1717def _get_sheets_service ():
1818 """Authenticates and returns a Google Sheets service object."""
@@ -49,7 +49,8 @@ async def update_onboarding_data_in_sheet(user_email: str, onboarding_data: dict
4949 rows = result .get ('values' , [])
5050
5151 row_index = - 1
52- for i , row in enumerate (rows ):
52+ # Start from row 1 to skip header
53+ for i , row in enumerate (rows [1 :], start = 1 ):
5354 if row and row [0 ] == user_email :
5455 row_index = i
5556 break
@@ -59,7 +60,7 @@ async def update_onboarding_data_in_sheet(user_email: str, onboarding_data: dict
5960 return
6061
6162 # 2. Prepare data for batch update
62- row_number = row_index + 1
63+ row_number = row_index + 1 # +1 because we skipped header
6364
6465 # Handle location which can be a string or a dict
6566 location = onboarding_data .get ('location' , '' )
@@ -71,27 +72,45 @@ async def update_onboarding_data_in_sheet(user_email: str, onboarding_data: dict
7172 else :
7273 location = str (location )
7374
74- # Prepare a list of values to update. None will skip the cell.
75- # Columns: B (Contact), D (Location), E (Profession), F (Hobbies), H (Plan)
76- values_to_update = [
77- [
78- onboarding_data .get ('whatsapp_notifications_number' , '' ), # B
79- None , # C - Email (skip)
80- location , # D
81- onboarding_data .get ('professional-context' , '' ), # E
82- onboarding_data .get ('personal-context' , '' ), # F
83- None , # G - Insider (skip)
84- plan .capitalize () # H
85- ]
75+ # Prepare a list of update requests for different columns
76+ # New columns: A:Name, B:Contact, C:Email, D:Location, E:Profession, F:Working Hours, G:Key People, H:Personal Context, I:Insider, J:Plan
77+ data_to_update = [
78+ # A: Name
79+ {
80+ 'range' : f"{ SHEET_NAME } !A{ row_number } " ,
81+ 'values' : [[onboarding_data .get ('user-name' , '' )]]
82+ },
83+ # B: Contact
84+ {
85+ 'range' : f"{ SHEET_NAME } !B{ row_number } " ,
86+ 'values' : [[onboarding_data .get ('whatsapp_notifications_number' , '' )]]
87+ },
88+ # D: Location, E: Profession, F: Working Hours, G: Key People, H: Personal Context
89+ {
90+ 'range' : f"{ SHEET_NAME } !D{ row_number } :H{ row_number } " ,
91+ 'values' : [[
92+ location ,
93+ onboarding_data .get ('professional-context' , '' ),
94+ onboarding_data .get ('working-hours' , '' ),
95+ onboarding_data .get ('key-people' , '' ),
96+ onboarding_data .get ('personal-context' , '' )
97+ ]]
98+ },
99+ # J: Plan
100+ {
101+ 'range' : f"{ SHEET_NAME } !J{ row_number } " ,
102+ 'values' : [[plan .capitalize ()]]
103+ }
86104 ]
87105
88- # 3. Update the sheet row from column B to H
89- range_to_update = f"{ SHEET_NAME } !B{ row_number } :H{ row_number } "
90- service .spreadsheets ().values ().update (
106+ # 3. Update the sheet using batchUpdate to avoid overwriting unrelated columns
107+ body = {
108+ 'valueInputOption' : 'USER_ENTERED' ,
109+ 'data' : data_to_update
110+ }
111+ service .spreadsheets ().values ().batchUpdate (
91112 spreadsheetId = GOOGLE_SHEET_ID ,
92- range = range_to_update ,
93- valueInputOption = 'USER_ENTERED' ,
94- body = {'values' : values_to_update }
113+ body = body
95114 ).execute ()
96115 logger .info (f"Successfully updated onboarding data for { user_email } in Google Sheet." )
97116
@@ -139,9 +158,9 @@ async def get_user_properties_from_sheet(user_email: str) -> dict:
139158 }
140159
141160 try :
142- # Read columns C (Email), G (Insider), H (Plan)
143- # Reading C:H is safer to avoid index out of bounds if rows have fewer columns
144- range_to_read = f"{ SHEET_NAME } !C:H "
161+ # Read columns C (Email), I (Insider), J (Plan)
162+ # Reading C:J is safer to avoid index out of bounds if rows have fewer columns
163+ range_to_read = f"{ SHEET_NAME } !C:J "
145164 result = service .spreadsheets ().values ().get (spreadsheetId = GOOGLE_SHEET_ID , range = range_to_read ).execute ()
146165 rows = result .get ('values' , [])
147166
@@ -150,9 +169,9 @@ async def get_user_properties_from_sheet(user_email: str) -> dict:
150169 "plan_type" : "free" # Default to free
151170 }
152171
153- # Column C is index 0 in our `rows` array. G is index 4, H is index 5 .
154- relative_insider_index = 4
155- relative_plan_index = 5
172+ # Column C is index 0 in our `rows` array. I is index 6, J is index 7 .
173+ relative_insider_index = 6
174+ relative_plan_index = 7
156175
157176 for row in rows [1 :]: # Skip header row
158177 if not row or len (row ) == 0 :
0 commit comments