# Google Sheet API
Git URL: https://github.com/sos418/GoogleSheetDemo
## Google Sheet API 筆記
需先申請Google API權限
[Google Oauth 2.0](https://developers.google.com/identity/protocols/oauth2) 使用者需登入授權。
[Google Oauth Service Account](https://developers.google.com/identity/protocols/oauth2/service-account) 可以省略登入步驟,適合server to server。
### Sheet API - Java
1. 取得 credential。 **DataStoreFactory** 屬於記憶體空間,用來存放credentials,另一個選擇是 **FileDataStoreFactory** 使用檔案儲存credentials。
2. 使用Sheet API取得 **Spreadsheets** 物件。
3. 使用 spreadsheets.values 集合進行讀取、寫入動作。**ValueRange**物件代表取得的值,屬於Java物件的lists of lists,對應表格的行或欄。
## 程式架構
* **Controller**
* GoogleSheetController
* **GET** /googleSheet?range=A1
從範例表單中讀取資料,參數range需給值
* **POST** /createSheet
創建表單,會回傳sheet ID
* **PUT** /updateSheet?spreadsheetId=1BX0xBTvtMnOPP33gcukteqwWpqT5Fpz6PWvCQ_qmg7I
更新表單內容,參數需給sheet ID,目前是將更新的值寫死在程式
* **Service**
* GoogleSheetService
* 取得的Credential,去呼叫Google Sheet API service。
* **Config**
* GoogleAuthorizaConfig
* 載入google oauth credentials設定
* 建立Oauth Code Flow,使用者需登入,登入後回設定的Callback。
* 將取得的tokens放到資料夾內儲存。
## Demo
0. start application: ./gradlew bootRun
1. http://localhost:8080/googleSheet?range=A1:E

範圍有值回傳200 + 取得的值
2. http://localhost:8080/googleSheet?range=G1

範圍沒值回傳204
3. http://localhost:8080/createSheet

回傳建立的sheet ID
4. http://localhost:8080/updateSheet?spreadsheetId=1BX0xBTvtMnOPP33gcukteqwWpqT5Fpz6PWvCQ_qmg7I

參數帶上一步產生的sheet ID,塞入值到excel.

## Reference
https://www.baeldung.com/google-sheets-java-client
https://developers.google.com/sheets/api/quickstart/java
{"metaMigratedAt":"2023-06-15T11:48:05.685Z","metaMigratedFrom":"Content","title":"Google Sheet API","breaks":true,"contributors":"[{\"id\":\"e9f11c2a-f0dd-4086-b260-093ced450a30\",\"add\":2473,\"del\":634}]"}