去 google cloud console 上建一個專案
https://console.cloud.google.com/
接著在專案裡面搜尋 Google Drive API
接著啟用它
去憑證 (Credentials) 點選"+建立憑證" 然後,選擇 "服務帳戶"
設定完服務帳戶 ID 後可以在下方看到一組服務帳戶ID為首的email帳號
接著去服務帳戶的頁面裡面點選"金鑰>新增金鑰>選 json 格式"就會下載一個金鑰 json 檔案
記得還要啟用 google sheet api
安裝 pysheets
https://github.com/nithinmurali/pygsheets
pip install pysheets
使用 pysheets 把資料寫入 google sheet
import pygsheets
import pandas as pd
#authorization
gc = pygsheets.authorize(service_file='{這裡放金鑰json的檔案路徑}')
# Create empty dataframe
df = pd.DataFrame()
# Create a column
df['name'] = ['tao1', 'tao2', 'tao3', "tao4"]
df['age'] = [11, 12, 13, 14]
#open the google spreadsheet
sh = gc.open('{這裡寫你的google sheet的名稱}')
#select the first worksheet
wks = sh[0]
#update the first worksheet with df, starting at cell A1 i.e.(0,0)
wks.set_dataframe(df,(0,0))
細節可以查 document
https://pygsheets.readthedocs.io/en/stable/worksheet.html
我紀錄一下我常用的
wks.update_value('A1', 'XDD')
wks.update_row(4, ['tao3', 18])
start = 'A1'
end = 'B4'
res = wks.get_values(start, end)
print(type(res))
res
可以看到 output 長這樣
<class 'list'>
[['XDD', 'age'], ['tao1', '11'], ['tao2', '12'], ['tao3', '18']]
再複習一次基本的設定與連線到表單 (sheet)
import pygsheets
import pandas as pd
#authorization
gc = pygsheets.authorize(service_file='{這裡放金鑰json的檔案路徑}')
#open the google spreadsheet
sh = gc.open('{這裡寫你的google sheet的名稱}')
例如建立一個 200 row 的表單,Tab 名稱為 nba_2023
N = 200
sheet_name = "nba_2023"
sh.add_worksheet(sheet_name, rows=N)
使用 property="title" 以及 value="nba" 去找到剛剛建立的 sheet
datas = [["Irving", "Dallas Maverick", "PG"],
["Westbrook", "La Clippers", "PG"],
["Durant", "Phoenix Suns", "SF"],
["Curry", "Golden State Warriors", "PG"],
["Harden", "Philadelphia 76ers", "SG"],
["Jokić", "Denver Nuggets", "C"]]
df_new = pd.DataFrame(columns=['name', 'team', 'position'], data=datas)
ws = sh.worksheet(property="title", value="nba_2023")
ws.set_dataframe(df_new, (0,0))
目前結果
values = [["Dončić", "Dallas Maverick", "SG"],
["Davis", "LA Lakers", "PF"],
["Tatum", "Boston Celtics", "SF"]]
ws = sh.worksheet(property="title", value="nba_2023")
ws.append_table(values=values)
結果
再執行一次就會看到這樣
補充說明: 剛剛我們建立表單時設定 rows=200
當我執行完 append_table 兩次後,共新增了 6 筆資料,
此時表單的大小也會跟著長大,變成 200 + 6 = 206
報告完畢!
or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Syncing