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.vaadin.addon.spreadsheet.CellValueManager.java

License:Open Source License

/**
 * Marks the given cell as deleted and notifies the evaluator
 *
 * @param cell//from w  ww .ja  va  2s . c o  m
 *            Deleted cell
 */
protected void cellDeleted(Cell cell) {
    getFormulaEvaluator().notifyDeleteCell(cell);
    spreadsheet.removeInvalidFormulaMark(cell.getColumnIndex() + 1, cell.getRowIndex() + 1);
    markCellForRemove(cell);
}

From source file:com.vaadin.addon.spreadsheet.CellValueManager.java

License:Open Source License

/**
 * Marks the given cell for removal./* w w w  .  j a va 2 s  . c o  m*/
 *
 * @param cell
 *            Cell to mark for removal
 */
protected void markCellForRemove(Cell cell) {
    String cellKey = SpreadsheetUtil.toKey(cell);
    CellData cd = new CellData();
    cd.col = cell.getColumnIndex() + 1;
    cd.row = cell.getRowIndex() + 1;
    removedCells.add(cd);
    clearCellCache(cellKey);
}

From source file:com.vaadin.addon.spreadsheet.CellValueManager.java

License:Open Source License

/**
 * Method for updating cells that are marked for update and formula cells.
 *
 * Iterates over the whole sheet (existing rows and columns) and updates
 * client side cache for all sent formula cells, and cells that have been
 * marked for updating.//w ww .j a v a  2  s .co  m
 *
 */
protected void updateMarkedCellValues() {
    final ArrayList<CellData> updatedCellData = new ArrayList<CellData>();
    Sheet sheet = spreadsheet.getActiveSheet();
    // it is unnecessary to worry about having custom components in the cell
    // because the client side handles it -> it will not replace a custom
    // component with a cell value

    // update all cached formula cell values on client side, because they
    // might have changed. also make sure all marked cells are updated
    Iterator<Row> rows = sheet.rowIterator();
    while (rows.hasNext()) {
        final Row r = rows.next();
        final Iterator<Cell> cells = r.cellIterator();
        while (cells.hasNext()) {
            final Cell cell = cells.next();
            int rowIndex = cell.getRowIndex();
            int columnIndex = cell.getColumnIndex();
            final String key = SpreadsheetUtil.toKey(columnIndex + 1, rowIndex + 1);
            CellData cd = createCellDataForCell(cell);
            // update formula cells
            if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
                if (cd != null) {
                    if (sentFormulaCells.contains(key) || markedCells.contains(key)) {
                        sentFormulaCells.add(key);
                        updatedCellData.add(cd);
                    }
                } else if (sentFormulaCells.contains(key)) {
                    // in case the formula cell value has changed to null or
                    // empty; this case is probably quite rare, formula cell
                    // pointing to a cell that was removed or had its value
                    // cleared ???
                    sentFormulaCells.add(key);
                    cd = new CellData();
                    cd.col = columnIndex + 1;
                    cd.row = rowIndex + 1;
                    cd.cellStyle = "" + cell.getCellStyle().getIndex();
                    updatedCellData.add(cd);
                }
            } else if (markedCells.contains(key)) {
                sentCells.add(key);
                updatedCellData.add(cd);
            }
        }
    }
    if (!changedFormulaCells.isEmpty()) {
        fireFormulaValueChangeEvent(changedFormulaCells);
        changedFormulaCells = new HashSet<CellReference>();
    }
    // empty cells have cell data with just col and row
    updatedCellData.addAll(removedCells);
    if (!updatedCellData.isEmpty()) {
        spreadsheet.getRpcProxy().cellsUpdated(updatedCellData);
        spreadsheet.getRpcProxy().refreshCellStyles();
    }
    markedCells.clear();
    removedCells.clear();
}

From source file:com.vaadin.addon.spreadsheet.Spreadsheet.java

License:Open Source License

/**
 * Gets the locked state of the given cell.
 * /*from w w w  . j  a v  a2 s  .c  o m*/
 * @param cell
 *            The cell to check
 * @return true if the cell is locked, false otherwise
 */
public boolean isCellLocked(Cell cell) {
    if (isActiveSheetProtected()) {
        if (cell != null) {
            if (cell.getCellStyle().getIndex() != 0) {
                return cell.getCellStyle().getLocked();
            } else {
                return getState(false).lockedColumnIndexes.contains(cell.getColumnIndex() + 1)
                        && getState(false).lockedRowIndexes.contains(cell.getRowIndex() + 1);
            }
        } else {
            return true;
        }
    } else {
        return false;
    }
}

From source file:com.vaadin.addon.spreadsheet.SpreadsheetStyleFactory.java

License:Open Source License

/**
 * This should be called when a Cell's styling has been changed. This will
 * tell the Spreadsheet to send the change to the client side.
 * //from   w ww .  j  av a2 s .com
 * @param cell
 *            Target cell
 * @param updateCustomBorders
 *            true to also update custom borders
 */
public void cellStyleUpdated(Cell cell, boolean updateCustomBorders) {
    final String cssSelector = ".col" + (cell.getColumnIndex() + 1) + ".row" + (cell.getRowIndex() + 1);
    final Integer key = (int) cell.getCellStyle().getIndex();
    // remove/modify all possible old custom styles that the cell had (can
    // be found from state)
    ArrayList<String> add = new ArrayList<String>();
    Iterator<String> iterator = spreadsheet.getState().shiftedCellBorderStyles.iterator();
    while (iterator.hasNext()) {
        String style = iterator.next();
        // only cell with this style -> remove
        if (style.startsWith(cssSelector + "{")) {
            iterator.remove();
        } else if (style.contains(cssSelector)) { // shifted borders
            iterator.remove();
            int index = style.indexOf(cssSelector);
            if (index > 0) { // doesn't start with the selector
                style = style.replace(cssSelector + ",", "");
                // in case it is the last
                style = style.replace("," + cssSelector + "{", "{");
            } else {
                style = style.replace(cssSelector + ",", "");
                // in case it is the only
                style = style.replace(cssSelector + "{", "{");
            }
            if (!style.startsWith(",") && !style.startsWith("{")) {
                add.add(style);
            }
        }
    }
    for (String s : add) {
        spreadsheet.getState().shiftedCellBorderStyles.add(s);
    }
    // remove the cell's new custom styles from state (will be added again
    // as this cell is styled)
    if (shiftedBorderLeftStyles.containsKey(key)) {
        final String style = shiftedBorderLeftStyles.get(key);
        spreadsheet.getState().shiftedCellBorderStyles.remove(style);
    }
    if (shiftedBorderTopStyles.containsKey(key)) {
        final String style = shiftedBorderTopStyles.get(key);
        spreadsheet.getState().shiftedCellBorderStyles.remove(style);
    }
    if (mergedCellBorders.containsKey(cssSelector)) {
        final String style = buildMergedCellBorderCSS(cssSelector, mergedCellBorders.remove(cssSelector));
        spreadsheet.getState().shiftedCellBorderStyles.remove(style);
    }

    // TODO May need optimizing since the client side might already have
    // this cell style
    CellStyle cellStyle = cell.getCellStyle();
    addCellStyleCSS(cellStyle);

    shiftedBorderTopStylesMap.clear();
    shiftedBorderLeftStylesMap.clear();
    // custom styles
    doCellCustomStyling(cell);
    updateStyleMap(shiftedBorderLeftStylesMap, shiftedBorderLeftStyles);
    updateStyleMap(shiftedBorderTopStylesMap, shiftedBorderTopStyles);
    if (updateCustomBorders) {
        if (shiftedBorderLeftStyles.containsKey(key)) {
            final String style = shiftedBorderLeftStyles.get(key);
            spreadsheet.getState().shiftedCellBorderStyles.add(style);
        }
        if (shiftedBorderTopStyles.containsKey(key)) {
            final String style = shiftedBorderTopStyles.get(key);
            spreadsheet.getState().shiftedCellBorderStyles.add(style);
        }
        if (mergedCellBorders.containsKey(cssSelector)) {
            spreadsheet.getState().shiftedCellBorderStyles
                    .add(buildMergedCellBorderCSS(cssSelector, mergedCellBorders.get(cssSelector)));
        }
    }
}

From source file:com.vaadin.addon.spreadsheet.SpreadsheetStyleFactory.java

License:Open Source License

private void doCellCustomStyling(final Cell cell) {
    CellStyle cellStyle = cell.getCellStyle();
    final Integer key = (int) cellStyle.getIndex();
    if (key == 0) { // default style
        return;//from ww w .j  a va2  s . com
    }

    // merged regions have their borders in edge cells that are "invisible"
    // inside the region -> right and bottom cells need to be transfered to
    // the actual merged cell
    final int columnIndex = cell.getColumnIndex();
    final int rowIndex = cell.getRowIndex();
    MergedRegion region = spreadsheet.mergedRegionContainer.getMergedRegion((columnIndex + 1), (rowIndex + 1));
    if (region != null) {
        final String borderRight = getBorderRightStyle(cellStyle);
        final String borderBottom = getBorderBottomStyle(cellStyle);
        if ((borderRight != null && !borderRight.isEmpty())
                || (borderBottom != null && !borderBottom.isEmpty())) {
            StringBuilder sb = new StringBuilder(".col");
            sb.append(region.col1);
            sb.append(".row");
            sb.append(region.row1);
            final String cssKey = sb.toString();
            final String currentBorders = mergedCellBorders.get(cssKey);
            StringBuilder style;
            if (currentBorders != null && !currentBorders.isEmpty()) {
                style = new StringBuilder(currentBorders);
            } else {
                style = new StringBuilder();
            }
            if (borderRight != null && !borderRight.isEmpty()
                    && (currentBorders == null || !currentBorders.contains("border-right"))) {
                style.append(borderRight);
            }
            if (borderBottom != null && !borderBottom.isEmpty()
                    && (currentBorders == null || !currentBorders.contains("border-bottom"))) {
                style.append(borderBottom);
            }
            final String newBorders = style.toString();
            if (!newBorders.isEmpty()) {
                mergedCellBorders.put(cssKey, newBorders);
            }
        }

    }

    // only take transfered borders into account on the (possible) merged
    // regions edges
    if (region == null || region.col1 == (columnIndex + 1) || region.col2 == (columnIndex + 1)
            || region.row1 == (rowIndex + 1) || region.row2 == (rowIndex + 1)) {

        if (shiftedBorderLeftStyles.containsKey(key)) {
            // need to add the border right style to previous cell on
            // left, which might be a merged cell
            if (columnIndex > 0) {
                int row, col;

                MergedRegion previousRegion = spreadsheet.mergedRegionContainer.getMergedRegion(columnIndex,
                        rowIndex + 1);
                if (previousRegion != null) {
                    col = previousRegion.col1;
                    row = previousRegion.row1;
                } else {
                    col = columnIndex;
                    row = rowIndex + 1;
                }
                insertMapEntryIfNeeded(shiftedBorderLeftStylesMap, key, row, col);
            }
        }
        if (shiftedBorderTopStyles.containsKey(key)) {
            // need to add the border bottom style to cell on previous
            // row, which might be a merged cell
            if (rowIndex > 0) {
                int row, col;
                MergedRegion previousRegion = spreadsheet.mergedRegionContainer.getMergedRegion(columnIndex + 1,
                        rowIndex);

                if (previousRegion != null) {
                    col = previousRegion.col1;
                    row = previousRegion.row1;
                } else {
                    col = columnIndex + 1;
                    row = rowIndex;
                }
                insertMapEntryIfNeeded(shiftedBorderTopStylesMap, key, row, col);

            }
        }

    }
}

From source file:com.vaadin.addon.spreadsheet.SpreadsheetUtil.java

License:Open Source License

/**
 * Translates cell coordinates from the given Cell object to a cell key used
 * to identify cells in the server<->client communiScation.
 * /*from ww  w.  j  ava2s  .c  om*/
 * @param cell
 *            Cell to fetch the coordinates from
 * @return Cell key
 */
public static final String toKey(Cell cell) {
    return toKey(cell.getColumnIndex() + 1, cell.getRowIndex() + 1);
}

From source file:csv.impl.DefaultExcelFormatter.java

License:Open Source License

/**
 * Sets the cell style./*from   w w w .j a  va2 s .  c o  m*/
 * This implementations calls various other methods to define
 * the style of the cell.
 * @param writer writer that requires the information
 * @param cell cell to be formatted
 * @param value value in cell
 * @see #getFormat(ExcelWriter, int, int, Object)
 * @see #getBackgroundColor(ExcelWriter, int, int, Object)
 * @see #getFillPattern(ExcelWriter, int, int, Object)
 * @see #getForegroundColor(ExcelWriter, int, int, Object)
 * @see #getFont(ExcelWriter, int, int, Object)
 * @see #getAlign(ExcelWriter, int, int, Object)
 * @see #getHyperlink(ExcelWriter, int, int, Object)
 */
@Override
public void setStyle(ExcelWriter writer, Cell cell, Object value) {
    int row = cell.getRowIndex();
    int column = cell.getColumnIndex();

    StyleDescription desc = new StyleDescription();

    // Collect cell style and check if we already had it before

    // data format
    desc.setFormat(getFormat(writer, row, column, value));
    desc.setFgColor(getForegroundColor(writer, row, column, value));
    desc.setFillPattern(getFillPattern(writer, row, column, value));
    desc.setBgColor(getBackgroundColor(writer, row, column, value));

    // Font
    desc.setFont(getFont(writer, row, column, value));

    // Borders
    desc.setTopBorderColor(getTopBorderColor(writer, row, column, value));
    desc.setLeftBorderColor(getLeftBorderColor(writer, row, column, value));
    desc.setRightBorderColor(getRightBorderColor(writer, row, column, value));
    desc.setBottomBorderColor(getBottomBorderColor(writer, row, column, value));
    desc.setTopBorderThickness(getTopBorderThickness(writer, row, column, value));
    desc.setLeftBorderThickness(getLeftBorderThickness(writer, row, column, value));
    desc.setRightBorderThickness(getRightBorderThickness(writer, row, column, value));
    desc.setBottomBorderThickness(getBottomBorderThickness(writer, row, column, value));
    desc.setTextWrap(isTextWrap(writer, row, column, value));

    // Alignment
    desc.setAlignment(getAlign(writer, row, column, value));

    if (!desc.isDefault()) {
        CellStyle style = styles.get(desc);
        if (style == null) {
            style = writer.getWorkbook().createCellStyle();
            styles.put(desc, style);
            desc.applyStyle(style);
        }

        desc.applyStyle(style);

        // set style
        cell.setCellStyle(style);
    }

    // Set a hyperlink
    Hyperlink link = getHyperlink(writer, row, column, value);
    if (link != null)
        cell.setHyperlink(link);

}

From source file:de.enerko.reports2.engine.CellDefinition.java

License:Apache License

public CellDefinition(final String sheetname, final Cell cell) {
    final int ct = cell.getCellType();

    Method m = null;//from  w w  w . j ava  2  s. c o  m
    try {
        m = this.getClass().getDeclaredMethod("parse_" + Report.IMPORTABLE_CELL_TYPES.get(new Integer(ct)),
                new Class[] { Cell.class });
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (m == null)
            throw new RuntimeException("Invalid type " + ct);
    }

    try {
        final CellValue cellValue = (CellValue) m.invoke(this, new Object[] { cell });
        this.sheetname = sheetname;
        this.column = cell.getColumnIndex();
        this.row = cell.getRowIndex();
        this.name = CellReferenceHelper.getCellReference(cell.getColumnIndex(), cell.getRowIndex());
        this.type = cellValue.type;
        this.value = cellValue.representation;
        if (cell.getCellComment() == null || cell.getCellComment().getString() == null)
            this.comment = null;
        else
            this.comment = new CommentDefinition(cell.getCellComment());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:de.enerko.reports2.engine.Report.java

License:Apache License

/**
 * This method adds a new cell to the sheet of a workbook. It could 
 * (together with {@link #fill(Workbook, Cell, String, String, boolean)}) be moved to
 * the {@link CellDefinition} itself, but that would mean that the {@link CellDefinition} is
 * tied to a specific Excel API. Having those methods here allows the Report to become
 * an interface if a second engine (i.e. JXL) should be added in the future.
 * @param workbook//  w w w  .  j ava  2 s  . c  o m
 * @param sheet
 * @param cellDefinition
 */
private void addCell(final Workbook workbook, final Sheet sheet, final CellDefinition cellDefinition) {
    final int columnNum = cellDefinition.column, rowNum = cellDefinition.row;

    Row row = sheet.getRow(rowNum);
    if (row == null)
        row = sheet.createRow(rowNum);

    Cell cell = row.getCell(columnNum);
    // If the cell already exists and is no blank cell
    // it will be used including all formating
    if (cell != null && cell.getCellType() != Cell.CELL_TYPE_BLANK) {
        cell = fill(workbook, cell, cellDefinition, false);
    }
    // Otherwise a new cell will be created, the datatype set and 
    // optionally a format will be created
    else {
        cell = fill(workbook, row.createCell(columnNum), cellDefinition, true);

        final Sheet referenceSheet;
        if (cellDefinition.getReferenceCell() != null
                && (referenceSheet = workbook.getSheet(cellDefinition.getReferenceCell().sheetname)) != null) {
            final Row referenceRow = referenceSheet.getRow(cellDefinition.getReferenceCell().row);
            final Cell referenceCell = referenceRow == null ? null
                    : referenceRow.getCell(cellDefinition.getReferenceCell().column);
            if (referenceCell != null && referenceCell.getCellStyle() != null)
                cell.setCellStyle(referenceCell.getCellStyle());
        }
    }

    // Add an optional comment      
    if (cellDefinition.hasComment()) {
        final CreationHelper factory = workbook.getCreationHelper();

        final Drawing drawing = sheet.createDrawingPatriarch();
        final ClientAnchor commentAnchor = factory.createClientAnchor();

        final int col1 = cellDefinition.comment.column == null ? cell.getColumnIndex() + 1
                : cellDefinition.comment.column;
        final int row1 = cellDefinition.comment.row == null ? cell.getRowIndex() : cellDefinition.comment.row;

        commentAnchor.setCol1(col1);
        commentAnchor.setRow1(row1);
        commentAnchor.setCol2(col1 + Math.max(1, cellDefinition.comment.width));
        commentAnchor.setRow2(row1 + Math.max(1, cellDefinition.comment.height));

        final Comment comment = drawing.createCellComment(commentAnchor);
        comment.setString(factory.createRichTextString(cellDefinition.comment.text));
        comment.setAuthor(cellDefinition.comment.author);
        comment.setVisible(cellDefinition.comment.visible);

        cell.setCellComment(comment);
    }
}