2

In the v3 API I could do something like:

String listFeedUrl = (new URI(worksheet.getListFeedUrl().toString())).toURL();
ListQuery lq = new ListQuery(listFeedUrl);

lq.setOrderBy("column:name");
lq.setReverse(false);

ListFeed lf = spreadsheetService.query(lq, ListFeed.class);

In v4 I'm not seeing an equivalent. What is the recommended way to get sorted cell data? Is my only choice to sort it once it's returned?

Jaime
  • 1,182
  • 2
  • 12
  • 29

2 Answers2

3

In fact you can do this, but the idea is to execute a request on the sheet to explicitly order the columns. I've managed to do it like this :

BatchUpdateSpreadsheetRequest busReq = new BatchUpdateSpreadsheetRequest();
SortSpec ss = new SortSpec();
// ordering ASCENDING or DESCENDING
ss.setSortOrder("DESCENDING");
// the column number starting from 0
ss.setDimensionIndex(1);
SortRangeRequest srr = new SortRangeRequest();
srr.setSortSpecs(Arrays.asList(ss));
Request req = new Request();
req.setSortRange(srr);
busReq.setRequests(Arrays.asList(req));
// mService is a instance of com.google.api.services.sheets.v4.Sheets
this.mService.spreadsheets().batchUpdate(spreadsheetId, busReq).execute();
k4cy
  • 322
  • 3
  • 8
  • Also you can use this URL to test the request construction : [developers.google.com/apis-explorer](https://developers.google.com/apis-explorer/#p/sheets/v4/sheets.spreadsheets.batchUpdate) – k4cy Feb 22 '17 at 15:11
1

It seem the answer is NO. See here

Order by column is not supported for reads, but it is possible to sort the data in the sheet (using a SortRange) request and then read it
Jaime
  • 1,182
  • 2
  • 12
  • 29