在Google表格API guide中,

我读到find请求(找到值为'X'的单元格)可以用JSON格式如下:

{
  "find": string,
  "replacement": string,
  "matchCase": boolean,
  "matchEntireCell": boolean,
  "searchByRegex": boolean,
  "includeFormulas": boolean,

  // Union field scope can be only one of the following:
  "range": {
    object(GridRange)
  },
  "sheetId": number,
  "allSheets": boolean,
  // End of list of possible types for union field scope.
}


可以使用此URL的形式进行请求
https://sheets.googleapis.com/v4/spreadsheets/spreadsheetId .......

但我无法将其转换为JAVA代码

我找到的最接近的代码是其中一个论坛的代码:

    requests.add(new Request().setFindReplace(new FindReplaceRequest().setFind(entry.getKey())
            .setMatchEntireCell(true)
            .setMatchCase(true)
            .setReplacement(entry.getValue())
            .setRange(new GridRange()
                    .setSheetId(0)
                    .setStartRowIndex(row)
                    .setEndRowIndex(row + 1))));
}



String cellReq = (new FindReplaceRequest().setFind("samuel")).getFind();


缺少我希望从API获得的响应,让我知道工作表中的哪个单元格具有我要查找的值。

谢谢

最佳答案

看来您只是从此页面上的指南中部分复制了示例:https://developers.google.com/sheets/api/guides/batchupdate#example

您需要将FindReplaceRequest放在BatchUpdateSpreadsheetRequest内(在requests字段中),并在Spreadsheets.BatchUpdate调用中发送它。

完整的示例是:

List<Request> requests = new ArrayList<>();
// Change the spreadsheet's title.
requests.add(new Request()
        .setUpdateSpreadsheetProperties(new UpdateSpreadsheetPropertiesRequest()
                .setProperties(new SpreadsheetProperties()
                        .setTitle(title))
                .setFields("title")));
// Find and replace text.
requests.add(new Request()
        .setFindReplace(new FindReplaceRequest()
                .setFind(find)
                .setReplacement(replacement)
                .setAllSheets(true)));
// Add additional requests (operations) ...

BatchUpdateSpreadsheetRequest body =
        new BatchUpdateSpreadsheetRequest().setRequests(requests);
BatchUpdateSpreadsheetResponse response =
        service.spreadsheets().batchUpdate(spreadsheetId, body).execute();
FindReplaceResponse findReplaceResponse = response.getReplies().get(1).getFindReplace();
System.out.printf("%d replacements made.", findReplaceResponse.getOccurrencesChanged());

关于java - 将JSON请求转换为Java Google Sheet API V4,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47329124/

10-11 22:28
查看更多