【问题标题】:Java | How to find a value (row and column number) in range of google spreadsheet using API?爪哇 |如何使用 API 在谷歌电子表格范围内查找值(行号和列号)?
【发布时间】:2016-08-26 01:25:45
【问题描述】:

我正在使用 Java API (V4) 从谷歌表格中读取和编辑数据。到目前为止,我能够根据行号和列号(索引)读取和编辑数据。

  • 我想做的是通过数据的特定值来本地化数据(我想使用它的值来获取特定单元格的行号和列号)。

到目前为止,这是我用来编辑数据的工作代码的一部分:

  // Copy the format from A1:C1 and paste it into A2:C5, so the data in 
// each column has the same background.             
requests.add(new Request()
                        .setCopyPaste(new CopyPasteRequest()
                                .setSource(new GridRange()
                                        .setSheetId(0)
                                        .setStartRowIndex(0)
                                        .setEndRowIndex(1)
                                        .setStartColumnIndex(0)
                                        .setEndColumnIndex(3))
                                .setDestination(new GridRange()
                                        .setSheetId(0)
                                        .setStartRowIndex(1)
                                        .setEndRowIndex(6)
                                        .setStartColumnIndex(0)
                                        .setEndColumnIndex(3))
                                .setPasteType("PASTE_FORMAT")));

                BatchUpdateSpreadsheetRequest batchUpdateRequest = new BatchUpdateSpreadsheetRequest()
                        .setRequests(requests);

谁能帮帮我? 提前谢谢你。

【问题讨论】:

    标签: java api find google-spreadsheet-api google-sheets-api


    【解决方案1】:

    查找文本尚不适用于 Google Sheet API。请对此issue tracker FR 加注星标以获取有关任何更新的通知。

    基于此 SO answer 的解决方法,“您可以获取您正在搜索的范围的数据,然后对其进行迭代以寻找匹配项。” 这是他的代码sn-p:

    /**
    * Finds a value within a given range. 
    * @param value The value to find.
    * @param range The range to search in.
    * @return A range pointing to the first cell containing the value, 
    * or null if not found.
    */
    function find(value, range) {
    var data = range.getValues();
    for (var i = 0; i < data.length; i++) {
    for (var j = 0; j < data[i].length; j++) {
    if (data[i][j] == value) {
    return range.getCell(i + 1, j + 1);
    }
    }
    }
    return null;
    }
    

    注意:代码示例在 google-apps-script 中

    就像在这段代码 sn-p 中一样,您需要先设置范围,然后检查值是否在范围内匹配。

    【讨论】:

      【解决方案2】:

      根据documentation,您可以使用FindReplaceRequest().getFind(); 找到值

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

      【讨论】:

      • @user6889641 你能举个简单的例子,因为我检查了响应不包含找到的字符串。如果请求中提到了替换,它只返回数字。根据developers.google.com/sheets/api/guides/batchupdate 示例,它仅适用于替换字符串。如果找到字符串,我希望获取整个 ROW。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-21
      相关资源
      最近更新 更多