2424import java .io .IOException ;
2525import java .nio .charset .StandardCharsets ;
2626import java .security .GeneralSecurityException ;
27+ import java .util .Base64 ;
2728import java .util .Collections ;
2829import java .util .List ;
2930
@@ -34,19 +35,27 @@ public class GoogleApiService {
3435 private static final Logger logger = LoggerFactory .getLogger (GoogleApiService .class );
3536 private static final String APPLICATION_NAME = "Recruit Form" ;
3637 private static final JsonFactory JSON_FACTORY = JacksonFactory .getDefaultInstance ();
38+
3739 @ Value ("${google.credentials.json}" )
3840 private String credentialsJson ;
3941 private Sheets sheetsService ;
4042
4143 // Google Sheets API 서비스 객체를 생성하는 메소드
42- private Sheets getSheetsService () throws IOException , GeneralSecurityException {
43- if (sheetsService == null ){
44+ private Sheets getSheetsService () throws IOException , GeneralSecurityException {
45+ if (sheetsService == null ) {
46+ byte [] decodedBytes = Base64 .getDecoder ().decode (credentialsJson );
47+ String credentialsJson = new String (decodedBytes , StandardCharsets .UTF_8 );
48+
49+ System .out .println (credentialsJson ); // 디코딩된 JSON 출력
50+
4451 ByteArrayInputStream credentialsStream = new ByteArrayInputStream (credentialsJson .getBytes (StandardCharsets .UTF_8 ));
45- GoogleCredentials credentials = GoogleCredentials
46- .fromStream (credentialsStream )
52+
53+ GoogleCredentials credentials = GoogleCredentials .fromStream (credentialsStream )
4754 .createScoped (Collections .singletonList ("https://www.googleapis.com/auth/spreadsheets" ));
4855
49- sheetsService = new Sheets .Builder (GoogleNetHttpTransport .newTrustedTransport (), JSON_FACTORY , new HttpCredentialsAdapter (credentials ))
56+ sheetsService = new Sheets .Builder (GoogleNetHttpTransport .newTrustedTransport (),
57+ JSON_FACTORY ,
58+ new HttpCredentialsAdapter (credentials ))
5059 .setApplicationName (APPLICATION_NAME )
5160 .build ();
5261 }
@@ -72,4 +81,44 @@ public void writeToSheet(String spreadsheetId, String range, List<List<Object>>
7281 }
7382 }
7483
84+ // 지원자 ID를 기준으로 행번호 찾기
85+ public int findRowByApplicantId (String spreadsheetId , String sheetName , Long applicantId ) {
86+ try {
87+ Sheets service = getSheetsService ();
88+ String range = sheetName + "!A:A" ;
89+ ValueRange response = service .spreadsheets ().values ()
90+ .get (spreadsheetId , range )
91+ .execute ();
92+ List <List <Object >> values = response .getValues ();
93+ if (values != null ) {
94+ for (int i = 0 ; i < values .size (); i ++) {
95+ List <Object > row = values .get (i );
96+ if (!row .isEmpty () && row .get (0 ).toString ().equals (applicantId .toString ())) {
97+ return i + 1 ;
98+ }
99+ }
100+ }
101+ return values == null ? 1 : values .size () + 1 ;
102+ } catch (IOException | GeneralSecurityException e ) {
103+ logger .error ("Failed to find row by applicant id" , e );
104+ throw new CustomException (ErrorCode .INTERNAL_SERVER_ERROR );
105+ }
106+ }
107+
108+
109+ public void updateSheet (String spreadsheetId , String range , List <List <Object >> values ) {
110+ try {
111+ Sheets service = getSheetsService ();
112+ ValueRange body = new ValueRange ().setValues (values );
113+ UpdateValuesResponse response = service .spreadsheets ().values ()
114+ .update (spreadsheetId , range , body )
115+ .setValueInputOption ("USER_ENTERED" )
116+ .execute ();
117+ logger .info ("Updated rows: {}" , response .getUpdatedRows ());
118+ } catch (IOException | GeneralSecurityException e ) {
119+ logger .error ("Failed to update data in the spreadsheet" , e );
120+ throw new CustomException (ErrorCode .WRITE_FAIL );
121+ }
122+ }
123+
75124}
0 commit comments