why_vincent why_vincent - 1 month ago 8
Java Question

Can read from sample sheet but now from own sheet

I'm trying out Google Sheets API from a Java application. I've accessed the file mentioned in the tutorial but I cannot access any file that I have created on my own.

This is the code I'm using:

String spreadsheetId = /*omitted*/;
String range = "Class Data!A1:B";
ValueRange response = service.spreadsheets().values()
.get(spreadsheetId, range)
.execute();
List<List<Object>> values = response.getValues();


I created a spreadsheet manually in Drive, filled A1:B with strings and copied the id from the URL, looking something like "1IeoY5jY3Su86x1uvgc1yJqEEU-6dd6FdUKo8Yf5J73k" (not an actual ID).

This generates an error 400 Unable to parse range: Class Data!A1:B

I'm guessing that this means that it cannot access my spreadsheet, since the sheet is filled with strings "ab" in these cells.

The sample code has a spreadsheetId that refers to some kind of public document and it is working for that document. I am guessing that I am doing something fundamentally wrong here. I have verified that the document is created with the same credentials as the ones I'm using for the Java application. Any ideas?

Answer

Class Data is the name of the sheet in the sample sheet. You should substitute that with the name of the sheet in your spreadsheet. You can see the name of the sheet on the tab near the bottom of the screen. It defaults to Sheet 1.

A1:B is the number of rows and columns to read. It is saying to read all data in columns A and B from rows 1 to the last row. You should swap that with the rows and columns you want to read in your sheet.