use of org.apache.poi.ss.formula.ptg.RefPtg in project poi by apache.
the class TestFormulaParser method testZeroRowRefs.
/**
* Zero is not a valid row number so cell references like 'A0' are not valid.
* Actually, they should be treated like defined names.
* <br/>
* In addition, leading zeros (on the row component) should be removed from cell
* references during parsing.
*/
@Test
public void testZeroRowRefs() throws IOException {
// bad because zero is not a valid row number
String badCellRef = "B0";
// this should get parsed as "B1"
String leadingZeroCellRef = "B000001";
HSSFWorkbook wb = new HSSFWorkbook();
try {
HSSFFormulaParser.parse(badCellRef, wb);
fail("Identified bug 47312b - Shouldn't be able to parse cell ref '" + badCellRef + "'.");
} catch (FormulaParseException e) {
// expected during successful test
confirmParseException(e, "Specified named range '" + badCellRef + "' does not exist in the current workbook.");
}
Ptg[] ptgs;
try {
ptgs = HSSFFormulaParser.parse(leadingZeroCellRef, wb);
assertEquals("B1", ((RefPtg) ptgs[0]).toFormulaString());
} catch (FormulaParseException e) {
confirmParseException(e, "Specified named range '" + leadingZeroCellRef + "' does not exist in the current workbook.");
// close but no cigar
fail("Identified bug 47312c - '" + leadingZeroCellRef + "' should parse as 'B1'.");
}
// create a defined name called 'B0' and try again
Name n = wb.createName();
n.setNameName("B0");
n.setRefersToFormula("1+1");
ptgs = HSSFFormulaParser.parse("B0", wb);
confirmTokenClasses(ptgs, NamePtg.class);
wb.close();
}
use of org.apache.poi.ss.formula.ptg.RefPtg in project poi by apache.
the class TestFormulaParser method test57196_Formula.
@Test
public void test57196_Formula() throws IOException {
HSSFWorkbook wb = new HSSFWorkbook();
Ptg[] ptgs = HSSFFormulaParser.parse("DEC2HEX(HEX2DEC(O8)-O2+D2)", wb, FormulaType.CELL, -1);
assertNotNull("Ptg array should not be null", ptgs);
confirmTokenClasses(ptgs, // ??
NameXPtg.class, // ??
NameXPtg.class, // O8
RefPtg.class, // HEX2DEC
FuncVarPtg.class, // O2
RefPtg.class, SubtractPtg.class, // D2
RefPtg.class, AddPtg.class, // DEC2HEX
FuncVarPtg.class);
RefPtg o8 = (RefPtg) ptgs[2];
FuncVarPtg hex2Dec = (FuncVarPtg) ptgs[3];
RefPtg o2 = (RefPtg) ptgs[4];
RefPtg d2 = (RefPtg) ptgs[6];
FuncVarPtg dec2Hex = (FuncVarPtg) ptgs[8];
assertEquals("O8", o8.toFormulaString());
assertEquals(255, hex2Dec.getFunctionIndex());
//assertEquals("", hex2Dec.toString());
assertEquals("O2", o2.toFormulaString());
assertEquals("D2", d2.toFormulaString());
assertEquals(255, dec2Hex.getFunctionIndex());
wb.close();
}
use of org.apache.poi.ss.formula.ptg.RefPtg in project poi by apache.
the class TestCFRuleRecord method testReserializeRefNTokens.
/**
* tRefN and tAreaN tokens must be preserved when re-serializing conditional format formulas
*/
@Test
public void testReserializeRefNTokens() {
RecordInputStream is = TestcaseRecordInputStream.create(CFRuleRecord.sid, DATA_REFN);
CFRuleRecord rr = new CFRuleRecord(is);
Ptg[] ptgs = rr.getParsedExpression1();
assertEquals(3, ptgs.length);
if (ptgs[0] instanceof RefPtg) {
throw new AssertionFailedError("Identified bug 45234");
}
assertEquals(RefNPtg.class, ptgs[0].getClass());
RefNPtg refNPtg = (RefNPtg) ptgs[0];
assertTrue(refNPtg.isColRelative());
assertTrue(refNPtg.isRowRelative());
byte[] data = rr.serialize();
TestcaseRecordInputStream.confirmRecordEncoding(CFRuleRecord.sid, DATA_REFN, data);
}
use of org.apache.poi.ss.formula.ptg.RefPtg in project poi by apache.
the class EmbeddedObjectRefSubRecord method readRefPtg.
private static Ptg readRefPtg(byte[] formulaRawBytes) {
LittleEndianInput in = new LittleEndianInputStream(new ByteArrayInputStream(formulaRawBytes));
byte ptgSid = in.readByte();
switch(ptgSid) {
case AreaPtg.sid:
return new AreaPtg(in);
case Area3DPtg.sid:
return new Area3DPtg(in);
case RefPtg.sid:
return new RefPtg(in);
case Ref3DPtg.sid:
return new Ref3DPtg(in);
}
return null;
}
use of org.apache.poi.ss.formula.ptg.RefPtg in project poi by apache.
the class TestTextObjectRecord method testLinkFormula.
public void testLinkFormula() {
RecordInputStream is = new RecordInputStream(new ByteArrayInputStream(linkData));
is.nextRecord();
TextObjectRecord rec = new TextObjectRecord(is);
Ptg ptg = rec.getLinkRefPtg();
assertNotNull(ptg);
assertEquals(RefPtg.class, ptg.getClass());
RefPtg rptg = (RefPtg) ptg;
assertEquals("T2", rptg.toFormulaString());
byte[] data2 = rec.serialize();
assertEquals(linkData.length, data2.length);
assertArrayEquals(linkData, data2);
}
Aggregations