Home > Software design >  Google Sheets - How to get multiple row data by row index
Google Sheets - How to get multiple row data by row index

Time:02-02

I'm trying to find a way using the google sheet api to fetch a particular set of rows by their index.

As an example I have a list of row indexs: 15,16,20. I would like for only these rows data to be returned.

Everything I'm trying with the range selection isn't working. Is there some other method I can use for this, or is it just not possible?

SpreadsheetsResource.ValuesResource.GetRequest getRowsRequest =
        service.Spreadsheets.Values.Get(spreadsheetId, "Inbound!{A15:B15; A16:B16}");

ValueRange getRowsResponse = getRowsRequest.Execute();
IList<IList<Object>> valuestest = getRowsResponse.Values;

The API returns a returns an error for the above call stating 'Unable to parse range'.

Quite surprised how much work it is just to filter the data in google sheet by a col value and then access just the rows needed. Rather than just pulling every row down and checking programatically!

CodePudding user response:

You can use the batchGet method instead of simply get. It works pretty much in the same way that get does but it lets you specify a list of ranges.

You could modify your code to something like:

List<string> ranges = new List<string>{"Inbound!A15:B15", "Inbound!A16:B16"};
//POPULATE THIS LIST WITH YOUR RANGES

SpreadsheetsResource.ValuesResource.BatchGetRequest 
    request = sheetsService.Spreadsheets.Values.BatchGet(spreadsheetId);

request.Ranges = ranges;

Data.BatchGetValuesResponse response = request.Execute();

You can find the documentation of the batchGet method, along with a C# example, here.

  •  Tags:  
  • Related