@@ -6,27 +6,38 @@ const Logger = require('./logger.service');
66class SheetsService {
77 constructor ( ) {
88 this . spreadsheetId = process . env . SPREADSHEET_ID ;
9+ this . sheetName = process . env . CHECKIN_SHEET_NAME || "Team Check-in" ;
910 if ( ! this . spreadsheetId ) {
1011 Logger . error ( 'SPREADSHEET_ID is not set in environment variables' ) ;
1112 throw new Error ( 'SPREADSHEET_ID is not set in environment variables' ) ;
1213 }
1314
1415 // Initialize Google Sheets API
1516 this . sheets = google . sheets ( 'v4' ) ;
16-
17+
1718 // Set up authentication using the service account credentials file
1819 // The GOOGLE_APPLICATION_CREDENTIALS environment variable should point to the JSON file
1920 if ( ! process . env . GOOGLE_APPLICATION_CREDENTIALS ) {
2021 Logger . error ( 'GOOGLE_APPLICATION_CREDENTIALS is not set in environment variables' ) ;
2122 throw new Error ( 'GOOGLE_APPLICATION_CREDENTIALS is not set in environment variables' ) ;
2223 }
23-
24+
2425 this . auth = new google . auth . GoogleAuth ( {
2526 keyFile : process . env . GOOGLE_APPLICATION_CREDENTIALS ,
2627 scopes : [ 'https://www.googleapis.com/auth/spreadsheets' ]
2728 } ) ;
2829 }
2930
31+ formatSheetRange ( range ) {
32+ const safeSheetName = this . sheetName . includes ( "'" )
33+ ? this . sheetName . replace ( / ' / g, "''" )
34+ : this . sheetName ;
35+ const quotedName = / [ ^ A - Z a - z 0 - 9 _ ] / . test ( safeSheetName )
36+ ? `'${ safeSheetName } '`
37+ : safeSheetName ;
38+ return `${ quotedName } !${ range } ` ;
39+ }
40+
3041 sanitizeForSheet ( value ) {
3142 if ( typeof value !== 'string' ) {
3243 return value ;
@@ -62,15 +73,15 @@ class SheetsService {
6273 Logger . info ( 'Attempting to append check-in data to spreadsheet' ) ;
6374 Logger . info ( 'Spreadsheet ID:' , this . spreadsheetId ) ;
6475 Logger . info ( 'Google Application Credentials path:' , process . env . GOOGLE_APPLICATION_CREDENTIALS ) ;
65-
76+
6677 const authClient = await this . auth . getClient ( ) ;
6778 Logger . info ( 'Successfully obtained auth client' ) ;
68-
79+
6980 // Validate form data
7081 if ( ! formData ) {
7182 throw new Error ( 'Form data is required' ) ;
7283 }
73-
84+
7485 // Format the data for the spreadsheet
7586 const values = [ [
7687 new Date ( ) . toISOString ( ) ,
@@ -91,7 +102,7 @@ class SheetsService {
91102
92103 const request = {
93104 spreadsheetId : this . spreadsheetId ,
94- range : 'Sheet1! A:K', // Updated to include columns through K (teamId)
105+ range : this . formatSheetRange ( ' A:K') ,
95106 valueInputOption : 'USER_ENTERED' ,
96107 insertDataOption : 'INSERT_ROWS' ,
97108 resource : {
0 commit comments