简体   繁体   English

Python Google表格API v4:如何批量获取和批量更新Google表格?

[英]Python Google Sheets API v4: How to do Batch Get and Batch Update Google sheets?

Google Sheets API v4: Google表格API v4:

Python Quickstart shows how to get data from a Google Spreadsheet: Python快速入门显示了如何从Google电子表格中获取数据:

result = service.spreadsheets().values()
         .get(spreadsheetId=spreadsheetId, range=rangeName).execute()
values = result.get('values', [])

See https://developers.google.com/sheets/quickstart/python 请参阅https://developers.google.com/sheets/quickstart/python

Now, I have resolved the syntax for Batch Get: 现在,我已经解决了Batch Get的语法:

I have tried the following but get null for 'values': 我尝试过以下内容,但对'values'获取null:

rangeName = ["Class Data!A2:E2","Class Data!A3:E3"]
result = service.spreadsheets().values().batchGet(
         spreadsheetId=spreadsheetId, ranges=(rangeName)).execute()
for r in result['valueRanges']:
    print (r.values()[0],r.values()[1])

I'm not good with python but I met same problem with java client and hope this solution will help. 我对python不好,但我遇到了与java客户端相同的问题,希望这个解决方案能有所帮助。 I tried to load ranges only with 我试着只加载范围

service.spreadsheets().values().batchGet
                        (spreadsheetID).execute();

as documentation said, but there was no results. 正如文件所述,但没有结果。 When I found a way - to make batchGet you should specify list of ranges which you want to get from spreadsheet. 当我找到一种方法 - 进行batchGet时,您应该指定要从电子表格中获取的范围列表。 Please, make a note to this setRange() invokaion: 请注意这个setRange() invokaion:

final ArrayList<String> RANGES = new ArrayList<>();
        RANGES.add("Sheet1");
        RANGES.add("Sheet2");
BatchGetValuesResponse response = service.spreadsheets().values().batchGet
                    (spreadsheetID).setRanges(RANGES).execute();

If anyone finds this on Google, the answer is (like NoisyFlasher implied with that Java example, and like the OP was on the track to) to simply provide the ranges as a Python list. 如果有人在Google上找到这个,那么答案是(就像NoisyFlasher暗示的那个Java示例,就像OP在轨道上一样)来简单地将范围作为Python列表提供。 However, the result is a dict, not objects: 但是,结果是dict,而不是对象:

rangeName = ["Class Data!A2:E2","Class Data!A3:E3"]
result = service.spreadsheets().values().batchGet(
         spreadsheetId=spreadsheetId, ranges=rangeName).execute()
for r in result['valueRanges']:
    print (r['values'])

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

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