Example usage for org.apache.poi.ss.usermodel Sheet getRow

List of usage examples for org.apache.poi.ss.usermodel Sheet getRow

Introduction

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

Prototype

Row getRow(int rownum);

Source Link

Document

Returns the logical row (not physical) 0-based.

Usage

From source file:com.vaadin.addon.spreadsheet.action.InsertDeleteCellCommentAction.java

License:Open Source License

@Override
public void executeActionOnSelection(Spreadsheet spreadsheet, SelectionChangeEvent event) {
    Sheet sheet = spreadsheet.getActiveSheet();
    CellReference cr = event.getSelectedCellReference();
    boolean cellCreated = false, rowCreated = false, commentEdited = false;
    Row row = sheet.getRow(cr.getRow());
    if (row == null) {
        row = sheet.createRow(cr.getRow());
        rowCreated = true;//from   w w w. j a  v a2s .c o  m
    }
    Cell cell = spreadsheet.getCell(cr);
    if (cell == null) {
        cell = row.createCell(cr.getCol());
        cellCreated = true;
    }
    if (cell.getCellComment() == null) {
        createCellComment(spreadsheet, sheet, cell, cr);
        commentEdited = true;
    } else {
        cell.removeCellComment();
        if (cellCreated) {
            sheet.getRow(cr.getRow()).removeCell(cell);
        }
        if (rowCreated) {
            sheet.removeRow(sheet.getRow(cr.getRow()));
        }
    }
    if (cell != null) {
        spreadsheet.refreshCells(cell);
    }
    if (commentEdited) {
        spreadsheet.editCellComment(cr);
    }
}

From source file:com.vaadin.addon.spreadsheet.action.MergeCellsAction.java

License:Open Source License

@Override
public boolean isApplicableForSelection(Spreadsheet spreadsheet, SelectionChangeEvent event) {
    if (event.getCellRangeAddresses().size() == 1 && event.getIndividualSelectedCells().size() == 0) {
        Sheet sheet = spreadsheet.getActiveSheet();
        if (isSheetProtected(sheet)) {
            CellRangeAddress selection = event.getCellRangeAddresses().get(0);
            for (int r = selection.getFirstRow(); r <= selection.getLastRow(); r++) {
                Row row = sheet.getRow(r);
                if (row != null) {
                    for (int c = selection.getFirstColumn(); c <= selection.getLastColumn(); c++) {
                        Cell cell = row.getCell(c);
                        if (isCellLocked(cell)) {
                            return false;
                        }//from w w w .j a  va 2s .  c om
                    }
                } else {
                    return false;
                }
            }
        }
        return true;
    }
    return false;
}

From source file:com.vaadin.addon.spreadsheet.action.ShowHideCellCommentAction.java

License:Open Source License

@Override
public void executeActionOnSelection(Spreadsheet spreadsheet, SelectionChangeEvent event) {
    CellReference cr = event.getSelectedCellReference();
    Comment cellComment = spreadsheet.getActiveSheet().getCellComment(cr.getRow(), cr.getCol());
    cellComment.setVisible(!cellComment.isVisible());
    Sheet sheet = spreadsheet.getActiveSheet();
    Row row = sheet.getRow(cr.getRow());
    if (row == null) {
        row = sheet.createRow(cr.getRow());
    }//www.j  a v a2 s. co  m
    Cell cell = spreadsheet.getCell(cr);
    if (cell == null) {
        cell = row.createCell(cr.getCol());
    }
    spreadsheet.refreshCells(cell);
}

From source file:com.vaadin.addon.spreadsheet.action.UnMergeCellsAction.java

License:Open Source License

@Override
public boolean isApplicableForSelection(Spreadsheet spreadsheet, SelectionChangeEvent event) {
    if (event.getSelectedCellMergedRegion() != null && event.getIndividualSelectedCells().size() == 0
            && event.getCellRangeAddresses().size() == 0) {
        Sheet sheet = spreadsheet.getActiveSheet();
        if (isSheetProtected(sheet)) {
            CellRangeAddress mergedCell = event.getSelectedCellMergedRegion();

            Row row = sheet.getRow(mergedCell.getFirstRow());
            if (row != null) {
                Cell cell = row.getCell(mergedCell.getFirstColumn());
                if (isCellLocked(cell)) {
                    return false;
                }//from w  w  w.j  a  va  2  s  . c  om
            } else {
                return false;
            }
        }
        return true;
    }
    return false;
}

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

License:Open Source License

private void shiftRowsDownInSelection(int newLastRow) {
    CellRangeAddress paintedCellRange = spreadsheet.getCellSelectionManager().getSelectedCellRange();
    int r1 = paintedCellRange.getFirstRow() + 1;
    int r2 = paintedCellRange.getLastRow() + 1;
    int c1 = paintedCellRange.getFirstColumn() + 1;
    int c2 = paintedCellRange.getLastColumn() + 1;
    Workbook workbook = spreadsheet.getWorkbook();
    final Sheet activeSheet = workbook.getSheetAt(workbook.getActiveSheetIndex());
    for (int shiftedRowIndex = r1; shiftedRowIndex <= r2; shiftedRowIndex++) {
        final Row shiftedRow = activeSheet.getRow(shiftedRowIndex - 1);
        int newRowIndex = r2 + 1 + (shiftedRowIndex - r1);
        while (newRowIndex <= newLastRow) {
            if (shiftedRow != null) {
                Row newRow = activeSheet.getRow(newRowIndex - 1);
                if (newRow == null) {
                    newRow = activeSheet.createRow(newRowIndex - 1);
                }//from   w  ww.  j  av a2  s. com
                for (int c = c1; c <= c2; c++) {
                    Double sequenceIncrement = getColumnSequenceIncrement(c, r1, r2);
                    Cell shiftedCell = shiftedRow.getCell(c - 1);
                    Cell newCell = newRow.getCell(c - 1);
                    if (shiftedCell != null) {
                        if (newCell == null) {
                            newCell = newRow.createCell(c - 1);
                        }
                        shiftCellValue(shiftedCell, newCell, false, sequenceIncrement);
                    } else if (newCell != null) {
                        // update style to 0
                        newCell.setCellStyle(null);
                        spreadsheet.getSpreadsheetStyleFactory().cellStyleUpdated(newCell, true);
                        newCell.setCellValue((String) null);
                        getCellValueManager().cellDeleted(newCell);
                    }
                }
            } else {
                getCellValueManager().removeCells(newRowIndex, c1, newRowIndex, c2, true);
            }
            newRowIndex += r2 - r1 + 1;
        }
    }
}

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

License:Open Source License

private void shiftRowsUpInSelection(int newFirstRow) {
    CellRangeAddress paintedCellRange = spreadsheet.getCellSelectionManager().getSelectedCellRange();
    int r1 = paintedCellRange.getFirstRow() + 1;
    int r2 = paintedCellRange.getLastRow() + 1;
    int c1 = paintedCellRange.getFirstColumn() + 1;
    int c2 = paintedCellRange.getLastColumn() + 1;
    Workbook workbook = spreadsheet.getWorkbook();
    final Sheet activeSheet = workbook.getSheetAt(workbook.getActiveSheetIndex());
    for (int shiftedRowIndex = r1; shiftedRowIndex <= r2; shiftedRowIndex++) {
        final Row shiftedRow = activeSheet.getRow(shiftedRowIndex - 1);
        int newRowIndex = r1 - 1 - (shiftedRowIndex - r1);
        while (newRowIndex >= newFirstRow) {
            if (shiftedRow != null) {
                Row newRow = activeSheet.getRow(newRowIndex - 1);
                if (newRow == null) {
                    newRow = activeSheet.createRow(newRowIndex - 1);
                }/*from  ww  w .j  a va 2 s  .  c o m*/
                for (int c = c1; c <= c2; c++) {
                    Double sequenceIncrement = getColumnSequenceIncrement(c, r1, r2);
                    Cell shiftedCell = shiftedRow.getCell(c - 1);
                    Cell newCell = newRow.getCell(c - 1);
                    if (shiftedCell != null) {
                        if (newCell == null) {
                            newCell = newRow.createCell(c - 1);
                        }
                        shiftCellValue(shiftedCell, newCell, false, sequenceIncrement);
                    } else if (newCell != null) {
                        // update style to 0
                        newCell.setCellStyle(null);
                        spreadsheet.getSpreadsheetStyleFactory().cellStyleUpdated(newCell, true);
                        newCell.setCellValue((String) null);
                        getCellValueManager().cellDeleted(newCell);
                    }
                }
            } else {
                getCellValueManager().removeCells(newRowIndex, c1, newRowIndex, c2, true);
            }
            newRowIndex = newRowIndex - (r2 - r1) - 1;
        }
    }
}

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

License:Open Source License

private void shiftColumnsRightInSelection(int newRightMostColumn) {
    CellRangeAddress paintedCellRange = spreadsheet.getCellSelectionManager().getSelectedCellRange();
    int r1 = paintedCellRange.getFirstRow() + 1;
    int r2 = paintedCellRange.getLastRow() + 1;
    int c1 = paintedCellRange.getFirstColumn() + 1;
    int c2 = paintedCellRange.getLastColumn() + 1;
    Workbook workbook = spreadsheet.getWorkbook();
    final Sheet activeSheet = workbook.getSheetAt(workbook.getActiveSheetIndex());
    for (int rIndex = r1; rIndex <= r2; rIndex++) {
        final Row row = activeSheet.getRow(rIndex - 1);
        if (row != null) {
            Double sequenceIncrement = getRowSequenceIncrement(rIndex, c1, c2);
            for (int shiftedCellIndex = c1; shiftedCellIndex <= c2; shiftedCellIndex++) {
                Cell shiftedCell = row.getCell(shiftedCellIndex - 1);
                int newCellIndex = c2 + 1 + (shiftedCellIndex - c1);
                while (newCellIndex <= newRightMostColumn) {
                    Cell newCell = row.getCell(newCellIndex - 1);
                    if (shiftedCell != null) {
                        if (newCell == null) {
                            newCell = row.createCell(newCellIndex - 1);
                        }/*from  w w w .  j a  va 2 s.c o m*/
                        shiftCellValue(shiftedCell, newCell, false, sequenceIncrement);

                    } else if (newCell != null) {
                        newCell.setCellValue((String) null);
                        getCellValueManager().cellDeleted(newCell);
                        // update style to 0
                        newCell.setCellStyle(null);
                        spreadsheet.getSpreadsheetStyleFactory().cellStyleUpdated(newCell, true);
                    }
                    newCellIndex += (c2 - c1) + 1;
                }
            }
        }
    }
}

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

License:Open Source License

private void shiftColumnsLeftInSelection(int newLeftMostColumn) {
    CellRangeAddress paintedCellRange = spreadsheet.getCellSelectionManager().getSelectedCellRange();
    int r1 = paintedCellRange.getFirstRow() + 1;
    int r2 = paintedCellRange.getLastRow() + 1;
    int c1 = paintedCellRange.getFirstColumn() + 1;
    int c2 = paintedCellRange.getLastColumn() + 1;
    Workbook workbook = spreadsheet.getWorkbook();
    final Sheet activeSheet = workbook.getSheetAt(workbook.getActiveSheetIndex());
    for (int rIndex = r1; rIndex <= r2; rIndex++) {
        final Row row = activeSheet.getRow(rIndex - 1);
        if (row != null) {
            Double sequenceIncrement = getRowSequenceIncrement(rIndex, c1, c2);
            for (int shiftedCellIndex = c1; shiftedCellIndex <= c2; shiftedCellIndex++) {
                Cell shiftedCell = row.getCell(shiftedCellIndex - 1);
                int newCellIndex = c1 - (shiftedCellIndex - c1) - 1;
                while (newCellIndex >= newLeftMostColumn) {
                    Cell newCell = row.getCell(newCellIndex - 1);
                    if (shiftedCell != null) {
                        if (newCell == null) {
                            newCell = row.createCell(newCellIndex - 1);
                        }//from  ww  w . j  a v a  2  s  . c  o m
                        shiftCellValue(shiftedCell, newCell, false, sequenceIncrement);
                    } else if (newCell != null) {
                        newCell.setCellValue((String) null);
                        getCellValueManager().cellDeleted(newCell);
                        // update style to 0
                        newCell.setCellStyle(null);
                        spreadsheet.getSpreadsheetStyleFactory().cellStyleUpdated(newCell, true);
                    }
                    newCellIndex = newCellIndex - (c2 - c1) - 1;
                }
            }
        }
    }
}

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

License:Open Source License

/**
 * Returns the increment between all consecutive cells in row with rIndex
 * from column c1 to column c2//from ww  w  .  j a va2 s.  co  m
 * 
 * @param rIndex
 *            Row index for the sequence recognition, 1-based
 * @param c1
 *            First column of the row to be considered, 1-based
 * @param c2
 *            Last column of the row to be considered, 1-based
 * @return common difference or null
 */
private Double getRowSequenceIncrement(int rIndex, int c1, int c2) {
    Double result = null;
    Workbook workbook = spreadsheet.getWorkbook();
    final Sheet activeSheet = workbook.getSheetAt(workbook.getActiveSheetIndex());
    final Row row = activeSheet.getRow(rIndex - 1);
    if (row != null) {
        Cell firstCell = row.getCell(c1 - 1);
        if (firstCell != null) {
            if (firstCell.getCellType() == CELL_TYPE_STRING) {
                return getSequenceIncrement(getRowStringValues(row, c1, c2));
            } else if (firstCell.getCellType() == CELL_TYPE_NUMERIC) {
                return getSequenceIncrement(getRowNumericValues(row, c1, c2));
            }
        }
    }
    return result;
}

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

License:Open Source License

/**
 * Returns an array with String values in column with columnIndex from row
 * r1 to row r2 in activeSheet until first non String cell or null value
 * Used by/*w w  w  . j  a  v a 2 s.c o m*/
 * {@link CellSelectionShifter#getColumnSequenceIncrement(int, int, int)}
 * 
 * @param activeSheet
 *            Sheet where the cells are going to be taken from
 * @param columnIndex
 *            Defines the origin of the cell values to be returned, 1-based
 * @param r1
 *            First row of the column to be returned, 1-based
 * @param r2
 *            Last row of the column to be returned, 1-based
 * @return String array with values
 */
private String[] getColumnStringValues(Sheet activeSheet, int columnIndex, int r1, int r2) {
    String[] result = new String[r2 - r1 + 1];
    Cell cell;
    Row row;
    for (int i = r1; i <= r2; i++) {
        row = activeSheet.getRow(i - 1);
        if (row != null) {
            cell = row.getCell(columnIndex - 1);
            if (cell != null && cell.getCellType() == CELL_TYPE_STRING) {
                result[i - r1] = cell.getStringCellValue();
            } else {
                break;
            }
        } else {
            break;
        }
    }
    return result;
}