简体   繁体   English

将JSON请求转换为Java Google Sheet API V4

[英]Translate JSON request to Java Google sheet API V4

In google Sheet API guide , 在Google Sheet API指南中

I read that a find request (find the cell with value 'X') can be formatted as follows in JSON: 我读到可以在JSON中将find请求(使用值'X'查找单元格)格式化为以下格式:

{
  "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.
}

requests can be made in the form of this URL https://sheets.googleapis.com/v4/spreadsheets/spreadsheetId ....... 可以使用以下URL的形式发出请求:https: //sheets.googleapis.com/v4/spreadsheets/spreadsheetId .......

But I'm not able to translate that to JAVA code 但我无法将其转换为JAVA代码

The closest I've found was this code in one of the forums: 我找到的最接近的代码是其中一个论坛的代码:

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

which lack a response that I expect from the API, letting me know which cell in the sheet has the value I'm trying to find. 缺少我希望从API获得的响应,让我知道工作表中的哪个单元格具有我要查找的值。

thanks 谢谢

It looks like you only partially copied the example from the Guide on this page: https://developers.google.com/sheets/api/guides/batchupdate#example 看来您只是从此页面上的指南中部分复制了该示例: https : //developers.google.com/sheets/api/guides/batchupdate#example

You need to place the FindReplaceRequest inside a BatchUpdateSpreadsheetRequest (in the requests field) and send it in a Spreadsheets.BatchUpdate call. 您需要将FindReplaceRequest放置在BatchUpdateSpreadsheetRequest (在requests字段中),并通过Spreadsheets.BatchUpdate调用发送它。

The full example is: 完整的示例是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM