When evaluating a name in another workbook, need to use an OperationEvaluationContext tied to that Workbook, and do so such that XSSF references work too

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1613154 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2014-07-24 14:56:23 +00:00
parent f06915e95d
commit fec4eb7a12
2 changed files with 16 additions and 11 deletions

View File

@ -377,25 +377,31 @@ public final class OperationEvaluationContext {
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;
int sheetIndex = refWorkbookEvaluator.getSheetIndexByExternIndex(ref3D.getExternSheetIndex());
String sheetName = refWorkbookEvaluator.getSheetName(sheetIndex);
SheetRefEvaluator sre = createExternSheetRefEvaluator(workbookName, sheetName);
return new LazyRefEval(ref3D.getRow(), ref3D.getColumn(), sre);
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;
int sheetIndex = refWorkbookEvaluator.getSheetIndexByExternIndex(area3D.getExternSheetIndex());
String sheetName = refWorkbookEvaluator.getSheetName(sheetIndex);
SheetRefEvaluator sre = createExternSheetRefEvaluator(workbookName, sheetName);
return new LazyAreaEval(area3D.getFirstRow(), area3D.getFirstColumn(), area3D.getLastRow(), area3D.getLastColumn(), sre);
return refWorkbookContext.getArea3DEval(area3D);
} else if(ptg instanceof Area3DPxg){
Area3DPxg area3D = (Area3DPxg)ptg;
return refWorkbookContext.getArea3DEval(area3D);
}
}
return ErrorEval.REF_INVALID;

View File

@ -156,9 +156,8 @@ public final class TestXSSFFormulaEvaluation extends BaseTestFormulaEvaluator {
// Evaluate and check results
assertEquals("\"Hello!\"", evaluator.evaluate(cXSLX_cell).formatAsString());
// TODO Fix XSSF reference evaluations to work
// assertEquals("\"Test A1\"", evaluator.evaluate(cXSLX_sNR).formatAsString());
// assertEquals("142.0", evaluator.evaluate(cXSLX_gNR).formatAsString());
assertEquals("\"Test A1\"", evaluator.evaluate(cXSLX_sNR).formatAsString());
assertEquals("142.0", evaluator.evaluate(cXSLX_gNR).formatAsString());
assertEquals("\"Hello!\"", evaluator.evaluate(cXSL_cell).formatAsString());
assertEquals("\"Test A1\"", evaluator.evaluate(cXSL_sNR).formatAsString());