Class OdfTable

java.lang.Object
org.odftoolkit.odfdom.doc.table.OdfTable

public class OdfTable extends Object
OdfTable represents the table feature in ODF spreadsheet and text documents.

OdfTable provides methods to get/add/delete/modify table column/row/cell.

  • Field Details

    • mDocument

      protected OdfDocument mDocument
    • mIsSpreadsheet

      protected boolean mIsSpreadsheet
    • mIsCellStyleInheritance

      protected boolean mIsCellStyleInheritance
  • Method Details

    • getInstance

      public static OdfTable getInstance(TableTableElement odfElement)
      Get a table feature instance by an instance of TableTableElement.
      Parameters:
      odfElement - an instance of TableTableElement
      Returns:
      an instance of OdfTable that can represent odfElement
    • getWidth

      public long getWidth()
      Get the width of the table (in Millimeter).

      Throw an UnsupportedOperationException if the table is one sheet of a spreadsheet document. because the sheet doesn't have an attribute of table width.

      Returns:
      the width of the current table (in Millimeter).

      An UnsupportedOperationException will be thrown if the table is in the spreadsheet document.

    • setWidth

      public void setWidth(long width)
      Set the width of the table (in Millimeter).

      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.

      Parameters:
      width - the width that need to set (in Millimeter).

      An UnsupportedOperationException will be thrown if the table is in the spreadsheet document.

    • newTable

      public static OdfTable newTable(OdfElement tableParent)
      Construct the OdfTable feature. The default column count is 5. The default row count is 2.

      The table will be inserted at the end of the document the parent element belongs to. An unique table name will be given, you may set a custom table name using the setTableName method.

      If the document is a text document, cell borders will be created by default.

      Parameters:
      tableParent - the ODF element the new table will be appended to
      Returns:
      the created OdfTable feature instance
    • newTable

      public static OdfTable newTable(OdfDocument document)
      Construct the OdfTable feature. The default column count is 5. The default row count is 2.

      The table will be inserted at the end of the given document. An unique table name will be given, you may set a custom table name using the setTableName method.

      If the document is a text document, cell borders will be created by default.

      Parameters:
      document - the ODF document that contains this feature
      Returns:
      the created OdfTable feature instance
    • newTable

      public static OdfTable newTable(OdfElement tableParent, int numRows, int numCols)
      Construct the OdfTable feature with a specified row number and column number.

      The table will be inserted at the end of the document. An unique table name will be given, you may set a custom table name using the setTableName method.

      If the document is a text document, cell borders will be created by default.

      Parameters:
      tableParent - the ODF element the new table will be appended to
      numRows - the row number
      numCols - the column number
      Returns:
      a new instance of OdfTable
    • newTable

      public static OdfTable newTable(OdfDocument document, int numRows, int numCols)
      Construct the OdfTable feature with a specified row number and column number.

      The table will be inserted at the end of the document. An unique table name will be given, you may set a custom table name using the setTableName method.

      If the document is a text document, cell borders will be created by default.

      Parameters:
      document - the ODF document that contains this feature
      numRows - the row number
      numCols - the column number
      Returns:
      a new instance of OdfTable
    • newTable

      public static OdfTable newTable(OdfElement tableParent, int numRows, int numCols, int headerRowNumber, int headerColumnNumber)
      Construct the OdfTable feature with a specified row number, column number, header row number, header column number.

      An unique table name will be given, you may set a custom table name using the setTableName method.

      If the document is a text document, cell borders will be created by default.

      Parameters:
      tableParent - the ODF element the new table will be appended to
      numRows - the row number
      numCols - the column number
      headerRowNumber - the header row number
      headerColumnNumber - the header column number
      Returns:
      a new instance of OdfTable
    • newTable

      public static OdfTable newTable(OdfDocument document, int numRows, int numCols, int headerRowNumber, int headerColumnNumber)
      Construct the OdfTable feature with a specified 2 dimension array as the data of this table. The value type of each cell is float.

      The table will be inserted at the end of the document. An unique table name will be given, you may set a custom table name using the setTableName method.

      If the document is a text document, cell borders will be created by default.

      Parameters:
      document - the ODF document that contains this feature
      numRows - the row number
      numCols - the column number
      headerRowNumber - the header row number
      headerColumnNumber - the header column number
      Returns:
      a new instance of OdfTable
    • newTable

      public static OdfTable newTable(OdfElement tableParent, String[] rowLabel, String[] columnLabel, double[][] data)
      Construct the OdfTable feature with a specified 2 dimension array as the data of this table. The value type of each cell is float.

      The table will be inserted at the end of the document. An unique table name will be given, you may set a custom table name using the setTableName method.

      If the document is a text document, cell borders will be created by default.

      Parameters:
      tableParent - the ODF document that contains this feature
      rowLabel - set as the header row, it can be null if no header row needed
      columnLabel - set as the header column, it can be null if no header column needed
      data - the two dimension array of double as the data of this table
      Returns:
      a new instance of OdfTable
    • newTable

      public static OdfTable newTable(OdfDocument document, String[] rowLabel, String[] columnLabel, double[][] data)
      Construct the OdfTable feature with a specified 2 dimension array as the data of this table. The value type of each cell is float.

      The table will be inserted at the end of the document. An unique table name will be given, you may set a custom table name using the setTableName method.

      If the document is a text document, cell borders will be created by default.

      Parameters:
      document - the ODF document that contains this feature
      rowLabel - set as the header row, it can be null if no header row needed
      columnLabel - set as the header column, it can be null if no header column needed
      data - the two dimension array of double as the data of this table
      Returns:
      a new instance of OdfTable
    • newTable

      public static OdfTable newTable(OdfElement tableParent, String[] rowLabel, String[] columnLabel, String[][] data)
      Construct the OdfTable feature with a specified 2 dimension array as the data of this table. The value type of each cell is string.

      The table will be inserted at the end of the document. An unique table name will be given, you may set a custom table name using the setTableName method.

      If the document is a text document, cell borders will be created by default.

      Parameters:
      tableParent - the ODF element the new table will be appended to
      rowLabel - set as the header row, it can be null if no header row needed
      columnLabel - set as the header column, it can be null if no header column needed
      data - the two dimension array of string as the data of this table
      Returns:
      a new instance of OdfTable
    • newTable

      public static OdfTable newTable(OdfDocument document, String[] rowLabel, String[] columnLabel, String[][] data)
      Construct the OdfTable feature with a specified 2 dimension array as the data of this table. The value type of each cell is string.

      The table will be inserted at the end of the document. An unique table name will be given, you may set a custom table name using the setTableName method.

      If the document is a text document, cell borders will be created by default.

      Parameters:
      document - the ODF document that contains this feature
      rowLabel - set as the header row, it can be null if no header row needed
      columnLabel - set as the header column, it can be null if no header column needed
      data - the two dimension array of string as the data of this table
      Returns:
      a new instance of OdfTable
    • getRowCount

      public int getRowCount()
      Get the row count of this table.
      Returns:
      total count of rows
    • getColumnCount

      public int getColumnCount()
      Get the column count of this table.
      Returns:
      total count of columns
    • appendRow

      public OdfTableRow appendRow()
      Append a row to the end of the table. The style of new row is same with the last row in the table.

      Since ODFDOM 8.5 automatic table expansion is supported. 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.

      Returns:
      a new appended row
      See Also:
    • appendRows

      public List<OdfTableRow> appendRows(int rowCount)
      Append a specific number of rows to the end of the table. The style of new rows are same with the last row in the table.

      Since ODFDOM 8.5 automatic table expansion is supported. 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.

      Parameters:
      rowCount - is the number of rows to be appended.
      Returns:
      a list of new appended rows
      See Also:
    • appendColumn

      public OdfTableColumn appendColumn()
      Append a column at the end of the table. The style of new column is same with the last column in the table.

      Since ODFDOM 8.5 automatic table expansion is supported. 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.

      Returns:
      a new appended column
      See Also:
    • appendColumns

      public List<OdfTableColumn> appendColumns(int columnCount)
      Append a specific number of columns to the right of the table. The style of new columns are same with the rightmost column in the table.

      Since ODFDOM 8.5 automatic table expansion is supported. 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.

      Parameters:
      columnCount - is the number of columns to be appended.
      Returns:
      a list of new appended columns
      See Also:
    • getOdfElement

      public TableTableElement getOdfElement()
      Return an instance of TableTableElement which represents this feature.
      Returns:
      an instance of TableTableElement
    • insertColumnsBefore

      public List<OdfTableColumn> insertColumnsBefore(int index, int columnCount)
      Insert a specific number of columns before the column whose index is index.
      Parameters:
      index - is the index of the column to insert before.
      columnCount - is the number of columns to insert.
      Returns:
      a list of new inserted columns
    • removeColumnsByIndex

      public void removeColumnsByIndex(int startIndex, int deleteColCount)
      Remove a specific number of columns, starting from the column at index.
      Parameters:
      startIndex - is the index of the first column to delete.
      deleteColCount - is the number of columns to delete.
    • removeColumnsByIndex

      public void removeColumnsByIndex(int startIndex, int deleteColCount, boolean columnsOnly)
      Remove a specific number of columns, starting from the column at index.
      Parameters:
      startIndex - is the index of the first column to delete.
      deleteColCount - is the number of columns to delete.
    • insertRowsBefore

      public List<OdfTableRow> insertRowsBefore(int index, int rowCount)
      Insert a specific number of rows before the row at index.
      Parameters:
      index - is the index of the row to insert before.
      rowCount - is the number of rows to insert.
      Returns:
      a list of new inserted rows
    • getColumnList

      public List<OdfTableColumn> getColumnList()
      Return a list of columns in the current table.
      Returns:
      a list of table columns
    • getRowList

      public List<OdfTableRow> getRowList()
      Return a list of table rows in the current table.
      Returns:
      a list of table rows
    • getRowElementList

      public List<TableTableRowElement> getRowElementList()
      Return a list of table rows in the current table.
      Returns:
      a list of table rows
    • getColumnByIndex

      public OdfTableColumn getColumnByIndex(int index)
      Get the column at the specified index. The table will be automatically expanded, when the given index is outside of the original table.
      Parameters:
      index - the zero-based index of the column.
      Returns:
      the column at the specified index
    • getRowByIndex

      public OdfTableRow getRowByIndex(int index)
      Get the row at the specified index. The table will be automatically expanded, when the given index is outside of the original table.
      Parameters:
      index - the zero-based index of the row.
      Returns:
      the row at the specified index
    • removeRowsByIndex

      public void removeRowsByIndex(int startIndex, int deleteRowCount)
      Remove the specific number of rows, starting from the row at index.
      Parameters:
      startIndex - is the zero-based index of the first row to delete.
      deleteRowCount - is the number of rows to delete.
    • remove

      public void remove()
      Remove this table from the document
    • getHeaderRowCount

      public int getHeaderRowCount()
      Return the number of header rows in this table.
      Returns:
      the number of header rows.
    • getHeaderColumnCount

      public int getHeaderColumnCount()
      Return the number of header columns in the table.
      Returns:
      the number of header columns.
    • getTableName

      public String getTableName()
      Return the table name.
      Returns:
      the table name
    • setTableName

      public void setTableName(String tableName)
      Set the table name.
      Parameters:
      tableName - the table name
      Throws:
      IllegalArgumentException - if the tableName is duplicate with one of tables in the current document
    • isProtected

      public boolean isProtected()
      Return true if the table is protected.
      Returns:
      true if the table is protected
    • setProtected

      public void setProtected(boolean isProtected)
      Set if the table is protected.
      Parameters:
      isProtected - the protected attribute of the table to be set
    • isCellStyleInheritance

      protected boolean isCellStyleInheritance()
      Return true if cell style is inherited when a new cell is added to the table.

      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.

      Returns:
      true if cell style is inherited when a new cell is added to the table.
      See Also:
    • setCellStyleInheritance

      protected 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. Of course, 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.

      Parameters:
      isEnabled - ifisEnabled is true, cell style will be inherited by new cell.
      See Also:
    • getCellRangeByPosition

      public OdfTableCellRange getCellRangeByPosition(int startCol, int startRow, int endCol, int endRow)
      Return a range of cells within the specified range. The table will be automatically expanded as need.
      Parameters:
      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.
      Returns:
      the specified cell range.
    • getCellRangeByPosition

      public OdfTableCellRange getCellRangeByPosition(String startAddress, String endAddress)
      Return a range of cells within the specified range. The range is specified by the cell address of the first cell and the cell address of the last cell. The table will be automatically expanded as need.

      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.

      Parameters:
      startAddress - the cell address of the first cell inside the range.
      endAddress - the cell address of the last cell inside the range.
      Returns:
      the specified cell range.
    • getCellRangeByName

      public OdfTableCellRange getCellRangeByName(String name)
      Return a range of cells by a specified name.

      After you get a cell range with getCellRangeByPosition, you can assign a name to this cell range with the method setCellRangeName in class OdfTableCellRange. Then you will get a named range which can be represented by name. This method can be used to get a named range.

      Parameters:
      name - the name of the specified named range
      Returns:
      the specified cell range.
    • getCellByPosition

      public OdfTableCell getCellByPosition(int colIndex, int rowIndex)
      Return a single cell that is positioned at the specified column and row. The table will be automatically expanded as need.
      Parameters:
      colIndex - the column index of the cell.
      rowIndex - the row index of the cell.
      Returns:
      the cell at the specified position
    • getCellByPosition

      public OdfTableCell getCellByPosition(String address)
      Return a single cell that is positioned at the specified cell address. The table can be automatically expanded as need.

      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.

      Parameters:
      address - the cell address of the cell.
      Returns:
      the cell at the specified position.