我可以从电子表格和工作表ID中获取数据源。我想要获取每个单元格的数据。即,我可以从工作表中获取数据源。现在,我需要从每个单元格获取数据(字符串类型?)以进行比较和输入。我该如何做到这一点呢?
还有一个值得一看的电子表格库: gspread。我曾经使用过上面提到的Google数据库,对我来说提供的API很奇怪。你需要提取电子表格的密钥才能开始使用该电子表格。
这里有一种更简单的方法。如果您需要从单元格中获取数据,只需打开工作表并获取值:
g = gspread.login('your@gmail.com', 'password')
worksheet = g.open('name_of_the_spreadsheet').get_worksheet(0)
# Say, you need A2
val = worksheet.cell(2, 1).value
# And then update
worksheet.update_cell(2, 1, '42')
谷歌数据API有一个Python绑定,其中包括电子表格:http://code.google.com/apis/spreadsheets/data/1.0/developers_guide_python.html
gspread可能是开始此过程的最快方法,但是使用gspread从您的本地主机更新数据存在一些速度限制。如果您正在使用gspread移动大量数据 - 例如将20列数据移动到一列中,您可能需要使用CRON作业自动化该过程。
还有另一个电子表格库:pygsheets。它类似于gspread,但使用的是Google API v4,因此提供了更多选项。
import pygsheets
gc = pygsheets.authorize()
# Open spreadsheet and then workseet
sh = gc.open('my new ssheet')
wks = sh.sheet1
# Update a cell with value (just to let him know values is updated ;) )
wks.update_cell('A1', "Hey yank this numpy array")
# update the sheet with array
wks.update_cells('A2', my_nparray.to_list())
# share the sheet with your friend
sh.share("myFriend@gmail.com")