23
votes

I would like to read a Google Spreadsheet like described in the Java Quickstart

https://developers.google.com/sheets/quickstart/java

The Quickstart explaines how to read data from a give range

.....
String range = "Class Data!A2:E";
ValueRange response = service.spreadsheets().values()
        .get(spreadsheetId, range)
        .execute();

List<List<Object>> values = response.getValues();
....

But I have the problem that I do not know the Range of the Spreadsheet. The number of columns can change. So how can I read for example all the data of the Sheet without knowing the range?

Regards

Michael

2

2 Answers

57
votes

You can specify the whole Sheet as just : String range = "Class Data";, for a Sheet named Class Data. You can see the output on the Try it part of the spreadsheets.values.get reference.

0
votes

For those who want to get data with the Spread Sheet ID (can be obtained from the URL) but not the sheet name, the following code snippet might be useful. It prints out all the sheet names in found under the spreadsheet. You can change the print statement to whatever you need.

spreadsheet = service.spreadsheets().get(spreadsheetId=SHEET_ID, includeGridData=True).execute()
    for sheet in spreadsheet['sheets']:
        print(sheet['properties']['title'])