简体   繁体   English

如何编写python脚本来操纵谷歌电子表格数据

[英]How to write a python script to manipulate google spreadsheet data

I am able to get the feed from the spreadsheet and worksheet ID. 我可以从电子表格和工作表ID中获取Feed。 I want to capture the data from each cell. 我想从每个单元格中捕获数据。

ie, I am able to get the feed from the worksheet. 即,我能够从工作表中获取提要。 Now I need to get data(string type?) from each of the cells to make a comparison and for input. 现在我需要从每个单元格中获取数据(字符串类型?)以进行比较和输入。

How exactly can I do that? 我怎么能这样做?

There's another spreadsheet library worth to look at: gspread. 还有另一个值得关注的电子表格库 :gspread。 I've used Google's data libary mentioned above and to me the provided api is weird. 我使用了上面提到的谷歌数据库,对我来说,提供的api很奇怪。 You need to extract spreadsheet's key to start working with this spreadsheet. 您需要提取电子表格的密钥才能开始使用此电子表格。

Here it's a way simpler. 这是一种更简单的方式。 If you need to fetch the data from a cell you can just open a worksheet and get the value: 如果需要从单元格中获取数据,只需打开工作表并获取值:

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') 

Google数据API具有Python绑定,包括电子表格: http//code.google.com/apis/spreadsheets/data/1.0/developers_guide_python.html

gspread is probably the fastest way to begin this process, however there are some speed limitations on updating data using gspread from your localhost. gspread可能是开始此过程的最快方法,但是使用来自localhost的gspread更新数据存在一些速度限制。 If you're moving large sets of data with gspread - for instance moving 20 columns of data over a column, you may want to automate the process using a CRON job. 如果您使用gspread移动大量数据 - 例如,在列上移动20列数据,您可能希望使用CRON作业自动执行该过程。

There's another spreadsheet library : pygsheets . 还有另一个电子表格库: pygsheets Its similar to gspread, but uses google api v4. 它类似于gspread,但使用google api v4。 Hence provides more options. 因此提供了更多选择。

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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM