我正在使用 Java API (V4) 读取和编辑 Google 工作表中的数据。到目前为止,我可以根据行号和列号(索引)读取和编辑数据。
- 我想要做的是通过数据的特定值来本地化数据(我想使用其值获取特定单元格的行号和列号)。
到目前为止,这是我用来编辑数据的工作代码的一部分:
// 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);
有人可以帮我吗?
最佳答案
Google Sheet API 尚不支持查找文本。请为此issue tracker FR加注星标收到任何更新通知。
基于此 SO 的解决方法 answer ,“您可以获得正在搜索的范围的数据,然后对其进行迭代以查找匹配项。” 这是他的代码片段:
/**
* 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
就像在此代码片段中一样,您需要先设置范围,然后检查值是否在范围内匹配。
关于java - 如何使用 API 在 Google 电子表格范围内查找值(行号和列号)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39157055/