public class Table extends Component
Table provides methods to get/add/delete/modify table column/row/cell.
Modifier and Type | Class and Description |
---|---|
static class |
Table.TableBuilder
This is a tool class which supplies all of the table creation detail.
|
Modifier and Type | Field and Description |
---|---|
protected Document |
mDocument |
protected boolean |
mIsCellStyleInheritance |
protected boolean |
mIsDescribedBySingleElement |
protected boolean |
mIsSpreadsheet |
Modifier and Type | Method and Description |
---|---|
Column |
appendColumn()
Append a column at the end of the table.
|
List<Column> |
appendColumns(int columnCount)
Append a specific number of columns to the right of the table.
|
Row |
appendRow()
Append a row to the end of the table.
|
List<Row> |
appendRows(int rowCount)
Append a specific number of rows to the end of the table.
|
void |
applyStyle(TableTemplate template)
Apply the formatting specified in the template to corresponding table cells.
|
Cell |
getCellByPosition(int colIndex,
int rowIndex)
Return a single cell that is positioned at the specified column and row.
|
Cell |
getCellByPosition(String address)
Return a single cell that is positioned at the specified cell address.
|
CellRange |
getCellRangeByName(String name)
Return a range of cells by a specified name.
|
CellRange |
getCellRangeByPosition(int startCol,
int startRow,
int endCol,
int endRow)
Return a range of cells within the specified range.
|
CellRange |
getCellRangeByPosition(String startAddress,
String endAddress)
Return a range of cells within the specified range.
|
Column |
getColumnByIndex(int index)
Get the column at the specified index.
|
int |
getColumnCount()
Get the column count of this table.
|
Iterator<Column> |
getColumnIterator()
Return an Iterator of the column in this table.
|
List<Column> |
getColumnList()
Return a list of columns in the current table.
|
int |
getHeaderColumnCount()
Return the number of header columns in the table.
|
int |
getHeaderRowCount()
Return the number of header rows in this table.
|
static Table |
getInstance(TableTableElement element)
Get a table feature instance by an instance of
TableTableElement . |
String |
getKeepWithNext() |
TableTableElement |
getOdfElement()
Return an instance of
TableTableElement which represents this feature. |
Row |
getRowByIndex(int index)
Get the row at the specified index.
|
int |
getRowCount()
Get the row count of this table.
|
Iterator<Row> |
getRowIterator()
Return an Iterator of the row in this table.
|
List<Row> |
getRowList()
Return a list of table rows in the current table.
|
DefaultStyleHandler |
getStyleHandler() |
String |
getTableName()
Return the table name.
|
double |
getWidth()
Get the width of the table (in Millimeter).
|
List<Column> |
insertColumnsBefore(int index,
int columnCount)
Insert a specific number of columns before the column whose index is
index . |
List<Row> |
insertRowsBefore(int index,
int rowCount)
Insert a specific number of rows before the row at
index . |
boolean |
isCellStyleInheritance()
Return true if cell style is inherited when a new cell is added to the table.
|
boolean |
isProtected()
Return true if the table is protected.
|
boolean |
isUseRepeat()
Return true if the new created multiple columns/rows/cells are described by a single element
when it's possible.
|
static Table |
newTable(TableContainer tableContainer)
Construct the
Table feature. |
static Table |
newTable(TableContainer tableContainer,
int numRows,
int numCols)
Construct the
Table feature with a specified row number and column number. |
static Table |
newTable(TableContainer tableContainer,
int numRows,
int numCols,
double marginLeft,
double marginRight)
Construct the
Table feature with a specified row number and column number. |
static Table |
newTable(TableContainer tableContainer,
int numRows,
int numCols,
int headerRowNumber,
int headerColumnNumber)
Construct the
Table feature with a specified row number, column number, header row
number, header column number. |
static Table |
newTable(TableContainer tableContainer,
String[] rowLabel,
String[] columnLabel,
double[][] data)
Construct the Table feature with a specified 2 dimension array as the data of this table.
|
static Table |
newTable(TableContainer tableContainer,
String[] rowLabel,
String[] columnLabel,
String[][] data)
Construct the Table feature with a specified 2 dimension array as the data of this table.
|
void |
remove()
Remove this table from the document
|
void |
removeColumnsByIndex(int startIndex,
int deleteColCount)
Remove a specific number of columns, starting from the column at
index . |
void |
removeRowsByIndex(int startIndex,
int deleteRowCount)
Remove the specific number of rows, starting from the row at
index . |
void |
setCellStyleInheritance(boolean isEnabled)
This method allows users to set whether cell style is inherited or not when a new cell is added
to the table.
|
void |
setKeepWithNext(boolean keepWithNext) |
void |
setProtected(boolean isProtected)
Set if the table is protected.
|
void |
setTableAlign(TableAlignAttribute.Value align)
Sets the table align.
|
void |
setTableName(String tableName)
Set the table name.
|
void |
setUseRepeat(boolean isSingle)
When two or more columns/rows/cells are added to a table, if they are adjoining, and have the
same content and style, and do not contain horizontally/vertically merged cells, they may be
described by a single element.
|
void |
setVerticalMargin(double spaceTop,
double spaceBottom)
Modifies the margin above and below the table.
|
void |
setWidth(double width)
Set the width of the table (in Millimeter).
|
getComponentByElement, getOwnerDocument, registerComponent, unregisterComponent
protected Document mDocument
protected boolean mIsSpreadsheet
protected boolean mIsCellStyleInheritance
protected boolean mIsDescribedBySingleElement
public static Table getInstance(TableTableElement element)
TableTableElement
.element
- an instance of TableTableElement
Table
that can represent element
public static Table newTable(TableContainer tableContainer)
Table
feature. The default column count is 5. The default row count
is 2.
The table will be inserted at the end of the container. An unique table name will be given,
you may set a custom table name using the setTableName
method.
If the tableContainer
is a text document, cell borders will be created by
default.
tableContainer
- the table container that contains this tableTable
feature instancepublic static Table newTable(TableContainer tableContainer, int numRows, int numCols)
Table
feature with a specified row number and column number.
The table will be inserted at the end of the tableContainer. An unique table name will be
given, you may set a custom table name using the setTableName
method.
If the tableContainer
is a text document, cell borders will be created by
default.
tableContainer
- the table container that contains this tablenumRows
- the row numbernumCols
- the column numberTable
public static Table newTable(TableContainer tableContainer, int numRows, int numCols, double marginLeft, double marginRight)
Table
feature with a specified row number and column number.
The table will be inserted at the end of the tableContainer. An unique table name will be
given, you may set a custom table name using the setTableName
method.
If the tableContainer
is a text document, cell borders will be created by
default.
tableContainer
- the table container that contains this tablenumRows
- the row numbernumCols
- the column numbermarginLeft
- double the left table margin in cm (between the left margin of document
and the table)marginRight
- double the right table margin in cm (between the right margin of document
and the table)Table
public static Table newTable(TableContainer tableContainer, int numRows, int numCols, int headerRowNumber, int headerColumnNumber)
Table
feature with a specified row number, column number, header row
number, header column number.
The table will be inserted at the end of the tableContainer. An unique table name will be
given, you may set a custom table name using the setTableName
method.
If the tableContainer
is a text document, cell borders will be created by
default.
tableContainer
- the ODF document that contains this featurenumRows
- the row numbernumCols
- the column numberheaderRowNumber
- the header row numberheaderColumnNumber
- the header column numberTable
public static Table newTable(TableContainer tableContainer, String[] rowLabel, String[] columnLabel, double[][] data)
The table will be inserted at the end of the tableContainer. An unique table name will be
given, you may set a custom table name using the setTableName
method.
If the tableContainer
is a text document, cell borders will be created by
default.
tableContainer
- the table container that contains this tablerowLabel
- set as the header row, it can be null if no header row neededcolumnLabel
- set as the header column, it can be null if no header column neededdata
- the two dimension array of double as the data of this tableTable
public static Table newTable(TableContainer tableContainer, String[] rowLabel, String[] columnLabel, String[][] data)
The table will be inserted at the end of the tableContainer. An unique table name will be
given, you may set a custom table name using the setTableName
method.
If the tableContainer
is a text document, cell borders will be created by
default.
tableContainer
- the table container that contains this tablerowLabel
- set as the header row, it can be null if no header row neededcolumnLabel
- set as the header column, it can be null if no header column neededdata
- the two dimension array of string as the data of this tableTable
public double getWidth()
Throw an UnsupportedOperationException if the table is one sheet of a spreadsheet document. because the sheet doesn't have an attribute of table width.
An UnsupportedOperationException will be thrown if the table is in the spreadsheet document.
public void setWidth(double width)
Throw an UnsupportedOperationException if the table is part of a spreadsheet document that does not allow to change the table size, because spreadsheet is not allow user to set the table size.
width
- the width that need to set (in Millimeter).
An UnsupportedOperationException will be thrown if the table is in the spreadsheet document.
public void setTableAlign(TableAlignAttribute.Value align)
align
- public void applyStyle(TableTemplate template) throws Exception
A table can only be formatted as one type of styles: even-odd-rows or even-odd-columns. The rule is to check the style of odd rows and even rows in the template, only if they have one different properties, table: style-name or table:paragraph-style-name, the table template will be treated as a even-odd-columns styled table.
If one style in the template is null, the style of corresponding cells will be removed. An empty template can be used to remove all the styles in a table.
template
- IllegalArgumentException
- if the given template is nullException
- if content DOM could not be initializedpublic int getRowCount()
public int getColumnCount()
public Row appendRow()
Since SIMPLE supports automatic table expansion. Whenever a cell outside the current table
is addressed the table is instantly expanded. Method getCellByPosition
can
randomly access any cell, no matter it in or out of the table original range.
appendRows(int)
,
getRowByIndex(int)
,
getCellByPosition(int, int)
,
getCellByPosition(String)
public List<Row> appendRows(int rowCount)
Since SIMPLE supports automatic table expansion. Whenever a cell outside the current table
is addressed the table is instantly expanded. Method getCellByPosition
can
randomly access any cell, no matter it in or out of the table original range.
rowCount
- is the number of rows to be appended.appendRow()
,
getRowByIndex(int)
,
getCellByPosition(int, int)
,
getCellByPosition(String)
public Column appendColumn()
Since SIMPLE supports automatic table expansion. Whenever a cell outside the current table
is addressed the table is instantly expanded. Method getCellByPosition
can
randomly access any cell, no matter it in or out of the table original range.
appendColumns(int)
,
getColumnByIndex(int)
,
getCellByPosition(int, int)
,
getCellByPosition(String)
public List<Column> appendColumns(int columnCount)
Since SIMPLE supports automatic table expansion. Whenever a cell outside the current table
is addressed the table is instantly expanded. Method getCellByPosition
can
randomly access any cell, no matter it in or out of the table original range.
columnCount
- is the number of columns to be appended.appendColumn()
,
getColumnByIndex(int)
,
getCellByPosition(int, int)
,
getCellByPosition(String)
public TableTableElement getOdfElement()
TableTableElement
which represents this feature.getOdfElement
in class Component
TableTableElement
public List<Column> insertColumnsBefore(int index, int columnCount)
index
.index
- is the index of the column to insert before.columnCount
- is the number of columns to insert.public void removeColumnsByIndex(int startIndex, int deleteColCount)
index
.startIndex
- is the index of the first column to delete.deleteColCount
- is the number of columns to delete.public List<Row> insertRowsBefore(int index, int rowCount)
index
.index
- is the index of the row to insert before.rowCount
- is the number of rows to insert.public List<Column> getColumnList()
public Iterator<Column> getColumnIterator()
Iterator
public List<Row> getRowList()
public Iterator<Row> getRowIterator()
Iterator
public Column getColumnByIndex(int index)
index
- the zero-based index of the column.public Row getRowByIndex(int index)
index
- the zero-based index of the row.public void removeRowsByIndex(int startIndex, int deleteRowCount)
index
.startIndex
- is the zero-based index of the first row to delete.deleteRowCount
- is the number of rows to delete.public void remove()
public int getHeaderRowCount()
public int getHeaderColumnCount()
public String getTableName()
public void setTableName(String tableName)
tableName
- the table nameIllegalArgumentException
- if the tableName is duplicate with one of tables in the
current documentpublic boolean isProtected()
public void setProtected(boolean isProtected)
isProtected
- the protected attribute of the table to be setpublic boolean isCellStyleInheritance()
The default setting is inherited. In this condition, the style of new column is same with the previous column before the inserted position, while the style of new row is same with the last row before the inserted position.
This feature setting will influence appendRow()
, appendColumn()
,
appendRows()
, appendColumns()
, insertRowsBefore()
and
insertColumnsBefore()
.
For getCellByPosition()
, getCellRangeByPosition()
,
getCellRangeByName()
, getRowByIndex()
and getColumnByIndex()
,
if need automatically expand cells, it will return empty cell(s) without any style settings. So
inheritance setting have no effect on them.
setCellStyleInheritance(boolean)
,
appendColumn()
,
appendColumns(int)
,
appendRow()
,
appendRows(int)
,
insertColumnsBefore(int, int)
,
insertRowsBefore(int, int)
,
getCellByPosition(int, int)
,
getCellByPosition(String)
,
getCellRangeByPosition(int, int, int, int)
,
getCellRangeByPosition(String, String)
,
getCellRangeByName(String)
,
getColumnByIndex(int)
,
getRowByIndex(int)
public void setCellStyleInheritance(boolean isEnabled)
This feature setting will influence appendRow()
, appendColumn()
,
appendRows()
, appendColumns()
, insertRowsBefore()
and
insertColumnsBefore()
.
For getCellByPosition()
, getCellRangeByPosition()
,
getCellRangeByName()
, getRowByIndex()
and getColumnByIndex()
,
if need automatically expand cells, it will return empty cell(s) without any style settings. So
inheritance setting have no effect on them.
isEnabled
- ifisEnabled
is true, cell style will be inherited by new cell.isCellStyleInheritance()
,
appendColumn()
,
appendColumns(int)
,
appendRow()
,
appendRows(int)
,
insertColumnsBefore(int, int)
,
insertRowsBefore(int, int)
,
getCellByPosition(int, int)
,
getCellByPosition(String)
,
getCellRangeByPosition(int, int, int, int)
,
getCellRangeByPosition(String, String)
,
getCellRangeByName(String)
,
getColumnByIndex(int)
,
getRowByIndex(int)
public boolean isUseRepeat()
The default setting is true
, which helps to decrease the document size. If
setting is false
, each column/row/cell will be described by its owned single
element.
This feature setting will influence appendRows()
, appendColumns()
,
insertRowsBefore()
, insertColumnsBefore()
, getCellByPosition()
, getCellRangeByPosition()
, getCellRangeByName()
,
getRowByIndex()
and getColumnByIndex()
.
setUseRepeat(boolean)
,
appendColumns(int)
,
appendRows(int)
,
insertColumnsBefore(int, int)
,
insertRowsBefore(int, int)
,
getCellByPosition(int, int)
,
getCellByPosition(String)
,
getCellRangeByPosition(int, int, int, int)
,
getCellRangeByPosition(String, String)
,
getCellRangeByName(String)
,
getColumnByIndex(int)
,
getRowByIndex(int)
public void setUseRepeat(boolean isSingle)
This method allows users to set whether the new created columns/rows/cells are described by
a single element. Of course, the default setting is true
, which helps to decrease
the document size. If setting is false
, each column/row/cell will be described by
its owned single element.
This feature setting will influence appendRows()
, appendColumns()
,
insertRowsBefore()
, insertColumnsBefore()
, getCellByPosition()
, getCellRangeByPosition()
, getCellRangeByName()
,
getRowByIndex()
and getColumnByIndex()
.
isSingle
- ifisSingle
is true, the new created columns/rows/cells are
described by a single element, if possible.isUseRepeat()
,
appendColumns(int)
,
appendRows(int)
,
insertColumnsBefore(int, int)
,
insertRowsBefore(int, int)
,
getCellByPosition(int, int)
,
getCellByPosition(String)
,
getCellRangeByPosition(int, int, int, int)
,
getCellRangeByPosition(String, String)
,
getCellRangeByName(String)
,
getColumnByIndex(int)
,
getRowByIndex(int)
public CellRange getCellRangeByPosition(int startCol, int startRow, int endCol, int endRow)
startCol
- the column index of the first cell inside the range.startRow
- the row index of the first cell inside the range.endCol
- the column index of the last cell inside the range.endRow
- the row index of the last cell inside the range.public CellRange getCellRangeByPosition(String startAddress, String endAddress)
The cell address is constructed with a table name, a dot (.), an alphabetic value representing the column, and a numeric value representing the row. The table name can be omitted. For example: "$Sheet1.A1", "Sheet1.A1" and "A1" are all valid cell address.
startAddress
- the cell address of the first cell inside the range.endAddress
- the cell address of the last cell inside the range.public CellRange getCellRangeByName(String name)
After you get a cell range with getCellRangeByPosition
, you
can assign a name to this cell range with the method
setCellRangeName
in class
CellRange
. Then you
will get a named range which can be represented by name. This
method can be used to get a named range.
name
- the name of the specified named rangepublic Cell getCellByPosition(int colIndex, int rowIndex)
colIndex
- the column index of the cell.rowIndex
- the row index of the cell.public Cell getCellByPosition(String address)
The cell address is constructed with a table name, a dot (.), an alphabetic value representing the column, and a numeric value representing the row. The table name can be omitted. For example: "$Sheet1.A1", "Sheet1.A1" and "A1" are all valid cell address.
address
- the cell address of the cell.public void setVerticalMargin(double spaceTop, double spaceBottom)
spaceTop
- space above the table in centimeter(cm), ex. 1.25 cmspaceBottom
- spacing below the table in centimeter(cm), ex. 0.7 cmpublic String getKeepWithNext()
public void setKeepWithNext(boolean keepWithNext)
keepWithNext
- public DefaultStyleHandler getStyleHandler()
Copyright © 2010–2018 Apache Software Foundation; Copyright © 2018–2020 The Document Foundation. All rights reserved.