📄 writablesheetimpl.java
字号:
*
* @param row the rows whose cells are to be returned
* @return the cells on the given row
*/
public Cell[] getRow(int row)
{
// Find the last non-null cell
boolean found = false;
int col = numColumns - 1;
while (col >= 0 && !found)
{
if (getCell(col, row).getType() != CellType.EMPTY)
{
found = true;
}
else
{
col--;
}
}
// Only create entries for non-empty cells
Cell[] cells = new Cell[col+1];
for (int i = 0; i <= col; i++)
{
cells[i] = getCell(i, row);
}
return cells;
}
/**
* Gets all the cells on the specified column
*
* @param col the column whose cells are to be returned
* @return the cells on the specified column
*/
public Cell[] getColumn(int col)
{
// Find the last non-null cell
boolean found = false;
int row = numRows - 1;
while (row >= 0 && !found)
{
if (getCell(col, row).getType() != CellType.EMPTY)
{
found = true;
}
else
{
row--;
}
}
// Only create entries for non-empty cells
Cell[] cells = new Cell[row+1];
for (int i = 0; i <= row; i++)
{
cells[i] = getCell(col, i);
}
return cells;
}
/**
* Gets the name of this sheet
*
* @return the name of the sheet
*/
public String getName()
{
return name;
}
/**
* Inserts a blank row into this spreadsheet. If the row is out of range
* of the rows in the sheet, then no action is taken
*
* @param row the row to insert
*/
public void insertRow(int row)
{
if (row < 0 || row >= numRows)
{
return;
}
// Create a new array to hold the new rows. Grow it if need be
RowRecord[] oldRows = rows;
if (numRows == rows.length)
{
rows = new RowRecord[oldRows.length + rowGrowSize];
}
else
{
rows = new RowRecord[oldRows.length];
}
// Copy in everything up to the new row
System.arraycopy(oldRows, 0, rows, 0, row);
// Copy in the remaining rows
System.arraycopy(oldRows, row, rows, row+1, numRows - row);
// Increment all the internal row number by one
for (int i = row+1; i <= numRows; i++)
{
if (rows[i] != null)
{
rows[i].incrementRow();
}
}
// Adjust any hyperlinks
HyperlinkRecord hr = null;
Iterator i = hyperlinks.iterator();
while (i.hasNext())
{
hr = (HyperlinkRecord) i.next();
hr.insertRow(row);
}
// Adjust any data validations
if (dataValidation != null)
{
dataValidation.insertRow(row);
}
// Adjust any merged cells
mergedCells.insertRow(row);
// Adjust any page breaks
ArrayList newRowBreaks = new ArrayList();
Iterator ri = rowBreaks.iterator();
while (ri.hasNext())
{
int val = ( (Integer) ri.next()).intValue();
if (val >= row)
{
val++;
}
newRowBreaks.add(new Integer(val));
}
rowBreaks = newRowBreaks;
// Adjust any conditional formats
for (Iterator cfit = conditionalFormats.iterator(); cfit.hasNext() ;)
{
ConditionalFormat cf = (ConditionalFormat) cfit.next();
cf.insertRow(row);
}
// Handle interested cell references on the main workbook
if (workbookSettings.getFormulaAdjust())
{
workbook.rowInserted(this, row);
}
// Adjust the maximum row record
numRows++;
}
/**
* Inserts a blank column into this spreadsheet. If the column is out of
* range of the columns in the sheet, then no action is taken
*
* @param col the column to insert
*/
public void insertColumn(int col)
{
if (col < 0 || col >= numColumns)
{
return;
}
// Iterate through all the row records adding in the column
for (int i = 0 ; i < numRows ; i++)
{
if (rows[i] != null)
{
rows[i].insertColumn(col);
}
}
// Adjust any hyperlinks
HyperlinkRecord hr = null;
Iterator i = hyperlinks.iterator();
while (i.hasNext())
{
hr = (HyperlinkRecord) i.next();
hr.insertColumn(col);
}
// Iterate through the column views, incrementing the column number
i = columnFormats.iterator();
while (i.hasNext())
{
ColumnInfoRecord cir = (ColumnInfoRecord) i.next();
if (cir.getColumn() >= col)
{
cir.incrementColumn();
}
}
// Iterate through the autosized columns, incrementing the column number
if (autosizedColumns.size() > 0)
{
TreeSet newAutosized = new TreeSet();
i = autosizedColumns.iterator();
while (i.hasNext())
{
Integer colnumber = (Integer) i.next();
if (colnumber.intValue() >= col)
{
newAutosized.add(new Integer(colnumber.intValue() + 1));
}
else
{
newAutosized.add(colnumber);
}
}
autosizedColumns = newAutosized;
}
// Handle any data validations
if (dataValidation != null)
{
dataValidation.insertColumn(col);
}
// Adjust any merged cells
mergedCells.insertColumn(col);
// Adjust any page breaks
ArrayList newColumnBreaks = new ArrayList();
Iterator ri = columnBreaks.iterator();
while (ri.hasNext())
{
int val = ( (Integer) ri.next()).intValue();
if (val >= col)
{
val++;
}
newColumnBreaks.add(new Integer(val));
}
columnBreaks = newColumnBreaks;
// Adjust any conditional formats
for (Iterator cfit = conditionalFormats.iterator(); cfit.hasNext() ;)
{
ConditionalFormat cf = (ConditionalFormat) cfit.next();
cf.insertColumn(col);
}
// Handle interested cell references on the main workbook
if (workbookSettings.getFormulaAdjust())
{
workbook.columnInserted(this, col);
}
numColumns++;
}
/**
* Removes a column from this spreadsheet. If the column is out of range
* of the columns in the sheet, then no action is taken
*
* @param col the column to remove
*/
public void removeColumn(int col)
{
if (col < 0 || col >= numColumns)
{
return;
}
// Iterate through all the row records removing the column
for (int i = 0 ; i < numRows ; i++)
{
if (rows[i] != null)
{
rows[i].removeColumn(col);
}
}
// Adjust any hyperlinks
HyperlinkRecord hr = null;
Iterator i = hyperlinks.iterator();
while (i.hasNext())
{
hr = (HyperlinkRecord) i.next();
if (hr.getColumn() == col &&
hr.getLastColumn() == col)
{
// The row with the hyperlink on has been removed, so get
// rid of it from the list
hyperlinks.remove(hyperlinks.indexOf(hr));
}
else
{
hr.removeColumn(col);
}
}
// Adjust any data validations
if (dataValidation != null)
{
dataValidation.removeColumn(col);
}
// Adjust any merged cells
mergedCells.removeColumn(col);
// Adjust any page breaks
ArrayList newColumnBreaks = new ArrayList();
Iterator ri = columnBreaks.iterator();
while (ri.hasNext())
{
int val = ( (Integer) ri.next()).intValue();
if (val != col)
{
if (val > col)
{
val--;
}
newColumnBreaks.add(new Integer(val));
}
}
columnBreaks = newColumnBreaks;
// Iterate through the column views, decrementing the column number
i = columnFormats.iterator();
ColumnInfoRecord removeColumn = null;
while (i.hasNext())
{
ColumnInfoRecord cir = (ColumnInfoRecord) i.next();
if (cir.getColumn() == col)
{
removeColumn = cir;
}
else if (cir.getColumn() > col)
{
cir.decrementColumn();
}
}
if (removeColumn != null)
{
columnFormats.remove(removeColumn);
}
// Iterate through the autosized columns, decrementing the column number
if (autosizedColumns.size() > 0)
{
TreeSet newAutosized = new TreeSet();
i = autosizedColumns.iterator();
while (i.hasNext())
{
Integer colnumber = (Integer) i.next();
if (colnumber.intValue() == col)
{
// do nothing
}
else if (colnumber.intValue() > col)
{
newAutosized.add(new Integer(colnumber.intValue() - 1));
}
else
{
newAutosized.add(colnumber);
}
}
autosizedColumns = newAutosized;
}
// Adjust any conditional formats
for (Iterator cfit = conditionalFormats.iterator(); cfit.hasNext() ;)
{
ConditionalFormat cf = (ConditionalFormat) cfit.next();
cf.removeColumn(col);
}
// Handle interested cell references on the main workbook
if (workbookSettings.getFormulaAdjust())
{
workbook.columnRemoved(this, col);
}
numColumns--;
}
/**
* Removes a row from this spreadsheet. If the row is out of
* range of the columns in the sheet, then no action is taken
*
* @param row the row to remove
*/
public void removeRow(int row)
{
if (row < 0 || row >= numRows)
{
return;
}
// Create a new array to hold the new rows. Grow it if need be
RowRecord[] oldRows = rows;
rows = new RowRecord[oldRows.length];
// Copy in everything up to the row to be removed
System.arraycopy(oldRows, 0, rows, 0, row);
// Copy in the remaining rows
System.arraycopy(oldRows, row + 1, rows, row, numRows - (row + 1));
// Decrement all the internal row numbers by one
for (int i = row; i < numRows; i++)
{
if (rows[i] != null)
{
rows[i].decrementRow();
}
}
// Adjust any hyperlinks
HyperlinkRecord hr = null;
Iterator i = hyperlinks.iterator();
while (i.hasNext())
{
hr = (HyperlinkRecord) i.next();
if (hr.getRow() == row &&
hr.getLastRow() == row)
{
// The row with the hyperlink on has been removed, so get
// rid of it from the list
i.remove();
}
else
{
hr.removeRow(row);
}
}
// Adjust any data validations
if (dataValidation != null)
{
dataValidation.removeRow(row);
}
// Adjust any merged cells
mergedCells.removeRow(row);
// Adjust any page breaks
ArrayList newRowBreaks = new ArrayList();
Iterator ri = rowBreaks.iterator();
while (ri.hasNext())
{
int val = ( (Integer) ri.next()).intValue();
if (val != row)
{
if (val > row)
{
val--;
}
newRowBreaks.add(new Integer(val));
}
}
rowBreaks = newRowBreaks;
// Adjust any conditional formats
for (Iterator cfit = conditionalFormats.iterator(); cfit.hasNext() ;)
{
ConditionalFormat cf = (ConditionalFormat) cfit.next();
cf.removeRow(row);
}
// Handle interested cell references on the main workbook
if (workbookSettings.getFormulaAdjust())
{
workbook.rowRemoved(this, row);
}
// Adjust any drawings
/*
if (drawings != null)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -