encapsulated hasUnlimitedVarags() logic into FunctionMetadata

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@775575 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Josh Micich 2009-05-17 02:02:48 +00:00
parent 508e2c83cb
commit 2b1d0dd9ab
3 changed files with 43 additions and 24 deletions

View File

@ -6,7 +6,7 @@
(the "License"); you may not use this file except in compliance with (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0 http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, distributed under the License is distributed on an "AS IS" BASIS,
@ -16,12 +16,24 @@
==================================================================== */ ==================================================================== */
package org.apache.poi.hssf.record.formula.function; package org.apache.poi.hssf.record.formula.function;
import org.apache.poi.ss.SpreadsheetVersion;
/** /**
* Holds information about Excel built-in functions. * Holds information about Excel built-in functions.
* *
* @author Josh Micich * @author Josh Micich
*/ */
public final class FunctionMetadata { public final class FunctionMetadata {
/**
* maxParams=30 in functionMetadata.txt means the maximum number arguments supported
* by the given version of Excel. Validation routines should take the actual limit (Excel 97 or 2007)
* from the SpreadsheetVersion enum.
* Perhaps a value like 'M' should be used instead of '30' in functionMetadata.txt
* to make that file more version neutral.
* @see org.apache.poi.ss.formula.FormulaParser#validateNumArgs(int, FunctionMetadata)
*/
private static final short FUNCTION_MAX_PARAMS = 30;
private final int _index; private final int _index;
private final String _name; private final String _name;
@ -58,7 +70,17 @@ public final class FunctionMetadata {
return _returnClassCode; return _returnClassCode;
} }
public byte[] getParameterClassCodes() { public byte[] getParameterClassCodes() {
return (byte[]) _parameterClassCodes.clone(); return _parameterClassCodes.clone();
}
/**
* Some varags functions (like VLOOKUP) have a specific limit to the number of arguments that
* can be passed. Other functions (like SUM) don't have such a limit. For those functions,
* the spreadsheet version determines the maximum number of arguments that can be passed.
* @return <code>true</code> if this function can the maximum number of arguments allowable by
* the {@link SpreadsheetVersion}
*/
public boolean hasUnlimitedVarags() {
return FUNCTION_MAX_PARAMS == _maxParams;
} }
public String toString() { public String toString() {
StringBuffer sb = new StringBuffer(64); StringBuffer sb = new StringBuffer(64);

View File

@ -20,7 +20,7 @@ package org.apache.poi.hssf.record.formula.function;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
/** /**
* Allows clients to get <tt>FunctionMetadata</tt> instances for any built-in function of Excel. * Allows clients to get {@link FunctionMetadata} instances for any built-in function of Excel.
* *
* @author Josh Micich * @author Josh Micich
*/ */
@ -30,14 +30,6 @@ public final class FunctionMetadataRegistry {
*/ */
public static final String FUNCTION_NAME_IF = "IF"; public static final String FUNCTION_NAME_IF = "IF";
/**
* maxParams=30 in functionMetadata.txt means the maximum number arguments supported
* by the given version of Excel. Validation routines should take the actual limit (Excel 97 or 2007)
* from the SpreadsheetVersion enum.
* @see org.apache.poi.ss.formula.FormulaParser#validateNumArgs(int, FunctionMetadata)
*/
public static final short FUNCTION_MAX_PARAMS = 30;
public static final short FUNCTION_INDEX_SUM = 4; public static final short FUNCTION_INDEX_SUM = 4;
public static final short FUNCTION_INDEX_EXTERNAL = 255; public static final short FUNCTION_INDEX_EXTERNAL = 255;
private static FunctionMetadataRegistry _instance; private static FunctionMetadataRegistry _instance;

View File

@ -31,7 +31,6 @@ import org.apache.poi.hssf.usermodel.HSSFErrorConstants;
import org.apache.poi.ss.util.AreaReference; import org.apache.poi.ss.util.AreaReference;
import org.apache.poi.ss.util.CellReference; import org.apache.poi.ss.util.CellReference;
import org.apache.poi.ss.util.CellReference.NameType; import org.apache.poi.ss.util.CellReference.NameType;
import org.apache.poi.ss.SpreadsheetVersion;
/** /**
* This class parses a formula string into a List of tokens in RPN order. * This class parses a formula string into a List of tokens in RPN order.
@ -984,14 +983,20 @@ public final class FormulaParser {
msg += " but got " + numArgs + "."; msg += " but got " + numArgs + ".";
throw new FormulaParseException(msg); throw new FormulaParseException(msg);
} }
//the maximum number of arguments depends on the Excel version //the maximum number of arguments depends on the Excel version
int maxArgs = fm.getMaxParams(); int maxArgs;
if( maxArgs == FunctionMetadataRegistry.FUNCTION_MAX_PARAMS) { if (fm.hasUnlimitedVarags()) {
//_book can be omitted by test cases if(_book != null) {
if(_book != null) maxArgs = _book.getSpreadsheetVersion().getMaxFunctionArgs(); maxArgs = _book.getSpreadsheetVersion().getMaxFunctionArgs();
} } else {
//_book can be omitted by test cases
maxArgs = fm.getMaxParams(); // just use BIFF8
}
} else {
maxArgs = fm.getMaxParams();
}
if(numArgs > maxArgs) { if(numArgs > maxArgs) {
String msg = "Too many arguments to function '" + fm.getName() + "'. "; String msg = "Too many arguments to function '" + fm.getName() + "'. ";
if(fm.hasFixedArgsLength()) { if(fm.hasFixedArgsLength()) {
msg += "Expected " + maxArgs; msg += "Expected " + maxArgs;