Reputation: 2184
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
Upvotes: 30
Views: 25738
Reputation: 21
If you are a little confused after @Bardy's answer (like me), then next line could help you (to get proper title of the Sheet):
response.getSheets().get(0).getProperties().getTitle();
So, about topic, you can firstly get your Spreadsheet:
Spreadsheet response = service
.spreadsheets()
.get(spreadsheetId)
.execute();
And then get ValueRange this way:
ValueRange response1 = service.spreadsheets().values()
.get(spreadsheetId,response.getSheets().get(0).getProperties().getTitle())
.execute();
(Assuming you got only 1 sheet, otherwise try to debug/print all sheets with
response.getSheets()
and then find the title of sheet you are using.
Upvotes: 2
Reputation: 49
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'])
Upvotes: 2