访问 Google 电子表格 API,使用筛选条件获取数据。

huangapple go评论114阅读模式
英文:

Accessing google spreadsheet api to fetch data with filter

问题

我正在尝试使用Java从Google电子表格中获取特定名称的数据。我可以使用Google API从列中获取完整的数据。我例如将范围指定为"sheet1!B:B"。然而,我不明白:

  1. 如何从多列中获取数据。我应该如何传递范围?
  2. 我如何为列指定数据筛选器。

任何指导都将不胜感激。我在GitHub或Google电子表格API文档中找不到相关信息。请帮帮我。

目前我有从Google电子表格中获取所有值的代码,如下所示:

String spreadsheetId = "电子表格ID";

String range = "Sheet1!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();
英文:

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();

答案1

得分: 2

以下是翻译好的部分:

让我们来看一下 Google 提供的官方示例此处

访问 Google 电子表格 API,使用筛选条件获取数据。

  1. 如何从多列中获取数据。我应该如何传递范围?

这是范围:

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

这意味着从单元格 A2 到表格 F 列的数据都会被获取,该表格的名称为 Class Data

  1. 如何为列指定数据筛选器。

一旦您获取了所有数据,您可以在普通的 for 循环中对其进行筛选。例如,如果我们只想要第 1 列和第 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));
}

输出将会是:

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

更多信息请参阅官方 Java 快速入门指南

英文:

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

访问 Google 电子表格 API,使用筛选条件获取数据。

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

This is the range:

String range = &quot;Class Data!A2:F&quot;;

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

> 2. 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&lt;List&lt;Object&gt;&gt; values = request.execute().getValues();
System.out.println(&quot;Name, Gender&quot;);
for (List row : values) {
    System.out.println(row.get(0)+&quot;, &quot;+row.get(1));
}

The output will be:

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

More info on the official Java Quickstart.

答案2

得分: 0

你可以尝试使用RestedBox来通过API访问你的Google Sheets和本地电子表格数据,包括查询操作。我之前在一些概念验证中使用它,而不是使用数据库。

英文:

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.

huangapple
  • 本文由 发表于 2020年9月2日 10:09:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63697718.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定