Java | 如何使用API​​在Google电子表格范围内查找值(行号和列号)?

我正在使用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); 

谁能帮帮我吗? 先感谢您。

根据文档 ,您可以使用以下FindReplaceRequest()查找值: FindReplaceRequest().getFind();

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

Google Sheet API尚未提供查找文字。 请为此问题跟踪器FR加注星标以获得有关任何更新的通知。

基于这个SO 答案的解决方法,“您可以获取您正在搜索的范围的数据,然后迭代它以寻找匹配。”这是他的代码片段:

 /** * 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中

与此代码段中一样,您需要先设置范围,然后检查值是否在范围内匹配。