Find among Cells"

From Documentation
m (correct highlight (via JWB))
 
(One intermediate revision by the same user not shown)
Line 5: Line 5:
 
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.
 
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]]
+
[[File:Zss-essentials-find-dialog.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.
+
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' high='9, 17, 18'>
+
<source lang='java' highlight='9, 17, 18'>
 
protected Range findNext(Sheet sheet, Range currentSelection) {
 
protected Range findNext(Sheet sheet, Range currentSelection) {
 
int lastColumn = Ranges.range(sheet).getDataRegion().getLastColumn();
 
int lastColumn = Ranges.range(sheet).getDataRegion().getLastColumn();

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.

Zss-essentials-find-dialog.png

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.


Last Update : 2022/01/19

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.