Example usage for org.apache.poi.ss.formula.eval ErrorEval NAME_INVALID

List of usage examples for org.apache.poi.ss.formula.eval ErrorEval NAME_INVALID

Introduction

In this page you can find the example usage for org.apache.poi.ss.formula.eval ErrorEval NAME_INVALID.

Prototype

ErrorEval NAME_INVALID

To view the source code for org.apache.poi.ss.formula.eval ErrorEval NAME_INVALID.

Click Source Link

Document

#NAME? - Wrong function or range name

Usage

From source file:com.dataart.spreadsheetanalytics.engine.ConverterUtils.java

License:Apache License

/** Inserts a value to a Cell based on a value type (class). */
static void populateCellValue(final Cell cell, final ICellValue value) {
    if (cell == null) {
        return;/*from   w  w  w.j  a v a 2 s  .  co  m*/
    }

    int cellType = resolveCellType(value);
    cell.setCellType(cellType);
    switch (cellType) {
    case CELL_TYPE_BLANK: {
        break;
    }
    case CELL_TYPE_BOOLEAN: {
        cell.setCellValue((Boolean) value.get());
        break;
    }
    case CELL_TYPE_NUMERIC: {
        cell.setCellValue((Double) value.get());
        break;
    }
    case CELL_TYPE_FORMULA: {
        try {
            cell.setCellFormula(((String) value.get()).substring(1));
            break;
        } catch (FormulaParseNameException e) {
            log.error("Formula parsing error while trying to set formula field in cell " + e.getMessage());
            cell.setCellFormula(ErrorEval.NAME_INVALID.getErrorString());
            break;
        } catch (FormulaParseNAException e) {
            log.error("Formula parsing error while trying to set formula field in cell " + e.getMessage());
            cell.setCellFormula(ErrorEval.NA.getErrorString());
            break;
        }
    }
    case CELL_TYPE_ERROR: {
        cell.setCellErrorValue(FormulaError.forString((String) value.get()).getCode());
        break;
    }
    case CELL_TYPE_STRING: {
        cell.setCellValue((String) value.get());
        break;
    }

    default: {
        throw new CalculationEngineException(String.format("Type of value %s is not supported: %s", value,
                value.getClass().getSimpleName()));
    }
    }
}

From source file:com.dataart.spreadsheetanalytics.functions.poi.data.FuncexecFunction.java

License:Apache License

@Override
public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {

    log.debug("In evaluate() of FUNCEXEC function. Args = {}", Arrays.toString(args));

    if (!(args[0] instanceof StringEval) && !(args[0] instanceof RefEval)) {
        log.warn(//from  w  w w .j av  a2 s  .co  m
                "The first argument of FUNCEXEC function must be a string (or a reference to a cell) - name of DEFINE function.");
        return ErrorEval.VALUE_INVALID;
    }

    String defineFunctionName;
    try {
        defineFunctionName = (String) coerceValueTo(
                getSingleValue(args[0], ec.getRowIndex(), ec.getColumnIndex()));
    } catch (EvaluationException e) {
        log.error(String.format("Cannot get the value of DEFINE functuion name: %s", args[0]), e);
        return ErrorEval.VALUE_INVALID;
    }
    defineFunctionName = defineFunctionName.toUpperCase(Locale.getDefault());

    MetaFunctionAccessor defines = (MetaFunctionAccessor) ec.getCustomEvaluationContext()
            .get(MetaFunctionAccessor.class);
    if (defines == null) {
        defines = this.external.getMetaFunctionAccessor();
    }

    if (defines.get(defineFunctionName) == null) {
        log.warn("No DEFINE function with name {} is found.", defineFunctionName);
        return ErrorEval.NAME_INVALID;
    }

    List<ValueEval> inputValues = new LinkedList<>();
    List<ValueEval> inArgs = new ArrayList(Arrays.asList(args));
    inArgs.remove(0); //remove define function name

    try {
        for (ValueEval v : ICustomFunction.prepareQueryArgs(inArgs)) {
            inputValues.add(getSingleValue(v, ec.getRowIndex(), ec.getColumnIndex()));
        }
    } catch (EvaluationException e) {
        log.error("Cannot resolve input values for FUNCEXEC function", e);
        return ErrorEval.VALUE_INVALID;
    }

    final DefineFunctionMeta meta = (DefineFunctionMeta) defines.get(defineFunctionName);
    log.info("Found DEFINE function to invoke. Name = {}.", defineFunctionName);

    if (meta.getInputs().size() != inputValues.size()) {
        log.warn("Wrong number of input arguments for FUNCEXEC+DEFINE. Expected: {}, Actual: {}.",
                meta.getInputs().size(), args.length - 1);
        return ErrorEval.VALUE_INVALID;
    }

    List<IA1Address> inputAddresses = meta.getInputs();
    log.debug("Input Addresses for DEFINE: {}, Input Values for DEFINE: {}.", inputAddresses, inputValues);

    if (inputAddresses.size() != inputValues.size()) {
        log.warn("Wrong number of input arguments for {} function.", defineFunctionName);
        return ErrorEval.VALUE_INVALID;
    }

    DataModelAccessor dataModels = (DataModelAccessor) ec.getCustomEvaluationContext()
            .get(DataModelAccessor.class);
    if (dataModels == null) {
        dataModels = this.external.getDataModelAccessor();
    }

    IDataModel dmWithDefine = dataModels.get(meta.getDataModelId());

    Workbook book = toWorkbook(dmWithDefine);
    EvaluationWorkbook defineBook = toEvaluationWorkbook(book);

    Sheet s = book.getSheetAt(0); //TODO one sheet support
    for (int i = 0; i < inputAddresses.size(); i++) {

        Row defineRow = s.getRow(inputAddresses.get(i).row());
        if (defineRow == null) {
            defineRow = s.createRow(inputAddresses.get(i).row());
        }
        Cell defineCell = defineRow.getCell(inputAddresses.get(i).column());
        if (defineCell == null) {
            defineCell = defineRow.createCell(inputAddresses.get(i).column());
        }

        populateCellValue(defineCell, inputValues.get(i));
        updateCell(defineBook, defineCell);
    }

    WorkbookEvaluator defineEvaluator = new WorkbookEvaluator(defineBook,
            IStabilityClassifier.TOTALLY_IMMUTABLE, null);
    List<ValueEval> outputValues = meta.getOutputs().stream().map(
            a -> defineEvaluator.evaluate(getEvaluationCell(defineBook, a), ec.getCustomEvaluationContext()))
            .collect(Collectors.<ValueEval>toList());

    log.debug("Output Values of DEFINE execution: {}.", outputValues);

    return outputValues.size() == 1 ? outputValues.get(0) : toArrayEval(outputValues);
}