如何使用gspread获取Google Spreadsheet单元格链接

时间:2017-06-18 00:36:18

标签: python google-sheets google-account gspread

只有一个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获取相同的单元格链接?

1 个答案:

答案 0 :(得分:-1)

首先,您需要生成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')

然后你可以抓住你想要的单元格

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

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

干杯!