workbookevaluator.java

来自「EXCEL read and write」· Java 代码 · 共 469 行 · 第 1/2 页

JAVA
469
字号
/* ====================================================================   Licensed to the Apache Software Foundation (ASF) under one or more   contributor license agreements.  See the NOTICE file distributed with   this work for additional information regarding copyright ownership.   The ASF licenses this file to You under the Apache License, Version 2.0   (the "License"); you may not use this file except in compliance with   the License.  You may obtain a copy of the License at       http://www.apache.org/licenses/LICENSE-2.0   Unless required by applicable law or agreed to in writing, software   distributed under the License is distributed on an "AS IS" BASIS,   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   See the License for the specific language governing permissions and   limitations under the License.==================================================================== */package org.apache.poi.ss.formula;import java.util.IdentityHashMap;import java.util.Map;import java.util.Stack;import org.apache.poi.hssf.record.formula.Area3DPtg;import org.apache.poi.hssf.record.formula.AreaErrPtg;import org.apache.poi.hssf.record.formula.AreaPtg;import org.apache.poi.hssf.record.formula.AttrPtg;import org.apache.poi.hssf.record.formula.BoolPtg;import org.apache.poi.hssf.record.formula.ControlPtg;import org.apache.poi.hssf.record.formula.DeletedArea3DPtg;import org.apache.poi.hssf.record.formula.DeletedRef3DPtg;import org.apache.poi.hssf.record.formula.ErrPtg;import org.apache.poi.hssf.record.formula.FuncVarPtg;import org.apache.poi.hssf.record.formula.IntPtg;import org.apache.poi.hssf.record.formula.MemErrPtg;import org.apache.poi.hssf.record.formula.MemFuncPtg;import org.apache.poi.hssf.record.formula.MissingArgPtg;import org.apache.poi.hssf.record.formula.NamePtg;import org.apache.poi.hssf.record.formula.NameXPtg;import org.apache.poi.hssf.record.formula.NumberPtg;import org.apache.poi.hssf.record.formula.OperationPtg;import org.apache.poi.hssf.record.formula.Ptg;import org.apache.poi.hssf.record.formula.Ref3DPtg;import org.apache.poi.hssf.record.formula.RefErrorPtg;import org.apache.poi.hssf.record.formula.RefPtg;import org.apache.poi.hssf.record.formula.StringPtg;import org.apache.poi.hssf.record.formula.UnionPtg;import org.apache.poi.hssf.record.formula.UnknownPtg;import org.apache.poi.hssf.record.formula.eval.AreaEval;import org.apache.poi.hssf.record.formula.eval.BlankEval;import org.apache.poi.hssf.record.formula.eval.BoolEval;import org.apache.poi.hssf.record.formula.eval.ErrorEval;import org.apache.poi.hssf.record.formula.eval.Eval;import org.apache.poi.hssf.record.formula.eval.FunctionEval;import org.apache.poi.hssf.record.formula.eval.MissingArgEval;import org.apache.poi.hssf.record.formula.eval.NameEval;import org.apache.poi.hssf.record.formula.eval.NameXEval;import org.apache.poi.hssf.record.formula.eval.NumberEval;import org.apache.poi.hssf.record.formula.eval.OperationEval;import org.apache.poi.hssf.record.formula.eval.RefEval;import org.apache.poi.hssf.record.formula.eval.StringEval;import org.apache.poi.hssf.record.formula.eval.ValueEval;import org.apache.poi.hssf.usermodel.HSSFCell;import org.apache.poi.hssf.util.CellReference;import org.apache.poi.ss.formula.EvaluationWorkbook.ExternalSheet;/** * Evaluates formula cells.<p/> * * For performance reasons, this class keeps a cache of all previously calculated intermediate * cell values.  Be sure to call {@link #clearCache()} if any workbook cells are changed between * calls to evaluate~ methods on this class.<br/> * * For POI internal use only * * @author Josh Micich */public final class WorkbookEvaluator {	private final EvaluationWorkbook _workbook;	private EvaluationCache _cache;	private int _workbookIx;	private final IEvaluationListener _evaluationListener;	private final Map _sheetIndexesBySheet;	private CollaboratingWorkbooksEnvironment _collaboratingWorkbookEnvironment;	public WorkbookEvaluator(EvaluationWorkbook workbook) {		this (workbook, null);	}	/* package */ WorkbookEvaluator(EvaluationWorkbook workbook, IEvaluationListener evaluationListener) {		_workbook = workbook;		_evaluationListener = evaluationListener;		_cache = new EvaluationCache(evaluationListener);		_sheetIndexesBySheet = new IdentityHashMap();		_collaboratingWorkbookEnvironment = CollaboratingWorkbooksEnvironment.EMPTY;		_workbookIx = 0;	}	/**	 * also for debug use. Used in toString methods	 */	/* package */ String getSheetName(int sheetIndex) {		return _workbook.getSheetName(sheetIndex);	}	private static boolean isDebugLogEnabled() {		return false;	}	private static void logDebug(String s) {		if (isDebugLogEnabled()) {			System.out.println(s);		}	}	/* package */ void attachToEnvironment(CollaboratingWorkbooksEnvironment collaboratingWorkbooksEnvironment, EvaluationCache cache, int workbookIx) {		_collaboratingWorkbookEnvironment = collaboratingWorkbooksEnvironment;		_cache = cache;		_workbookIx = workbookIx;	}	/* package */ CollaboratingWorkbooksEnvironment getEnvironment() {		return _collaboratingWorkbookEnvironment;	}	/* package */ void detachFromEnvironment() {		_collaboratingWorkbookEnvironment = CollaboratingWorkbooksEnvironment.EMPTY;		_cache = new EvaluationCache(_evaluationListener);		_workbookIx = 0;	}	/* package */ IEvaluationListener getEvaluationListener() {		return _evaluationListener;	}	/**	 * Should be called whenever there are changes to input cells in the evaluated workbook.	 * Failure to call this method after changing cell values will cause incorrect behaviour	 * of the evaluate~ methods of this class	 */	public void clearAllCachedResultValues() {		_cache.clear();		_sheetIndexesBySheet.clear();	}	/**	 * Should be called to tell the cell value cache that the specified (value or formula) cell 	 * has changed.	 */	public void notifyUpdateCell(EvaluationCell cell) {		int sheetIndex = getSheetIndex(cell.getSheet());		_cache.notifyUpdateCell(_workbookIx, sheetIndex, cell);	}	/**	 * Should be called to tell the cell value cache that the specified cell has just been	 * deleted. 	 */	public void notifyDeleteCell(EvaluationCell cell) {		int sheetIndex = getSheetIndex(cell.getSheet());		_cache.notifyDeleteCell(_workbookIx, sheetIndex, cell);	}	private int getSheetIndex(EvaluationSheet sheet) {		Integer result = (Integer) _sheetIndexesBySheet.get(sheet);		if (result == null) {			int sheetIndex = _workbook.getSheetIndex(sheet);			if (sheetIndex < 0) {				throw new RuntimeException("Specified sheet from a different book");			}			result = new Integer(sheetIndex);			_sheetIndexesBySheet.put(sheet, result);		}		return result.intValue();	}	public ValueEval evaluate(EvaluationCell srcCell) {		int sheetIndex = getSheetIndex(srcCell.getSheet());		return evaluateAny(srcCell, sheetIndex, srcCell.getRowIndex(), srcCell.getColumnIndex(), new EvaluationTracker(_cache));	}	/**	 * @return never <code>null</code>, never {@link BlankEval}	 */	private ValueEval evaluateAny(EvaluationCell srcCell, int sheetIndex,				int rowIndex, int columnIndex, EvaluationTracker tracker) {		if (srcCell == null || srcCell.getCellType() != HSSFCell.CELL_TYPE_FORMULA) {			ValueEval result = getValueFromNonFormulaCell(srcCell);			tracker.acceptPlainValueDependency(_workbookIx, sheetIndex, rowIndex, columnIndex, result);			return result;		}		FormulaCellCacheEntry cce = _cache.getOrCreateFormulaCellEntry(srcCell);		tracker.acceptFormulaDependency(cce);		IEvaluationListener evalListener = _evaluationListener;		if (cce.getValue() == null) {			if (!tracker.startEvaluate(cce)) {				return ErrorEval.CIRCULAR_REF_ERROR;			}			try {				ValueEval result;				Ptg[] ptgs = _workbook.getFormulaTokens(srcCell);				if (evalListener == null) {					result = evaluateFormula(sheetIndex, rowIndex, columnIndex, ptgs, tracker);				} else {					evalListener.onStartEvaluate(srcCell, cce, ptgs);					result = evaluateFormula(sheetIndex, rowIndex, columnIndex, ptgs, tracker);					evalListener.onEndEvaluate(cce, result);				}				tracker.updateCacheResult(result);			} finally {				tracker.endEvaluate(cce);			}		} else {			if(evalListener != null) {				evalListener.onCacheHit(sheetIndex, rowIndex, columnIndex, cce.getValue());			}			return cce.getValue();		}		if (isDebugLogEnabled()) {			String sheetName = getSheetName(sheetIndex);			CellReference cr = new CellReference(rowIndex, columnIndex);			logDebug("Evaluated " + sheetName + "!" + cr.formatAsString() + " to " + cce.getValue().toString());		}		return cce.getValue();	}	/**	 * Gets the value from a non-formula cell.	 * @param cell may be <code>null</code>	 * @return {@link BlankEval} if cell is <code>null</code> or blank, never <code>null</code>	 */	/* package */ static ValueEval getValueFromNonFormulaCell(EvaluationCell cell) {		if (cell == null) {			return BlankEval.INSTANCE;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?