use of org.apache.poi.ss.formula.ptg.Ref3DPtg in project poi by apache.
the class InternalWorkbook method cloneFilter.
public NameRecord cloneFilter(int filterDbNameIndex, int newSheetIndex) {
NameRecord origNameRecord = getNameRecord(filterDbNameIndex);
// copy original formula but adjust 3D refs to the new external sheet index
int newExtSheetIx = checkExternSheet(newSheetIndex);
Ptg[] ptgs = origNameRecord.getNameDefinition();
for (int i = 0; i < ptgs.length; i++) {
Ptg ptg = ptgs[i];
if (ptg instanceof Area3DPtg) {
Area3DPtg a3p = (Area3DPtg) ((OperandPtg) ptg).copy();
a3p.setExternSheetIndex(newExtSheetIx);
ptgs[i] = a3p;
} else if (ptg instanceof Ref3DPtg) {
Ref3DPtg r3p = (Ref3DPtg) ((OperandPtg) ptg).copy();
r3p.setExternSheetIndex(newExtSheetIx);
ptgs[i] = r3p;
}
}
NameRecord newNameRecord = createBuiltInName(NameRecord.BUILTIN_FILTER_DB, newSheetIndex + 1);
newNameRecord.setNameDefinition(ptgs);
newNameRecord.setHidden(true);
return newNameRecord;
}
use of org.apache.poi.ss.formula.ptg.Ref3DPtg in project poi by apache.
the class OperationEvaluationContext method getExternalNameXEval.
private ValueEval getExternalNameXEval(ExternalName externName, String workbookName) {
try {
// Fetch the workbook this refers to, and the name as defined with that
WorkbookEvaluator refWorkbookEvaluator = _bookEvaluator.getOtherWorkbookEvaluator(workbookName);
EvaluationName evaluationName = refWorkbookEvaluator.getName(externName.getName(), externName.getIx() - 1);
if (evaluationName != null && evaluationName.hasFormula()) {
if (evaluationName.getNameDefinition().length > 1) {
throw new RuntimeException("Complex name formulas not supported yet");
}
// Need to evaluate the reference in the context of the other book
OperationEvaluationContext refWorkbookContext = new OperationEvaluationContext(refWorkbookEvaluator, refWorkbookEvaluator.getWorkbook(), -1, -1, -1, _tracker);
Ptg ptg = evaluationName.getNameDefinition()[0];
if (ptg instanceof Ref3DPtg) {
Ref3DPtg ref3D = (Ref3DPtg) ptg;
return refWorkbookContext.getRef3DEval(ref3D);
} else if (ptg instanceof Ref3DPxg) {
Ref3DPxg ref3D = (Ref3DPxg) ptg;
return refWorkbookContext.getRef3DEval(ref3D);
} else if (ptg instanceof Area3DPtg) {
Area3DPtg area3D = (Area3DPtg) ptg;
return refWorkbookContext.getArea3DEval(area3D);
} else if (ptg instanceof Area3DPxg) {
Area3DPxg area3D = (Area3DPxg) ptg;
return refWorkbookContext.getArea3DEval(area3D);
}
}
return ErrorEval.REF_INVALID;
} catch (WorkbookNotFoundException wnfe) {
return ErrorEval.REF_INVALID;
}
}
use of org.apache.poi.ss.formula.ptg.Ref3DPtg in project poi by apache.
the class NameRecord method getExternSheetNumber.
/** gets the extern sheet number
* @return extern sheet index
*/
public int getExternSheetNumber() {
Ptg[] tokens = field_13_name_definition.getTokens();
if (tokens.length == 0) {
return 0;
}
Ptg ptg = tokens[0];
if (ptg.getClass() == Area3DPtg.class) {
return ((Area3DPtg) ptg).getExternSheetIndex();
}
if (ptg.getClass() == Ref3DPtg.class) {
return ((Ref3DPtg) ptg).getExternSheetIndex();
}
return 0;
}
use of org.apache.poi.ss.formula.ptg.Ref3DPtg 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.Ref3DPtg 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());
}
Aggregations