Skip to main content
 首页 » 编程设计

python之如何使用 gspread 获取 Google 电子表格单元格链接

2025年05月04日8lonelyxmas

只有一个 Google 电子表格文档。 从右键单击下拉菜单中选择第一个 A1 单元格后,我选择:Get Link to this Cell 命令。

URL 链接现在存储在内存中,可以粘贴到任何文本编辑器中。复制的单元格的 URL 链接如下所示:

https://docs.google.com/spreadsheets/d/f8s9HO0sidw9qIGwuqYq1wFFb9QWWpuFIXE3flYcgBG1/edit?ts=7559594f#gid=2879414198&range=A1

问题:如何在 Python 中使用 gspread 获取同一个单元格的链接?

请您参考如下方法:

首先,您需要生成 OAuth2 凭据: http://gspread.readthedocs.io/en/latest/oauth2.html

您需要使用 OAuth2 凭据在 Python 中创建一个新的授权对象:

gc = gspread.authorize(credentials) 

然后您需要创建到工作表的连接:

# If you want to be specific, use a key (which can be extracted from 
# the spreadsheet's url) 
yoursheet = gc.open_by_key('0BmgG6nO_6dprdS1MN3d3MkdPa142WFRrdnRRUWl1UFE') 
 
# Or, if you feel really lazy to extract that key, paste the entire url 
yoursheet = gc.open_by_url('https://docs.google.com/spreadsheet/ccc?key=0Bm...FE&hl') 

然后就可以抓取你想要的cell了

val = yoursheet.acell('B1').value 

所有代码都是从 gspread Github 的基本用法中提取的,稍作调整 https://github.com/burnash/gspread

干杯!