1
votes

Google Sheets API v4:

Python Quickstart shows how to get data from a Google Spreadsheet:

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

See https://developers.google.com/sheets/quickstart/python

Now, I have resolved the syntax for Batch Get:

I have tried the following but get null for 'values':

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])
2
Can you post your code or snippet like authorization and the main code that uses the spreadsheets.values.batchUpdate/batchGet, and the error logs too. Thanks!Mr.Rebot

2 Answers

0
votes

I'm not good with python but I met same problem with java client and hope this solution will help. 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. Please, make a note to this setRange() invokaion:

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

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. However, the result is a dict, not objects:

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