package jxl.biff; import java.util.regex.Matcher; import java.util.regex.Pattern; import jxl.Cell; import jxl.CellType; import jxl.LabelCell; import jxl.Sheet; public class CellFinder { private Sheet sheet; public CellFinder(Sheet s) { this.sheet = s; } public Cell findCell(String contents, int firstCol, int firstRow, int lastCol, int lastRow, boolean reverse) { Cell cell = null; boolean found = false; int numCols = lastCol - firstCol; int numRows = lastRow - firstRow; int row1 = reverse ? lastRow : firstRow; int row2 = reverse ? firstRow : lastRow; int col1 = reverse ? lastCol : firstCol; int col2 = reverse ? firstCol : lastCol; int inc = reverse ? -1 : 1; for (int i = 0; i <= numCols && !found; i++) { for (int j = 0; j <= numRows && !found; j++) { int curCol = col1 + i * inc; int curRow = row1 + j * inc; if (curCol < this.sheet.getColumns() && curRow < this.sheet.getRows()) { Cell c = this.sheet.getCell(curCol, curRow); if (c.getType() != CellType.EMPTY) if (c.getContents().equals(contents)) { cell = c; found = true; } } } } return cell; } public Cell findCell(String contents) { Cell cell = null; boolean found = false; for (int i = 0; i < this.sheet.getRows() && !found; i++) { Cell[] row = this.sheet.getRow(i); for (int j = 0; j < row.length && !found; j++) { if (row[j].getContents().equals(contents)) { cell = row[j]; found = true; } } } return cell; } public Cell findCell(Pattern pattern, int firstCol, int firstRow, int lastCol, int lastRow, boolean reverse) { Cell cell = null; boolean found = false; int numCols = lastCol - firstCol; int numRows = lastRow - firstRow; int row1 = reverse ? lastRow : firstRow; int row2 = reverse ? firstRow : lastRow; int col1 = reverse ? lastCol : firstCol; int col2 = reverse ? firstCol : lastCol; int inc = reverse ? -1 : 1; for (int i = 0; i <= numCols && !found; i++) { for (int j = 0; j <= numRows && !found; j++) { int curCol = col1 + i * inc; int curRow = row1 + j * inc; if (curCol < this.sheet.getColumns() && curRow < this.sheet.getRows()) { Cell c = this.sheet.getCell(curCol, curRow); if (c.getType() != CellType.EMPTY) { Matcher m = pattern.matcher(c.getContents()); if (m.matches()) { cell = c; found = true; } } } } } return cell; } public LabelCell findLabelCell(String contents) { LabelCell cell = null; boolean found = false; for (int i = 0; i < this.sheet.getRows() && !found; i++) { Cell[] row = this.sheet.getRow(i); for (int j = 0; j < row.length && !found; j++) { if ((row[j].getType() == CellType.LABEL || row[j].getType() == CellType.STRING_FORMULA) && row[j].getContents().equals(contents)) { cell = (LabelCell)row[j]; found = true; } } } return cell; } }