使用JAVA表格API识别Google表格中的单元格类型(公式,列表等)

时间:2019-03-06 18:51:48

标签: java google-sheets-api

下面的代码检索特定范围内的单元格的字符串表示形式。 我要实现的是获取一个单元格对象并标识值类型,如果它是公式,那么它是公式,如果它是列出从etc取值的位置。

    ValueRange response = service.spreadsheets().values()
            .get(spreadsheetId, range)
            .execute();
    List<List<Object>> values = response.getValues();
    if (values == null || values.isEmpty()) {
        System.out.println("No data found.");
    } else {
        for (List row : values) {
            System.out.println(row);
        }
    }

1 个答案:

答案 0 :(得分:0)

想通了如何做... 我只需要使用setIncludeGridData(true)调用getValues()

    Sheets.Spreadsheets.Get get = service.spreadsheets().get(spreadsheetId).setIncludeGridData(true);
    Spreadsheet sheet = get.execute();
    GridData gridData = sheet.getSheets().get(0).getData().get(0);
    List<RowData> rowData = gridData.getRowData();
    System.out.println("Found " + rowData.size() + " rows in sheet range");

然后,我可以遍历行数据,并在其中获取所需的所有信息,例如:

  {
    "effectiveFormat": {
      "backgroundColor": {
        "blue": 1.0,
        "green": 1.0,
        "red": 1.0
      },
      "horizontalAlignment": "RIGHT",
      "hyperlinkDisplayType": "PLAIN_TEXT",
      "padding": {
        "bottom": 2,
        "left": 3,
        "right": 3,
        "top": 2
      },
      "textFormat": {
        "bold": false,
        "fontFamily": "arial,sans,sans-serif",
        "fontSize": 10,
        "foregroundColor": {

        },
        "italic": false,
        "strikethrough": false,
        "underline": false
      },
      "verticalAlignment": "BOTTOM",
      "wrapStrategy": "OVERFLOW_CELL"
    },
    "effectiveValue": {
      "numberValue": 122.0
    },
    "formattedValue": "122",
    "userEnteredValue": {
      "formulaValue": "=SUM(A2:C2)"
    }
  }