use of org.apache.poi.hssf.usermodel.HSSFEvaluationWorkbook in project poi by apache.
the class TestEventWorkbookBuilder method testFormulas.
public void testFormulas() {
FormulaRecord[] fRecs = mockListen.getFormulaRecords();
// Check our formula records
assertEquals(6, fRecs.length);
InternalWorkbook stubWB = listener.getStubWorkbook();
assertNotNull(stubWB);
HSSFWorkbook stubHSSF = listener.getStubHSSFWorkbook();
assertNotNull(stubHSSF);
// Check these stubs have the right stuff on them
assertEquals("Sheet1", stubWB.getSheetName(0));
assertEquals("Sheet1", stubHSSF.getSheetName(0));
assertEquals("S2", stubWB.getSheetName(1));
assertEquals("S2", stubHSSF.getSheetName(1));
assertEquals("Sh3", stubWB.getSheetName(2));
assertEquals("Sh3", stubHSSF.getSheetName(2));
// Check we can get the formula without breaking
for (FormulaRecord fRec : fRecs) {
HSSFFormulaParser.toFormulaString(stubHSSF, fRec.getParsedExpression());
}
// Peer into just one formula, and check that
// all the ptgs give back the right things
Ptg[] ptgs = fRecs[0].getParsedExpression();
assertEquals(1, ptgs.length);
assertTrue(ptgs[0] instanceof Ref3DPtg);
Ref3DPtg ptg = (Ref3DPtg) ptgs[0];
HSSFEvaluationWorkbook book = HSSFEvaluationWorkbook.create(stubHSSF);
assertEquals("Sheet1!A1", ptg.toFormulaString(book));
// Now check we get the right formula back for
// a few sample ones
FormulaRecord fr;
// Sheet 1 A2 is on same sheet
fr = fRecs[0];
assertEquals(1, fr.getRow());
assertEquals(0, fr.getColumn());
assertEquals("Sheet1!A1", HSSFFormulaParser.toFormulaString(stubHSSF, fr.getParsedExpression()));
// Sheet 1 A5 is to another sheet
fr = fRecs[3];
assertEquals(4, fr.getRow());
assertEquals(0, fr.getColumn());
assertEquals("'S2'!A1", HSSFFormulaParser.toFormulaString(stubHSSF, fr.getParsedExpression()));
// Sheet 1 A7 is to another sheet, range
fr = fRecs[5];
assertEquals(6, fr.getRow());
assertEquals(0, fr.getColumn());
assertEquals("SUM(Sh3!A1:A4)", HSSFFormulaParser.toFormulaString(stubHSSF, fr.getParsedExpression()));
// Now, load via Usermodel and re-check
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("3dFormulas.xls");
assertEquals("Sheet1!A1", wb.getSheetAt(0).getRow(1).getCell(0).getCellFormula());
assertEquals("SUM(Sh3!A1:A4)", wb.getSheetAt(0).getRow(6).getCell(0).getCellFormula());
}
use of org.apache.poi.hssf.usermodel.HSSFEvaluationWorkbook in project poi by apache.
the class TestArea3DPtg method testToFormulaString.
/**
* confirms that sheet names get properly escaped
*/
public void testToFormulaString() {
Area3DPtg target = new Area3DPtg("A1:B1", (short) 0);
String sheetName = "my sheet";
HSSFWorkbook wb = createWorkbookWithSheet(sheetName);
HSSFEvaluationWorkbook book = HSSFEvaluationWorkbook.create(wb);
assertEquals("'my sheet'!A1:B1", target.toFormulaString(book));
wb.setSheetName(0, "Sheet1");
assertEquals("Sheet1!A1:B1", target.toFormulaString(book));
wb.setSheetName(0, "C64");
assertEquals("'C64'!A1:B1", target.toFormulaString(book));
}
use of org.apache.poi.hssf.usermodel.HSSFEvaluationWorkbook in project poi by apache.
the class TestDec2Hex method createContext.
private OperationEvaluationContext createContext() {
HSSFWorkbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet();
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("123.43");
cell = row.createCell(1);
cell.setCellValue("8");
cell = row.createCell(2);
cell.setCellValue("-8");
HSSFEvaluationWorkbook workbook = HSSFEvaluationWorkbook.create(wb);
WorkbookEvaluator workbookEvaluator = new WorkbookEvaluator(workbook, new IStabilityClassifier() {
@Override
public boolean isCellFinal(int sheetIndex, int rowIndex, int columnIndex) {
return true;
}
}, null);
OperationEvaluationContext ctx = new OperationEvaluationContext(workbookEvaluator, workbook, 0, 0, 0, null);
return ctx;
}
use of org.apache.poi.hssf.usermodel.HSSFEvaluationWorkbook in project poi by apache.
the class TestRef3DPtg method testToFormulaString.
public void testToFormulaString() {
Ref3DPtg target = new Ref3DPtg("A1", (short) 0);
HSSFWorkbook wb = createWorkbookWithSheet("my sheet");
HSSFEvaluationWorkbook book = HSSFEvaluationWorkbook.create(wb);
assertEquals("'my sheet'!A1", target.toFormulaString(book));
wb.setSheetName(0, "ProfitAndLoss");
assertEquals("ProfitAndLoss!A1", target.toFormulaString(book));
wb.setSheetName(0, "profit+loss");
assertEquals("'profit+loss'!A1", target.toFormulaString(book));
}
Aggregations