/* ==================================================================== 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.usermodel; import java.util.Iterator; import java.util.NoSuchElementException; import org.apache.poi.hssf.record.CellValueRecordInterface; import org.apache.poi.hssf.record.RowRecord; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; /** * High level representation of a row of a spreadsheet. * * Only rows that have cells should be added to a Sheet. * @version 1.0-pre * @author Andrew C. Oliver (acoliver at apache dot org) * @author Glen Stampoultzis (glens at apache.org) */ public final class HSSFRow implements Comparable, Row { // used for collections public final static int INITIAL_CAPACITY = 5; private int rowNum; private HSSFCell[] cells=new HSSFCell[INITIAL_CAPACITY]; /** * reference to low level representation */ private RowRecord row; /** * reference to containing low level Workbook */ private HSSFWorkbook book; /** * reference to containing Sheet */ private HSSFSheet sheet; /** * Creates new HSSFRow from scratch. Only HSSFSheet should do this. * * @param book low-level Workbook object containing the sheet that contains this row * @param sheet low-level Sheet object that contains this Row * @param rowNum the row number of this row (0 based) * @see org.apache.poi.hssf.usermodel.HSSFSheet#createRow(int) */ HSSFRow(HSSFWorkbook book, HSSFSheet sheet, int rowNum) { this.rowNum = rowNum; this.book = book; this.sheet = sheet; row = new RowRecord(rowNum); setRowNum(rowNum); } /** * Creates an HSSFRow from a low level RowRecord object. Only HSSFSheet should do * this. HSSFSheet uses this when an existing file is read in. * * @param book low-level Workbook object containing the sheet that contains this row * @param sheet low-level Sheet object that contains this Row * @param record the low level api object this row should represent * @see org.apache.poi.hssf.usermodel.HSSFSheet#createRow(int) */ HSSFRow(HSSFWorkbook book, HSSFSheet sheet, RowRecord record) { this.book = book; this.sheet = sheet; row = record; setRowNum(record.getRowNumber()); } /** * @deprecated (Aug 2008) use {@link HSSFRow#createCell(int) } */ public HSSFCell createCell(short columnIndex) { return createCell((int)columnIndex); } /** * @deprecated (Aug 2008) use {@link HSSFRow#createCell(int, int) } */ public HSSFCell createCell(short columnIndex, int type) { return createCell((int)columnIndex, type); } /** * Use this to create new cells within the row and return it. *
* The cell that is returned is a CELL_TYPE_BLANK. The type can be changed
* either through calling setCellValue or setCellType.
*
* @param column - the column number this cell represents
*
* @return HSSFCell a high level representation of the created cell.
*/
public HSSFCell createCell(int column)
{
return this.createCell(column,HSSFCell.CELL_TYPE_BLANK);
}
/**
* Use this to create new cells within the row and return it.
*
* The cell that is returned is a CELL_TYPE_BLANK. The type can be changed
* either through calling setCellValue or setCellType.
*
* @param columnIndex - the column number this cell represents
*
* @return HSSFCell a high level representation of the created cell.
*/
public HSSFCell createCell(int columnIndex, int type)
{
short shortCellNum = (short)columnIndex;
if(columnIndex > 0x7FFF) {
shortCellNum = (short)(0xffff - columnIndex);
}
HSSFCell cell = new HSSFCell(book, sheet, getRowNum(), shortCellNum, type);
addCell(cell);
sheet.getSheet().addValueRecord(getRowNum(), cell.getCellValueRecord());
return cell;
}
/**
* remove the HSSFCell from this row.
* @param cell to remove
*/
public void removeCell(Cell cell) {
if(cell == null) {
throw new IllegalArgumentException("cell must not be null");
}
removeCell((HSSFCell)cell, true);
}
private void removeCell(HSSFCell cell, boolean alsoRemoveRecords) {
short column=cell.getCellNum();
if(column < 0) {
throw new RuntimeException("Negative cell indexes not allowed");
}
if(column >= cells.length || cell != cells[column]) {
throw new RuntimeException("Specified cell is not from this row");
}
cells[column]=null;
if(alsoRemoveRecords) {
CellValueRecordInterface cval = cell.getCellValueRecord();
sheet.getSheet().removeValueRecord(getRowNum(), cval);
}
if (cell.getCellNum()+1 == row.getLastCol()) {
row.setLastCol((short) (findLastCell(row.getLastCol())+1));
}
if (cell.getCellNum() == row.getFirstCol()) {
row.setFirstCol(findFirstCell(row.getFirstCol()));
}
}
/**
* Removes all the cells from the row, and their
* records too.
*/
protected void removeAllCells() {
for(int i=0; i
* short minColIx = row.getFirstCellNum();
* short maxColIx = row.getLastCellNum();
* for(short colIx=minColIx; colIx<maxColIx; colIx++) {
* HSSFCell cell = row.getCell(colIx);
* if(cell == null) {
* continue;
* }
* //... do something with cell
* }
*
*
* @return short representing the last logical cell in the row PLUS ONE, or -1 if the
* row does not contain any cells.
*/
public short getLastCellNum() {
if (getPhysicalNumberOfCells() == 0) {
return -1;
}
return row.getLastCol();
}
/**
* gets the number of defined cells (NOT number of cells in the actual row!).
* That is to say if only columns 0,4,5 have values then there would be 3.
* @return int representing the number of defined cells in the row.
*/
public int getPhysicalNumberOfCells()
{
int count=0;
for(int i=0;i