Example usage for org.apache.poi.ss.usermodel Cell getRowIndex

List of usage examples for org.apache.poi.ss.usermodel Cell getRowIndex

Introduction

In this page you can find the example usage for org.apache.poi.ss.usermodel Cell getRowIndex.

Prototype

int getRowIndex();

Source Link

Document

Returns row index of a row in the sheet that contains this cell

Usage

From source file:com.asakusafw.testdriver.excel.DefaultExcelRuleExtractor.java

License:Apache License

private String getStringCell(Row row, RuleSheetFormat item) throws FormatException {
    assert row != null;
    assert item != null;
    Cell cell = row.getCell(item.getColumnIndex());
    if (cell == null || cell.getCellType() == Cell.CELL_TYPE_BLANK) {
        return ""; //$NON-NLS-1$
    } else if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
        return cell.getStringCellValue();
    }/*w  ww . j a  v a  2s. co  m*/
    throw new FormatException(
            MessageFormat.format(Messages.getString("DefaultExcelRuleExtractor.errorInvalidStringCell"), //$NON-NLS-1$
                    item.getTitle(), cell.getRowIndex() + 1, cell.getColumnIndex() + 1));
}

From source file:com.asakusafw.testdriver.excel.ExcelSheetDataModelSource.java

License:Apache License

private void evaluateInCell(Cell cell) throws IOException {
    try {/*from  ww w  . j a v a2 s  . co  m*/
        Workbook workbook = cell.getSheet().getWorkbook();
        FormulaEvaluator formulaEvaluator = workbook.getCreationHelper().createFormulaEvaluator();
        formulaEvaluator.evaluateInCell(cell);
    } catch (RuntimeException e) {
        throw new IOException(MessageFormat.format(
                Messages.getString("ExcelSheetDataModelSource.errorFailedToResolveFormulaCell"), //$NON-NLS-1$
                id, cell.getRowIndex() + 1, cell.getColumnIndex() + 1), e);
    }
}

From source file:com.asakusafw.testdriver.excel.legacy.LegacyExcelRuleExtractor.java

License:Apache License

@Override
public String extractName(Row row) throws FormatException {
    if (row == null) {
        throw new IllegalArgumentException("row must not be null"); //$NON-NLS-1$
    }// w w  w . jav  a2s  .c o  m
    // strict checking for cell type
    Cell cell = row.getCell(ConditionSheetItem.COLUMN_NAME.getCol());
    if (cell == null || cell.getCellType() == Cell.CELL_TYPE_BLANK) {
        return null;
    } else if (cell.getCellType() != Cell.CELL_TYPE_STRING) {
        throw new FormatException(MessageFormat.format(
                Messages.getString("LegacyExcelRuleExtractor.errorInvalidNameType"), //$NON-NLS-1$
                ConditionSheetItem.COLUMN_NAME.getName(), cell.getRowIndex() + 1, cell.getColumnIndex() + 1));
    }
    String name = cell.getStringCellValue();
    if (name.isEmpty()) {
        return null;
    }
    return name.toLowerCase();
}

From source file:com.asakusafw.testdriver.excel.legacy.LegacyExcelRuleExtractor.java

License:Apache License

private String getStringCell(Row row, ConditionSheetItem item) throws FormatException {
    assert row != null;
    assert item != null;
    Cell cell = row.getCell(item.getCol());
    if (cell == null || cell.getCellType() == Cell.CELL_TYPE_BLANK) {
        return ""; //$NON-NLS-1$
    } else if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
        return cell.getStringCellValue();
    }/*from w  w w.j a  v a  2 s  .co m*/
    throw new FormatException(
            MessageFormat.format(Messages.getString("LegacyExcelRuleExtractor.errorInvalidStringCell"), //$NON-NLS-1$
                    item.getName(), cell.getRowIndex() + 1, cell.getColumnIndex() + 1));
}

From source file:com.bayareasoftware.chartengine.ds.util.ExcelInference.java

License:Apache License

private void loadStrings() {
    int last = sheet.getLastRowNum();
    int first = sheet.getFirstRowNum();
    DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
    for (int i = 0/*first*/; i < last && i < maxrows; i++) {
        HSSFRow row = sheet.getRow(i);/*  w w  w .j  a v  a2s  .c om*/
        if (row == null) {
            rawStrings.add(new String[0]);
            continue;
        }
        int count;// = row.getLastCellNum() - row.getFirstCellNum();
        count = row.getLastCellNum();
        eval.setCurrentRow(row);
        if (count < 0) {
            rawStrings.add(new String[0]);
            continue;
        }
        Iterator<Cell> iter = row.cellIterator();
        String[] s = new String[count];
        while (iter.hasNext()) {
            Cell cell = iter.next();
            //int col = cell.getCellNum();
            int col = cell.getRowIndex();
            if (col >= 0 && col < count) {
                s[col] = getCellString(cell, eval, fmt);
            } else {
                String msg = "cell at row=" + rawStrings.size() + " column=" + col + " is out of bounds.";
                throw new RuntimeException(msg);
            }
        }
        rawStrings.add(s);
    }
}

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

License:Apache License

/**
 * Extracts {@link IDataModel} from {@link Workbook} at given {@link ICellAddress}.
 * Useful for formula. If given {@link ICellAddress} contains formula it can be parsed.
 * Based on this parsed information a new {@link IDataModel} might be created.
 *//*from  w ww .  j a  va 2s  . co m*/
static IDataModel toDataModel(final Workbook book, final ICellAddress address) {
    if (book == null || address == null) {
        return null;
    }
    if (address instanceof A1RangeAddress) {
        throw new CalculationEngineException(
                "A1RangeAddress is not supported, only one cell can be converted to DataModel.");
    }

    Sheet s = book.getSheetAt(0); /* TODO: only one sheet is supported */
    Row r = s.getRow(address.a1Address().row());
    if (r == null) {
        return null;
    }
    Cell c = r.getCell(address.a1Address().column());
    if (c == null || CELL_TYPE_FORMULA != c.getCellType()) {
        return null;
    }

    return createDataModelFromCell(s, create((XSSFWorkbook) book),
            fromRowColumn(c.getRowIndex(), c.getColumnIndex()));
}

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

License:Apache License

/**
 * Extracts {@link IDataModel} from {@link Workbook} for given function name.
 * Useful for formula with particular functions. Does scan IDataModel for exact formula use and create new 
 * {@link IDataModel} for every formula found.
 *///from w w  w. j  a  v  a2 s.c  o  m
static List<IDataModel> toDataModels(final Workbook book, final String function) {
    if (book == null || function == null) {
        return emptyList();
    }
    List<IDataModel> list = new LinkedList<>();

    final FormulaParsingWorkbook parsingBook = create((XSSFWorkbook) book);

    Sheet s = book.getSheetAt(0); /* TODO: only one sheet is supported */
    for (Row r : s) {
        for (Cell c : r) {
            if (c == null || CELL_TYPE_FORMULA != c.getCellType()) {
                continue;
            }

            try {
                if (ConverterUtils.isFunctionInFormula(c.getCellFormula(), function)) {
                    list.add(createDataModelFromCell(s, parsingBook,
                            fromRowColumn(c.getRowIndex(), c.getColumnIndex())));
                }
            } catch (FormulaParseException e) {
                log.warn("Warning while parsing excel formula. Probably this is OK.", e);
            }
        }
    }

    return list;
}

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

License:Apache License

/**
 * Does the same logic as {@link #toDataModels(Workbook, String)}, but for each new {@link IDataModel} created
 * also created an instance of given {@link FunctionMeta}.
 */// www. java 2 s . c om
static <T extends FunctionMeta> Map<T, IDataModel> toMetaFunctions(Workbook book, Class<T> metaClass) {
    Map<T, IDataModel> map = new HashMap<>();

    book.addToolPack(Functions.getUdfFinder());
    final FormulaParsingWorkbook parsingBook = create((XSSFWorkbook) book);

    Sheet s = book.getSheetAt(0); /* TODO: only one sheet is supported */
    for (Row r : s) {
        for (Cell c : r) {
            if (c == null || CELL_TYPE_FORMULA != c.getCellType()) {
                continue;
            }

            try {
                String formula = c.getCellFormula();
                String keyword = metaClass.getAnnotation(FunctionMeta.MetaFunctionKeyword.class).value();

                if (!formula.startsWith(keyword)) {
                    continue;
                }

                IDataModel dataModel = createDataModelFromCell(s, parsingBook,
                        fromRowColumn(c.getRowIndex(), c.getColumnIndex()));
                T meta = createAttributeFunctionMeta(metaClass, formula, dataModel);

                map.put(meta, dataModel);
            } catch (Exception e) {
                log.debug("Warning while parsing custom excel formula. It is OK.", e);
            }
        }
    }

    return map;
}

From source file:com.dataart.spreadsheetanalytics.engine.graph.PoiDependencyGraphBuilder.java

License:Apache License

public static IExecutionGraph buildDependencyGraph(IDataModel dataModel, IA1Address cell) {
    if (dataModel == null) {
        throw new CalculationEngineException("DataModel and PoiModel are required to build dependency graph");
    }//  w w  w .j a  v  a  2s .  c  o  m

    PoiDependencyGraphBuilder db = new PoiDependencyGraphBuilder(dataModel);

    Sheet s = db.poiBook.getSheetAt(0); //TODO: works for only one sheet workbooks
    if (s == null) {
        return null;
    }
    Row r = s.getRow(cell.row());
    if (r == null) {
        return null;
    }
    Cell c = r.getCell(cell.column());
    if (c == null) {
        return null;
    }

    ExecutionGraphVertex v = ExecutionGraph
            .createVertex(A1Address.fromRowColumn(c.getRowIndex(), c.getColumnIndex()).address());
    db.state.addVertex(v);

    if (CELL_TYPE_FORMULA == c.getCellType()) {
        db.collect(v, c.getCellFormula());
    }

    return db.state;
}

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

License:Apache License

public static PoiProxyCell makeCell(PoiProxySheet sheet, Cell cell, Ptg[] ptgs) {
    final int row = cell.getRowIndex();
    final int col = cell.getColumnIndex();
    return new PoiProxyCell(sheet, row, col, ConverterUtils.resolveCellValue(cell), ptgs);
}