Ananya
Ananya

Reputation: 21

Accessing google spreadsheet api to fetch data with filter

I am trying to fetch data with specific name from a google spreadsheet using java. I am able to fetch complete data from a column using google api. I specify range as "sheet1!B:B" for example. However, I am not understanding

  1. How to fetch data from multiple columns. How should I pass the range?
  2. How do I specify the data filter for columns.

Any guidance would be really appreciated. I am unable to find it in github or googlespreadsheet api docs. Please help me out

Currently I have code to fetch all values from googlespread sheet using below code

String spreadsheetId="spreadseet id";

String range="Sheet!A2:zz";

Sheets service = new Sheets.Builder(httpTransport, JSON_FACTORY, credential).setApplicationName(APPLICATION_NAME).build();

Sheets.Spreadsheets.Values.Get request=service.spreadsheets().values().get(spreadsheetId, range);

ValueRange response = request.execute();

Upvotes: 2

Views: 935

Answers (2)

Simran
Simran

Reputation: 369

You can try using RestedBox to access your Google Sheets and local spreadsheet data through an API, including querying. I've been using it for some POC's instead of a database.

Upvotes: 0

Marc
Marc

Reputation: 3265

Let's take the official example provided by google here:

enter image description here

  1. How to fetch data from multiple columns. How should I pass the range?

This is the range:

String range = "Class Data!A2:F";

Which means fetch all the data from the cell A2 to the column F of the sheet with the name Class Data.

  1. How do I specify the data filter for columns.

Once you have all the data you can filter it in a normal for-loop. For example if we want only the column 1 and 2:

List<List<Object>> values = request.execute().getValues();
System.out.println("Name, Gender");
for (List row : values) {
    System.out.println(row.get(0)+", "+row.get(1));
}

The output will be:

Name, Gender
Alexandra, Female
Andrew, Male
Anna, Female

More info on the official Java Quickstart.

Upvotes: 2

Related Questions