1

I have the following code using the Google sheets API and got it working perfectly. It sees all my data and puts it into the app perfectly via the for loop. It runs through the spreadsheets and puts the data in an edit view.

private List<String> getDataFromApi() throws IOException {
    String spreadsheetId = "1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms";
    String range = "Class Data!A2:E";
    List<String> results = new ArrayList<String>();
    ValueRange response = this.mService.spreadsheets().values()
        .get(spreadsheetId, range)
        .execute();
    List<List<Object>> values = response.getValues();
    if (values != null) {
        results.add("Name, Major");
        for (List row : values) {
            results.add(row.get(0) + ", " + row.get(4));
        }
    }
    return results;
}

As you can see, the for loop retrieves the data one by one until there is no more data inside a specific range in the spreadsheet.

My question: I want to retrieve a specific range, not using a for loop. However, as an example, if I want to get cell A1, and I do result.add(A.get(0)), it doesn't work.

How can I retrieve a specific range within the spreadsheet.

Sabuncu
  • 5,095
  • 5
  • 55
  • 89
Jacques Celliers
  • 51
  • 1
  • 1
  • 8
  • The for loop works perfectly it retrieves the rows one by one and puts it in an edit view, i didn't change it. what i want to do is get just one specific cell in my spreadsheet not with a for loop. i want to retrieve just one cell. my question is how can i achieve this? – Jacques Celliers Jan 07 '17 at 09:35
  • I have re-edited your question based on your comment. However, you still need to clarify what you mean when you say "it doesn't work". What happens? Do you not get the result at all, or do you get a different range? Do you get a specific error? – Sabuncu Jan 07 '17 at 10:31
  • I get an error android studio does not like it when i enter that code – Jacques Celliers Jan 07 '17 at 11:11
  • Please post that exact error in place of the words where you say "it doesn't work". – Sabuncu Jan 07 '17 at 12:22
  • Based from this [documentation](https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/get), to retrieve only subsets of the spreadsheet, use the [`ranges`](https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/get#body.QUERY_PARAMETERS.ranges) URL parameter. Limiting the range will return only the portions of the spreadsheet that intersect the requested ranges. Ranges are specified using A1 notation. You may also refer with this [related SO question](http://stackoverflow.com/questions/37331756). Hope this helps! – abielita Jan 08 '17 at 15:52

0 Answers0