Java. 从 Spreadsheet 对象中获取值(列表的列表)

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

Java. Get value from Spreadsheet object (list of lists)

问题

我使用 CellData 获取到一个电子表格对象

var request = service.spreadsheets().get(spreadsheetId);
request.set("fields", "sheets/data/rowData/values/note");
request.setRanges(ranges);
var spreadsheet = request.execute();
var resultString = spreadsheet.toString();

返回的对象如下图所示:

Java. 从 Spreadsheet 对象中获取值(列表的列表)

我需要从中获取 note 的值。

toString() 方法返回:

{
  "sheets": [
    {
      "data": [
        {
          "rowData": [
            {
              "values": [
                {
                  "note": "test note"
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}

我可以通过在字符串中搜索的方式获取值,但有没有更正确的方法呢?

英文:

I'm getting a spreadsheet object with CellData

	    var request = service.spreadsheets().get(spreadsheetId);
		request.set("fields", "sheets/data/rowData/values/note");
		request.setRanges(ranges);
		var spreadsheet = request.execute();
		var resultString = spreadsheet.toString();

The object returned looks like this:

Java. 从 Spreadsheet 对象中获取值(列表的列表)

I need to get just a value of note from this.

toString() method returns

{"sheets":[{"data":[{"rowData":[{"values":[{"note":"test note"}]}]}]}]}

I obviously can get value by just searching in that string, but what is the correct way of doing this?

答案1

得分: 1

这个 API 调用的响应是 电子表格资源,在 Java 库中,由 Spreadsheet 类 管理。

Spreadsheet 类有一个 getSheets() 方法,它返回一个 Sheets 列表。由于你只有一个表格,获取列表中的第一个元素(第一个工作表),并使用相应的方法获取工作表的 GridData

RowDataCellData 重复相同的过程,直到通过 getNote() 获取到 note

代码示例:

Spreadsheet spreadsheet = request.execute();
Sheet sheet = spreadsheet.getSheets().get(0);
GridData data = sheet.getData().get(0);
RowData rowData = data.getRowData().get(0);
CellData cellData = rowData.getValues().get(0);
String note = cellData.getNote();

注意事项:

  • 基于您提供的信息,我假设每个列表中只有一个元素(即,您只想检索单个工作表中的单个单元格)。如果情况不是这样的,你应该通过循环访问相应的列表,而不是仅仅访问其第一个元素。
  • 不要忘记导入所有这些类的模块。
英文:

The response to this API call is the Spreadsheet resource which, in the Java library, is managed by the Class Spreadsheet.

The Spreadsheet class has a getSheets() method, which returns a list of Sheets. Since you only have one sheet, get the first element in the spreadsheet (the first sheet), and use the corresponding method to get the sheet's GridData.

Repeat the same process for RowData and CellData until you reach the note via getNote():

Code example:

Spreadsheet spreadsheet = request.execute();
Sheet sheet = spreadsheet.getSheets().get(0);
GridData data = sheet.getData().get(0);
RowData rowData = data.getRowData().get(0);
CellData cellData = rowData.getValues().get(0);
String note = cellData.getNote();

Notes:

  • Based on the information you provided, I assumed you only have one element in each of the lists (that is, you only want to retrieve a single cell in a single sheet). If that's not the case, you should loop through the corresponding list instead of accessing its first element.
  • Don't forget to import the modules for all these classes.

huangapple
  • 本文由 发表于 2020年8月27日 15:51:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63611485.html
匿名

发表评论

匿名网友

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

确定