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.") |
|||
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:Example.png | center]] | ||
+ | |||
+ | 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. | ||
+ | |||
+ | <source lang='java' high='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}} |
Revision as of 03:58, 20 April 2016
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.