Find among Cells"
From Documentation
(Created page with " Find a text among cells is a common function. Component doesn't have this built-in function, but we can easily build our own with ZSS API.") |
m (correct highlight (via JWB)) |
||
(2 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
+ | {{ZKSpreadsheetEssentials3PageHeader}} | ||
− | Find a text among cells is a common function. | + | |
+ | |||
+ | Find a text among cells is a common function. ZK Spreadsheet doesn't have this built-in function, but we can easily build our own with ZSS API. | ||
+ | |||
+ | [[File:Zss-essentials-find-dialog.png | center]] | ||
+ | |||
+ | We list the key method here. For complete code, please refer to the [[ZK_Spreadsheet_Essentials/Download_Example_Source_Code | example code project]]. This method starts finding from the next cell by rows of the current selection. If nothing found, return the current selection. When reaching the end of a sheet, it does not find from the beginning. | ||
+ | |||
+ | <source lang='java' highlight='9, 17, 18'> | ||
+ | protected Range findNext(Sheet sheet, Range currentSelection) { | ||
+ | int lastColumn = Ranges.range(sheet).getDataRegion().getLastColumn(); | ||
+ | int lastRow = Ranges.range(sheet).getDataRegion().getLastRow(); | ||
+ | String keyword = keywordBox.getValue().trim(); | ||
+ | int row = getStartingRow(sheet, currentSelection); | ||
+ | int column = getStartingColumn(sheet, currentSelection); | ||
+ | while (row <= lastRow){ | ||
+ | while (column <= lastColumn){ | ||
+ | Range cell = Ranges.range(sheet, row, column); | ||
+ | if (cell.getCellData().getType() == CellType.STRING){ | ||
+ | if (match(cell.getCellData().getEditText(), keyword)){ | ||
+ | return cell; | ||
+ | } | ||
+ | } | ||
+ | column++; | ||
+ | } | ||
+ | column = 0; | ||
+ | row++; | ||
+ | } | ||
+ | return currentSelection; | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | {{ZKSpreadsheetEssentialsPageFooter}} |
Latest revision as of 12:50, 19 January 2022
Find a text among cells is a common function. ZK Spreadsheet doesn't have this built-in function, but we can easily build our own with ZSS API.
We list the key method here. For complete code, please refer to the example code project. This method starts finding from the next cell by rows of the current selection. If nothing found, return the current selection. When reaching the end of a sheet, it does not find from the beginning.
protected Range findNext(Sheet sheet, Range currentSelection) {
int lastColumn = Ranges.range(sheet).getDataRegion().getLastColumn();
int lastRow = Ranges.range(sheet).getDataRegion().getLastRow();
String keyword = keywordBox.getValue().trim();
int row = getStartingRow(sheet, currentSelection);
int column = getStartingColumn(sheet, currentSelection);
while (row <= lastRow){
while (column <= lastColumn){
Range cell = Ranges.range(sheet, row, column);
if (cell.getCellData().getType() == CellType.STRING){
if (match(cell.getCellData().getEditText(), keyword)){
return cell;
}
}
column++;
}
column = 0;
row++;
}
return currentSelection;
}
All source code listed in this book is at Github.