public RowRecord getRow(int rowIndex) {
int maxrow = SpreadsheetVersion.EXCEL97.getLastRowIndex();
if (rowIndex < 0 || rowIndex > maxrow) {
- throw new IllegalArgumentException("The row number must be between 0 and " + maxrow);
+ throw new IllegalArgumentException("The row number must be between 0 and " + maxrow + ", but had: " + rowIndex);
}
return _rowRecords.get(Integer.valueOf(rowIndex));
}
// Calculate Offset from the start of a DBCellRecord to the first Row
rv.visitRecord(dbcrBuilder.build(pos));
}
- for (int i=0; i< _unknownRecords.size(); i++) {
+ for (Record _unknownRecord : _unknownRecords) {
// Potentially breaking the file here since we don't know exactly where to write these records
- rv.visitRecord(_unknownRecords.get(i));
+ rv.visitRecord(_unknownRecord);
}
}
public boolean isRowGroupCollapsed(int row) {
int collapseRow = findEndOfRowOutlineGroup(row) + 1;
- if (getRow(collapseRow) == null) {
- return false;
- }
- return getRow( collapseRow ).getColapsed();
+ return getRow(collapseRow) != null && getRow(collapseRow).getColapsed();
}
public void expandRow(int rowNumber) {
- int idx = rowNumber;
- if (idx == -1)
+ if (rowNumber == -1)
return;
// If it is already expanded do nothing.
- if (!isRowGroupCollapsed(idx)) {
+ if (!isRowGroupCollapsed(rowNumber)) {
return;
}
// Find the start of the group.
- int startIdx = findStartOfRowOutlineGroup(idx);
+ int startIdx = findStartOfRowOutlineGroup(rowNumber);
RowRecord row = getRow(startIdx);
// Find the end of the group.
- int endIdx = findEndOfRowOutlineGroup(idx);
+ int endIdx = findEndOfRowOutlineGroup(rowNumber);
// expand:
// collapsed bit must be unset
// to look at the start and the end of the current group to determine which
// is the enclosing group
// hidden bit only is altered for this outline level. ie. don't un-collapse contained groups
- if (!isRowGroupHiddenByParent(idx)) {
+ if (!isRowGroupHiddenByParent(rowNumber)) {
for (int i = startIdx; i <= endIdx; i++) {
RowRecord otherRow = getRow(i);
if (row.getOutlineLevel() == otherRow.getOutlineLevel() || !isRowGroupCollapsed(i)) {
* @deprecated use {@link #getCellValueIterator()} instead
*/
public CellValueRecordInterface[] getValueRecords() {
+ //noinspection deprecation
return _valuesAgg.getValueRecords();
}
public abstract class Ptg {
public static final Ptg[] EMPTY_PTG_ARRAY = { };
-
/**
* Reads <tt>size</tt> bytes of the input stream, to create an array of <tt>Ptg</tt>s.
* Extra data (beyond <tt>size</tt>) may be read if and <tt>ArrayPtg</tt>s are present.
*/
public static int getEncodedSize(Ptg[] ptgs) {
int result = 0;
- for (int i = 0; i < ptgs.length; i++) {
- result += ptgs[i].getSize();
+ for (Ptg ptg : ptgs) {
+ result += ptg.getSize();
}
return result;
}
*/
public static int getEncodedSizeWithoutArrayData(Ptg[] ptgs) {
int result = 0;
- for (int i = 0; i < ptgs.length; i++) {
- Ptg ptg = ptgs[i];
+ for (Ptg ptg : ptgs) {
if (ptg instanceof ArrayPtg) {
result += ArrayPtg.PLAIN_TOKEN_SIZE;
} else {
* @return number of bytes written
*/
public static int serializePtgs(Ptg[] ptgs, byte[] array, int offset) {
- int nTokens = ptgs.length;
-
LittleEndianByteArrayOutputStream out = new LittleEndianByteArrayOutputStream(array, offset);
List<Ptg> arrayPtgs = null;
- for (int k = 0; k < nTokens; k++) {
- Ptg ptg = ptgs[k];
-
+ for (Ptg ptg : ptgs) {
ptg.write(out);
if (ptg instanceof ArrayPtg) {
if (arrayPtgs == null) {
}
}
if (arrayPtgs != null) {
- for (int i=0;i<arrayPtgs.size();i++) {
- ArrayPtg p = (ArrayPtg)arrayPtgs.get(i);
+ for (Ptg arrayPtg : arrayPtgs) {
+ ArrayPtg p = (ArrayPtg) arrayPtg;
p.writeTokenValueBytes(out);
}
}
public abstract boolean isBaseToken();
public static boolean doesFormulaReferToDeletedCell(Ptg[] ptgs) {
- for (int i = 0; i < ptgs.length; i++) {
- if (isDeletedCellRef(ptgs[i])) {
+ for (Ptg ptg : ptgs) {
+ if (isDeletedCellRef(ptg)) {
return true;
}
}
return false;
}
+
private static boolean isDeletedCellRef(Ptg ptg) {
if (ptg == ErrPtg.REF_INVALID) {
return true;
package org.apache.poi.ss.formula.eval;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.fail;
+import org.apache.poi.hssf.HSSFTestDataSamples;
+import org.apache.poi.hssf.usermodel.*;
+import org.apache.poi.ss.usermodel.*;
+import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
-import org.apache.poi.hssf.HSSFTestDataSamples;
-import org.apache.poi.hssf.usermodel.HSSFCell;
-import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
-import org.apache.poi.hssf.usermodel.HSSFRow;
-import org.apache.poi.hssf.usermodel.HSSFSheet;
-import org.apache.poi.hssf.usermodel.HSSFWorkbook;
-import org.apache.poi.ss.usermodel.Cell;
-import org.apache.poi.ss.usermodel.CellValue;
-import org.apache.poi.ss.usermodel.FormulaEvaluator;
-import org.apache.poi.ss.usermodel.Row;
-import org.apache.poi.ss.usermodel.Sheet;
-import org.apache.poi.ss.usermodel.Workbook;
-import org.junit.Test;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
/**
* Miscellaneous tests for bugzilla entries.<p/> The test name contains the
InputStream is = HSSFTestDataSamples.openSampleFileStream("27349-vlookupAcrossSheets.xls");
// original bug may have thrown exception here,
// or output warning to stderr
- HSSFWorkbook wb = new HSSFWorkbook(is);
+ Workbook wb = new HSSFWorkbook(is);
- HSSFSheet sheet = wb.getSheetAt(0);
- HSSFRow row = sheet.getRow(1);
- HSSFCell cell = row.getCell(0);
+ Sheet sheet = wb.getSheetAt(0);
+ Row row = sheet.getRow(1);
+ Cell cell = row.getCell(0);
// this definitely would have failed due to 27349
assertEquals("VLOOKUP(1,'DATA TABLE'!$A$8:'DATA TABLE'!$B$10,2)", cell
.getCellFormula());
// We might as well evaluate the formula
- HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
+ FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator();
CellValue cv = fe.evaluate(cell);
- assertEquals(HSSFCell.CELL_TYPE_NUMERIC, cv.getCellType());
+ assertEquals(Cell.CELL_TYPE_NUMERIC, cv.getCellType());
assertEquals(3.0, cv.getNumberValue(), 0.0);
wb.close();
*/
@Test
public void test27405() throws Exception {
-
- HSSFWorkbook wb = new HSSFWorkbook();
- HSSFSheet sheet = wb.createSheet("input");
+ Workbook wb = new HSSFWorkbook();
+ Sheet sheet = wb.createSheet("input");
// input row 0
- HSSFRow row = sheet.createRow(0);
- HSSFCell cell = row.createCell(0);
- cell = row.createCell(1);
+ Row row = sheet.createRow(0);
+ /*Cell cell =*/ row.createCell(0);
+ Cell cell = row.createCell(1);
cell.setCellValue(1); // B1
// input row 1
row = sheet.createRow(1);
// }
// use POI's evaluator as an extra sanity check
- HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
+ FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator();
CellValue cv;
cv = fe.evaluate(cell);
- assertEquals(HSSFCell.CELL_TYPE_NUMERIC, cv.getCellType());
+ assertEquals(Cell.CELL_TYPE_NUMERIC, cv.getCellType());
assertEquals(1.0, cv.getNumberValue(), 0.0);
cv = fe.evaluate(row.getCell(1));
- assertEquals(HSSFCell.CELL_TYPE_BOOLEAN, cv.getCellType());
+ assertEquals(Cell.CELL_TYPE_BOOLEAN, cv.getCellType());
assertEquals(true, cv.getBooleanValue());
wb.close();
*/
@Test
public void test42448() throws IOException {
- HSSFWorkbook wb = new HSSFWorkbook();
- HSSFSheet sheet1 = wb.createSheet("Sheet1");
+ Workbook wb = new HSSFWorkbook();
+ Sheet sheet1 = wb.createSheet("Sheet1");
- HSSFRow row = sheet1.createRow(0);
- HSSFCell cell = row.createCell(0);
+ Row row = sheet1.createRow(0);
+ Cell cell = row.createCell(0);
// it's important to create the referenced sheet first
- HSSFSheet sheet2 = wb.createSheet("A"); // note name 'A'
+ Sheet sheet2 = wb.createSheet("A"); // note name 'A'
// TODO - POI crashes if the formula is added before this sheet
// RuntimeException("Zero length string is an invalid sheet name")
// Excel doesn't crash but the formula doesn't work until it is
double expectedResult = (4.0 * 8.0 + 5.0 * 9.0) / 10.0;
- HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
+ FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator();
CellValue cv = fe.evaluate(cell);
- assertEquals(HSSFCell.CELL_TYPE_NUMERIC, cv.getCellType());
+ assertEquals(Cell.CELL_TYPE_NUMERIC, cv.getCellType());
assertEquals(expectedResult, cv.getNumberValue(), 0.0);
wb.close();
}
- private static void addCell(HSSFSheet sheet, int rowIx, int colIx,
+ private static void addCell(Sheet sheet, int rowIx, int colIx,
double value) {
sheet.createRow(rowIx).createCell(colIx).setCellValue(value);
}