All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.poi.hssf.record.formula.functions.Subtotal Maven / Gradle / Ivy

There is a newer version: 5.2.5
Show newest version
/* ====================================================================
   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.hssf.record.formula.functions;

import org.apache.poi.hssf.record.formula.eval.ErrorEval;
import org.apache.poi.hssf.record.formula.eval.EvaluationException;
import org.apache.poi.hssf.record.formula.eval.OperandResolver;
import org.apache.poi.hssf.record.formula.eval.ValueEval;
import org.apache.poi.ss.formula.eval.NotImplementedException;

/**
 * Implementation for the Excel function SUBTOTAL

* * Syntax :
* SUBTOTAL ( functionCode, ref1, ref2 ... )
*

* * *
functionCode(1-11) Selects the underlying aggregate function to be used (see table below)
ref1, ref2 ...Arguments to be passed to the underlying aggregate function

*

* * * * * * * * * * * * * * * *
functionCodeAggregate Function
1AVERAGE
2COUNT
3COUNTA
4MAX
5MIN
6PRODUCT
7STDEV
8STDEVP *
9AVERAGE
10VAR *
11VARP *
101-111*

* * Not implemented in POI yet. Functions 101-111 are the same as functions 1-11 but with * the option 'ignore hidden values'. *

* * @author Paul Tomlin < pault at bulk sms dot com > */ public class Subtotal implements Function { private static Function findFunction(int functionCode) throws EvaluationException { switch (functionCode) { case 1: return AggregateFunction.AVERAGE; case 2: return new Count(); case 3: return new Counta(); case 4: return AggregateFunction.MAX; case 5: return AggregateFunction.MIN; case 6: return AggregateFunction.PRODUCT; case 7: return AggregateFunction.STDEV; case 8: throw new NotImplementedException("STDEVP"); case 9: return AggregateFunction.SUM; case 10: throw new NotImplementedException("VAR"); case 11: throw new NotImplementedException("VARP"); } if (functionCode > 100 && functionCode < 112) { throw new NotImplementedException("SUBTOTAL - with 'exclude hidden values' option"); } throw EvaluationException.invalidValue(); } public ValueEval evaluate(ValueEval[] args, int srcRowIndex, int srcColumnIndex) { int nInnerArgs = args.length-1; // -1: first arg is used to select from a basic aggregate function if (nInnerArgs < 1) { return ErrorEval.VALUE_INVALID; } Function innerFunc; try { ValueEval ve = OperandResolver.getSingleValue(args[0], srcRowIndex, srcColumnIndex); int functionCode = OperandResolver.coerceValueToInt(ve); innerFunc = findFunction(functionCode); } catch (EvaluationException e) { return e.getErrorEval(); } ValueEval[] innerArgs = new ValueEval[nInnerArgs]; System.arraycopy(args, 1, innerArgs, 0, nInnerArgs); return innerFunc.evaluate(innerArgs, srcRowIndex, srcColumnIndex); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy