View | Details | Raw Unified | Return to bug 48996
Collapse All | Expand All

(-)src/java/org/apache/poi/hssf/usermodel/HSSFEvaluationWorkbook.java (+5 lines)
Lines 38-43 Link Here
38
 * Internal POI use only
38
 * Internal POI use only
39
 *
39
 *
40
 * @author Josh Micich
40
 * @author Josh Micich
41
 * @author Stephen Wolke (smwolke at geistig.com)
41
 */
42
 */
42
public final class HSSFEvaluationWorkbook implements FormulaRenderingWorkbook, EvaluationWorkbook, FormulaParsingWorkbook {
43
public final class HSSFEvaluationWorkbook implements FormulaRenderingWorkbook, EvaluationWorkbook, FormulaParsingWorkbook {
43
44
Lines 108-113 Link Here
108
		return _iBook.getExternalSheet(externSheetIndex);
109
		return _iBook.getExternalSheet(externSheetIndex);
109
	}
110
	}
110
111
112
	public ExternalName getExternalName(NameXPtg n){
113
		return _iBook.getExternalName(n.getSheetRefIndex(), n.getNameIndex());
114
	}
115
	
111
	public String resolveNameXText(NameXPtg n) {
116
	public String resolveNameXText(NameXPtg n) {
112
		return _iBook.resolveNameXText(n.getSheetRefIndex(), n.getNameIndex());
117
		return _iBook.resolveNameXText(n.getSheetRefIndex(), n.getNameIndex());
113
	}
118
	}
(-)src/java/org/apache/poi/hssf/model/LinkTable.java (-1 / +14 lines)
Lines 63-68 Link Here
63
 *
63
 *
64
 *
64
 *
65
 * @author Josh Micich
65
 * @author Josh Micich
66
 * @author Stephen Wolke (smwolke at geistig.com)
66
 */
67
 */
67
final class LinkTable {
68
final class LinkTable {
68
69
Lines 124-129 Link Here
124
		public String getNameText(int definedNameIndex) {
125
		public String getNameText(int definedNameIndex) {
125
			return _externalNameRecords[definedNameIndex].getText();
126
			return _externalNameRecords[definedNameIndex].getText();
126
		}
127
		}
128
		
129
		public int getIx(int definedNameIndex) {
130
			return _externalNameRecords[definedNameIndex].getIx();
131
		}
127
132
128
		/**
133
		/**
129
		 * Performs case-insensitive search
134
		 * Performs case-insensitive search
Lines 317-323 Link Here
317
			return null;
322
			return null;
318
		}
323
		}
319
		int shIx = _externSheetRecord.getFirstSheetIndexFromRefIndex(extRefIndex);
324
		int shIx = _externSheetRecord.getFirstSheetIndexFromRefIndex(extRefIndex);
320
		String usSheetName = ebr.getSheetNames()[shIx];
325
		//if the shIx is <0 then the external reference is a global reference and has no sheet name
326
		String usSheetName = null;
327
		if(shIx >=0)
328
			usSheetName = ebr.getSheetNames()[shIx];
321
		return new String[] {
329
		return new String[] {
322
				ebr.getURL(),
330
				ebr.getURL(),
323
				usSheetName,
331
				usSheetName,
Lines 415-420 Link Here
415
		return -1;
423
		return -1;
416
	}
424
	}
417
425
426
	public int resolveNameXIx(int refIndex, int definedNameIndex) {
427
		int extBookIndex = _externSheetRecord.getExtbookIndexFromRefIndex(refIndex);
428
		return _externalBookBlocks[extBookIndex].getIx(definedNameIndex);
429
	}
430
	
418
	public String resolveNameXText(int refIndex, int definedNameIndex) {
431
	public String resolveNameXText(int refIndex, int definedNameIndex) {
419
		int extBookIndex = _externSheetRecord.getExtbookIndexFromRefIndex(refIndex);
432
		int extBookIndex = _externSheetRecord.getExtbookIndexFromRefIndex(refIndex);
420
		return _externalBookBlocks[extBookIndex].getNameText(definedNameIndex);
433
		return _externalBookBlocks[extBookIndex].getNameText(definedNameIndex);
(-)src/java/org/apache/poi/hssf/model/InternalWorkbook.java (+11 lines)
Lines 81-86 Link Here
81
import org.apache.poi.hssf.record.formula.FormulaShifter;
81
import org.apache.poi.hssf.record.formula.FormulaShifter;
82
import org.apache.poi.hssf.record.formula.Ptg;
82
import org.apache.poi.hssf.record.formula.Ptg;
83
import org.apache.poi.hssf.util.HSSFColor;
83
import org.apache.poi.hssf.util.HSSFColor;
84
import org.apache.poi.ss.formula.EvaluationWorkbook.ExternalName;
84
import org.apache.poi.ss.formula.EvaluationWorkbook.ExternalSheet;
85
import org.apache.poi.ss.formula.EvaluationWorkbook.ExternalSheet;
85
import org.apache.poi.util.Internal;
86
import org.apache.poi.util.Internal;
86
import org.apache.poi.util.POILogFactory;
87
import org.apache.poi.util.POILogFactory;
Lines 107-112 Link Here
107
 * @author  Brian Sanders (bsanders at risklabs dot com) - custom palette
108
 * @author  Brian Sanders (bsanders at risklabs dot com) - custom palette
108
 * @author  Dan Sherman (dsherman at isisph.com)
109
 * @author  Dan Sherman (dsherman at isisph.com)
109
 * @author  Glen Stampoultzis (glens at apache.org)
110
 * @author  Glen Stampoultzis (glens at apache.org)
111
 * @author  Stephen Wolke (smwolke at geistig.com)
110
 * @see org.apache.poi.hssf.usermodel.HSSFWorkbook
112
 * @see org.apache.poi.hssf.usermodel.HSSFWorkbook
111
 */
113
 */
112
@Internal
114
@Internal
Lines 1762-1767 Link Here
1762
        }
1764
        }
1763
        return new ExternalSheet(extNames[0], extNames[1]);
1765
        return new ExternalSheet(extNames[0], extNames[1]);
1764
    }
1766
    }
1767
    
1768
    public ExternalName getExternalName(int refIndex, int definedNameIndex) {
1769
        String name = linkTable.resolveNameXText(refIndex, definedNameIndex);
1770
        int ix = linkTable.resolveNameXIx(refIndex, definedNameIndex);
1771
        if (name == null) {
1772
            return null;
1773
        }
1774
        return new ExternalName(name,definedNameIndex,ix);
1775
    }
1765
1776
1766
    /**
1777
    /**
1767
     * Finds the sheet index for a particular external sheet number.
1778
     * Finds the sheet index for a particular external sheet number.
(-)src/java/org/apache/poi/hssf/record/formula/NameXPtg.java (+6 lines)
Lines 25-30 Link Here
25
/**
25
/**
26
 * 
26
 * 
27
 * @author aviks
27
 * @author aviks
28
 * @author Stephen Wolke (smwolke at geistig.com)
28
 */
29
 */
29
public final class NameXPtg extends OperandPtg implements WorkbookDependentFormula {
30
public final class NameXPtg extends OperandPtg implements WorkbookDependentFormula {
30
	public final static short sid = 0x39;
31
	public final static short sid = 0x39;
Lines 84-87 Link Here
84
	public int getNameIndex() {
85
	public int getNameIndex() {
85
		return _nameNumber - 1;
86
		return _nameNumber - 1;
86
	}
87
	}
88
        
89
	public String toString(){
90
		String retValue = "NameXPtg:[sheetRefIndex:" + _sheetRefIndex + " , nameNumber:" + _nameNumber + "]" ;
91
		return retValue;
92
	}
87
}
93
}
(-)src/java/org/apache/poi/hssf/record/ExternalNameRecord.java (+8 lines)
Lines 26-31 Link Here
26
 * EXTERNALNAME (0x0023)<p/>
26
 * EXTERNALNAME (0x0023)<p/>
27
 *
27
 *
28
 * @author Josh Micich
28
 * @author Josh Micich
29
 * @author Stephen Wolke (smwolke at geistig.com)
29
 */
30
 */
30
public final class ExternalNameRecord extends StandardRecord {
31
public final class ExternalNameRecord extends StandardRecord {
31
32
Lines 96-101 Link Here
96
	public String getText() {
97
	public String getText() {
97
		return field_4_name;
98
		return field_4_name;
98
	}
99
	}
100
	
101
	/**
102
	 * @return the int value of the reserved field that should be 0 but is not always.
103
	 */
104
	public int getIx() {
105
		return field_2_not_used;
106
	}
99
107
100
	protected int getDataSize(){
108
	protected int getDataSize(){
101
		int result = 2 + 4;  // short and int
109
		int result = 2 + 4;  // short and int
(-)src/java/org/apache/poi/ss/formula/OperationEvaluationContext.java (+39 lines)
Lines 17-26 Link Here
17
17
18
package org.apache.poi.ss.formula;
18
package org.apache.poi.ss.formula;
19
19
20
import org.apache.poi.hssf.record.formula.Area3DPtg;
21
import org.apache.poi.hssf.record.formula.NameXPtg;
22
import org.apache.poi.hssf.record.formula.Ptg;
23
import org.apache.poi.hssf.record.formula.Ref3DPtg;
20
import org.apache.poi.hssf.record.formula.eval.*;
24
import org.apache.poi.hssf.record.formula.eval.*;
21
import org.apache.poi.hssf.record.formula.functions.FreeRefFunction;
25
import org.apache.poi.hssf.record.formula.functions.FreeRefFunction;
22
import org.apache.poi.ss.SpreadsheetVersion;
26
import org.apache.poi.ss.SpreadsheetVersion;
23
import org.apache.poi.ss.formula.CollaboratingWorkbooksEnvironment.WorkbookNotFoundException;
27
import org.apache.poi.ss.formula.CollaboratingWorkbooksEnvironment.WorkbookNotFoundException;
28
import org.apache.poi.ss.formula.EvaluationWorkbook.ExternalName;
24
import org.apache.poi.ss.formula.EvaluationWorkbook.ExternalSheet;
29
import org.apache.poi.ss.formula.EvaluationWorkbook.ExternalSheet;
25
import org.apache.poi.ss.util.CellReference;
30
import org.apache.poi.ss.util.CellReference;
26
import org.apache.poi.ss.util.CellReference.NameType;
31
import org.apache.poi.ss.util.CellReference.NameType;
Lines 32-37 Link Here
32
 * For POI internal use only
37
 * For POI internal use only
33
 *
38
 *
34
 * @author Josh Micich
39
 * @author Josh Micich
40
 * @author Stephen Wolke (smwolke at geistig.com)
35
 */
41
 */
36
public final class OperationEvaluationContext {
42
public final class OperationEvaluationContext {
37
	public static final FreeRefFunction UDF = UserDefinedFunction.instance;
43
	public static final FreeRefFunction UDF = UserDefinedFunction.instance;
Lines 254-257 Link Here
254
		SheetRefEvaluator sre = createExternSheetRefEvaluator(extSheetIndex);
260
		SheetRefEvaluator sre = createExternSheetRefEvaluator(extSheetIndex);
255
		return new LazyAreaEval(firstRowIndex, firstColumnIndex, lastRowIndex, lastColumnIndex, sre);
261
		return new LazyAreaEval(firstRowIndex, firstColumnIndex, lastRowIndex, lastColumnIndex, sre);
256
	}
262
	}
263
	public ValueEval getNameXEval(NameXPtg nameXPtg) {
264
		ExternalSheet externSheet = _workbook.getExternalSheet(nameXPtg.getSheetRefIndex());
265
		if(externSheet == null)
266
			return new NameXEval(nameXPtg); 
267
		String workbookName = externSheet.getWorkbookName();
268
		ExternalName externName = _workbook.getExternalName(nameXPtg);
269
		try{
270
			WorkbookEvaluator refWorkbookEvaluator = _bookEvaluator.getOtherWorkbookEvaluator(workbookName);
271
			EvaluationName evaluationName = refWorkbookEvaluator.getName(externName.getName(),externName.getIx()-1);
272
			if(evaluationName.hasFormula()){
273
				if (evaluationName.getNameDefinition().length > 1) {
274
					throw new RuntimeException("Complex name formulas not supported yet");
275
				}
276
				Ptg ptg = evaluationName.getNameDefinition()[0];
277
				if(ptg instanceof Ref3DPtg){
278
					Ref3DPtg ref3D = (Ref3DPtg)ptg;
279
					int sheetIndex = refWorkbookEvaluator.getSheetIndexByExternIndex(ref3D.getExternSheetIndex());
280
					String sheetName = refWorkbookEvaluator.getSheetName(sheetIndex);
281
					SheetRefEvaluator sre = createExternSheetRefEvaluator(workbookName, sheetName);
282
					return new LazyRefEval(ref3D.getRow(), ref3D.getColumn(), sre);
283
				}else if(ptg instanceof Area3DPtg){
284
					Area3DPtg area3D = (Area3DPtg)ptg;
285
					int sheetIndex = refWorkbookEvaluator.getSheetIndexByExternIndex(area3D.getExternSheetIndex());
286
					String sheetName = refWorkbookEvaluator.getSheetName(sheetIndex);
287
					SheetRefEvaluator sre = createExternSheetRefEvaluator(workbookName, sheetName);
288
					return new LazyAreaEval(area3D.getFirstRow(), area3D.getFirstColumn(), area3D.getLastRow(), area3D.getLastColumn(), sre);
289
				}
290
			}
291
			return ErrorEval.REF_INVALID;
292
		}catch(WorkbookNotFoundException wnfe){
293
			return ErrorEval.REF_INVALID;
294
		}
295
	}
257
}
296
}
(-)src/java/org/apache/poi/ss/formula/eval/forked/ForkedEvaluationWorkbook.java (+5 lines)
Lines 36-41 Link Here
36
 * updated after a call to {@link #getOrCreateUpdatableCell(String, int, int)}.
36
 * updated after a call to {@link #getOrCreateUpdatableCell(String, int, int)}.
37
 *
37
 *
38
 * @author Josh Micich
38
 * @author Josh Micich
39
 * @author Stephen Wolke (smwolke at geistig.com)
39
 */
40
 */
40
final class ForkedEvaluationWorkbook implements EvaluationWorkbook {
41
final class ForkedEvaluationWorkbook implements EvaluationWorkbook {
41
42
Lines 91-96 Link Here
91
		return _masterBook.getExternalSheet(externSheetIndex);
92
		return _masterBook.getExternalSheet(externSheetIndex);
92
	}
93
	}
93
94
95
	public ExternalName getExternalName(NameXPtg ptg) {
96
		return _masterBook.getExternalName(ptg);
97
	}
98
94
	public Ptg[] getFormulaTokens(EvaluationCell cell) {
99
	public Ptg[] getFormulaTokens(EvaluationCell cell) {
95
		if (cell instanceof ForkedEvaluationCell) {
100
		if (cell instanceof ForkedEvaluationCell) {
96
			// doesn't happen yet because formulas cannot be modified from the master workbook
101
			// doesn't happen yet because formulas cannot be modified from the master workbook
(-)src/java/org/apache/poi/ss/formula/WorkbookEvaluator.java (-2 / +16 lines)
Lines 55-61 Link Here
55
import org.apache.poi.hssf.record.formula.eval.EvaluationException;
55
import org.apache.poi.hssf.record.formula.eval.EvaluationException;
56
import org.apache.poi.hssf.record.formula.eval.MissingArgEval;
56
import org.apache.poi.hssf.record.formula.eval.MissingArgEval;
57
import org.apache.poi.hssf.record.formula.eval.NameEval;
57
import org.apache.poi.hssf.record.formula.eval.NameEval;
58
import org.apache.poi.hssf.record.formula.eval.NameXEval;
59
import org.apache.poi.hssf.record.formula.eval.NumberEval;
58
import org.apache.poi.hssf.record.formula.eval.NumberEval;
60
import org.apache.poi.hssf.record.formula.eval.OperandResolver;
59
import org.apache.poi.hssf.record.formula.eval.OperandResolver;
61
import org.apache.poi.hssf.record.formula.eval.RefEval;
60
import org.apache.poi.hssf.record.formula.eval.RefEval;
Lines 65-70 Link Here
65
import org.apache.poi.hssf.record.formula.functions.FreeRefFunction;
64
import org.apache.poi.hssf.record.formula.functions.FreeRefFunction;
66
import org.apache.poi.hssf.record.formula.functions.IfFunc;
65
import org.apache.poi.hssf.record.formula.functions.IfFunc;
67
import org.apache.poi.hssf.record.formula.udf.UDFFinder;
66
import org.apache.poi.hssf.record.formula.udf.UDFFinder;
67
import org.apache.poi.hssf.usermodel.HSSFEvaluationWorkbook;
68
import org.apache.poi.hssf.util.CellReference;
68
import org.apache.poi.hssf.util.CellReference;
69
import org.apache.poi.ss.formula.CollaboratingWorkbooksEnvironment.WorkbookNotFoundException;
69
import org.apache.poi.ss.formula.CollaboratingWorkbooksEnvironment.WorkbookNotFoundException;
70
import org.apache.poi.ss.formula.eval.NotImplementedException;
70
import org.apache.poi.ss.formula.eval.NotImplementedException;
Lines 80-85 Link Here
80
 * For POI internal use only
80
 * For POI internal use only
81
 *
81
 *
82
 * @author Josh Micich
82
 * @author Josh Micich
83
 * @author Stephen Wolke (smwolke at geistig.com)
83
 */
84
 */
84
public final class WorkbookEvaluator {
85
public final class WorkbookEvaluator {
85
86
Lines 120-125 Link Here
120
	/* package */ String getSheetName(int sheetIndex) {
121
	/* package */ String getSheetName(int sheetIndex) {
121
		return _workbook.getSheetName(sheetIndex);
122
		return _workbook.getSheetName(sheetIndex);
122
	}
123
	}
124
        
125
	int getSheetIndexByExternIndex(int externSheetIndex) {
126
		return _workbook.convertFromExternSheetIndex(externSheetIndex);
127
	}
123
128
124
	/* package */ EvaluationSheet getSheet(int sheetIndex) {
129
	/* package */ EvaluationSheet getSheet(int sheetIndex) {
125
		return _workbook.getSheet(sheetIndex);
130
		return _workbook.getSheet(sheetIndex);
Lines 207-212 Link Here
207
		return evaluateAny(srcCell, sheetIndex, srcCell.getRowIndex(), srcCell.getColumnIndex(), new EvaluationTracker(_cache));
212
		return evaluateAny(srcCell, sheetIndex, srcCell.getRowIndex(), srcCell.getColumnIndex(), new EvaluationTracker(_cache));
208
	}
213
	}
209
214
215
	public EvaluationName getName(String name,int sheetIndex){
216
		if(_workbook instanceof HSSFEvaluationWorkbook){
217
			NamePtg namePtg =((HSSFEvaluationWorkbook)_workbook).getName(name, sheetIndex).createPtg();
218
			return  _workbook.getName(namePtg);
219
		}else
220
			return null;
221
	}
222
        
210
	/**
223
	/**
211
	 * Case-insensitive.
224
	 * Case-insensitive.
212
	 * @return -1 if sheet with specified name does not exist
225
	 * @return -1 if sheet with specified name does not exist
Lines 524-530 Link Here
524
			throw new RuntimeException("Don't now how to evalate name '" + nameRecord.getNameText() + "'");
537
			throw new RuntimeException("Don't now how to evalate name '" + nameRecord.getNameText() + "'");
525
		}
538
		}
526
		if (ptg instanceof NameXPtg) {
539
		if (ptg instanceof NameXPtg) {
527
			return new NameXEval(((NameXPtg) ptg));
540
			NameXPtg namexPtg = (NameXPtg) ptg;
541
			return ec.getNameXEval(namexPtg);
528
		}
542
		}
529
543
530
		if (ptg instanceof IntPtg) {
544
		if (ptg instanceof IntPtg) {
(-)src/java/org/apache/poi/ss/formula/EvaluationWorkbook.java (+27 lines)
Lines 27-32 Link Here
27
 * For POI internal use only
27
 * For POI internal use only
28
 *
28
 *
29
 * @author Josh Micich
29
 * @author Josh Micich
30
 * @author Stephen Wolke (smwolke at geistig.com)
30
 */
31
 */
31
public interface EvaluationWorkbook {
32
public interface EvaluationWorkbook {
32
	String getSheetName(int sheetIndex);
33
	String getSheetName(int sheetIndex);
Lines 46-51 Link Here
46
	 * @return <code>null</code> if externSheetIndex refers to a sheet inside the current workbook
47
	 * @return <code>null</code> if externSheetIndex refers to a sheet inside the current workbook
47
	 */
48
	 */
48
	ExternalSheet getExternalSheet(int externSheetIndex);
49
	ExternalSheet getExternalSheet(int externSheetIndex);
50
	ExternalName getExternalName(NameXPtg ptg);
49
	int convertFromExternSheetIndex(int externSheetIndex);
51
	int convertFromExternSheetIndex(int externSheetIndex);
50
	EvaluationName getName(NamePtg namePtg);
52
	EvaluationName getName(NamePtg namePtg);
51
	String resolveNameXText(NameXPtg ptg);
53
	String resolveNameXText(NameXPtg ptg);
Lines 66-69 Link Here
66
			return _sheetName;
68
			return _sheetName;
67
		}
69
		}
68
	}
70
	}
71
	
72
	class ExternalName {
73
		private final String _name;
74
		private final int _nameNumber;
75
		private final int _ix;
76
77
		public ExternalName(String name, int nameNumber, int ix) {
78
			_name = name;
79
			_nameNumber = nameNumber;
80
			_ix = ix;
81
		}
82
83
		public int getIx() {
84
			return _ix;
85
		}
86
87
		public String getName() {
88
			return _name;
89
		}
90
91
		public int getNameNumber() {
92
			return _nameNumber;
93
		}
94
		
95
	}
69
}
96
}
(-)src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvaluationWorkbook.java (+5 lines)
Lines 35-40 Link Here
35
 * Internal POI use only
35
 * Internal POI use only
36
 *
36
 *
37
 * @author Josh Micich
37
 * @author Josh Micich
38
 * @author Stephen Wolke (smwolke at geistig.com)
38
 */
39
 */
39
public final class XSSFEvaluationWorkbook implements FormulaRenderingWorkbook, EvaluationWorkbook, FormulaParsingWorkbook {
40
public final class XSSFEvaluationWorkbook implements FormulaRenderingWorkbook, EvaluationWorkbook, FormulaParsingWorkbook {
40
41
Lines 95-100 Link Here
95
		return _uBook.getSheetName(sheetIndex);
96
		return _uBook.getSheetName(sheetIndex);
96
	}
97
	}
97
98
99
	public ExternalName getExternalName(NameXPtg ptg) {
100
		throw new UnsupportedOperationException("Not supported yet.");
101
	}
102
98
	public NameXPtg getNameXPtg(String name) {
103
	public NameXPtg getNameXPtg(String name) {
99
		// may require to return null to make tests pass
104
		// may require to return null to make tests pass
100
		throw new RuntimeException("Not implemented yet");
105
		throw new RuntimeException("Not implemented yet");
(-)src/testcases/org/apache/poi/hssf/record/formula/AllFormulaTests.java (+2 lines)
Lines 28-33 Link Here
28
 * Collects all tests for <tt>org.apache.poi.hssf.record.formula</tt>.
28
 * Collects all tests for <tt>org.apache.poi.hssf.record.formula</tt>.
29
 * 
29
 * 
30
 * @author Josh Micich
30
 * @author Josh Micich
31
 * @author Stephen Wolke (smwolke at geistig.com)
31
 */
32
 */
32
public final class AllFormulaTests {
33
public final class AllFormulaTests {
33
	
34
	
Lines 44-49 Link Here
44
		result.addTestSuite(TestAttrPtg.class);
45
		result.addTestSuite(TestAttrPtg.class);
45
		result.addTestSuite(TestErrPtg.class);
46
		result.addTestSuite(TestErrPtg.class);
46
		result.addTestSuite(TestExternalFunctionFormulas.class);
47
		result.addTestSuite(TestExternalFunctionFormulas.class);
48
		result.addTestSuite(TestExternalNameReference.class);
47
		result.addTestSuite(TestFormulaShifter.class);
49
		result.addTestSuite(TestFormulaShifter.class);
48
		result.addTestSuite(TestFuncPtg.class);
50
		result.addTestSuite(TestFuncPtg.class);
49
		result.addTestSuite(TestFuncVarPtg.class);
51
		result.addTestSuite(TestFuncVarPtg.class);

Return to bug 48996