Table class
Table class
Represents a table in a Word document. To learn more, visit the Working with Tables documentation article.
Remarks
Table is a block-level node and can be a child of classes derived from Story or InlineStory.
Table can contain one or more Row nodes.
A minimal valid table needs to have at least one Row.
Inheritance: Table → CompositeNode → Node
Constructors
Name | Description |
---|---|
Table(doc) | Initializes a new instance of the Table class. |
Properties
Name | Description |
---|---|
absoluteHorizontalDistance | Gets or sets absolute horizontal floating table position specified by the table properties, in points. Default value is 0. |
absoluteVerticalDistance | Gets or sets absolute vertical floating table position specified by the table properties, in points. Default value is 0. |
alignment | Specifies how an inline table is aligned in the document. |
allowAutoFit | Allows Microsoft Word and Aspose.Words to automatically resize cells in a table to fit their contents. |
allowCellSpacing | Gets or sets the “Allow spacing between cells” option. |
allowOverlap | Gets whether a floating table shall allow other floating objects in the document to overlap its extents when displayed. Default value is true . |
bidi | Gets or sets whether this is a right-to-left table. |
bottomPadding | Gets or sets the amount of space (in points) to add below the contents of cells. |
cellSpacing | Gets or sets the amount of space (in points) between the cells. |
count | Gets the number of immediate children of this node. (Inherited from CompositeNode) |
customNodeId | Specifies custom node identifier. (Inherited from Node) |
description | Gets or sets description of this table. It provides an alternative text representation of the information contained in the table. |
distanceBottom | Gets or sets distance between table bottom and the surrounding text, in points. |
distanceLeft | Gets or sets distance between table left and the surrounding text, in points. |
distanceRight | Gets or sets distance between table right and the surrounding text, in points. |
distanceTop | Gets or sets distance between table top and the surrounding text, in points. |
document | Gets the document to which this node belongs. (Inherited from Node) |
firstChild | Gets the first child of the node. (Inherited from CompositeNode) |
firstRow | Returns the first Row node in the table. |
hasChildNodes | Returns true if this node has any child nodes.(Inherited from CompositeNode) |
horizontalAnchor | Gets the base object from which the horizontal positioning of floating table should be calculated. Default value is RelativeHorizontalPosition.Column. |
isComposite | Returns true if this node can contain other nodes.(Inherited from Node) |
lastChild | Gets the last child of the node. (Inherited from CompositeNode) |
lastRow | Returns the last Row node in the table. |
leftIndent | Gets or sets the value that represents the left indent of the table. |
leftPadding | Gets or sets the amount of space (in points) to add to the left of the contents of cells. |
nextSibling | Gets the node immediately following this node. (Inherited from Node) |
nodeType | Returns NodeType.Table. |
parentNode | Gets the immediate parent of this node. (Inherited from Node) |
preferredWidth | Gets or sets the table preferred width. |
previousSibling | Gets the node immediately preceding this node. (Inherited from Node) |
range | Returns a Range object that represents the portion of a document that is contained in this node. (Inherited from Node) |
relativeHorizontalAlignment | Gets or sets floating table relative horizontal alignment. |
relativeVerticalAlignment | Gets or sets floating table relative vertical alignment. |
rightPadding | Gets or sets the amount of space (in points) to add to the right of the contents of cells. |
rows | Provides typed access to the rows of the table. |
style | Gets or sets the table style applied to this table. |
styleIdentifier | Gets or sets the locale independent style identifier of the table style applied to this table. |
styleName | Gets or sets the name of the table style applied to this table. |
styleOptions | Gets or sets bit flags that specify how a table style is applied to this table. |
textWrapping | Gets or sets Table.textWrapping for table. |
title | Gets or sets title of this table. It provides an alternative text representation of the information contained in the table. |
topPadding | Gets or sets the amount of space (in points) to add above the contents of cells. |
verticalAnchor | Gets the base object from which the vertical positioning of floating table should be calculated. Default value is RelativeVerticalPosition.Margin. |
Methods
Name | Description |
---|---|
appendChild(newChild) | Adds the specified node to the end of the list of child nodes for this node. (Inherited from CompositeNode) |
asBody() | Cast node to Body. (Inherited from Node) |
asBookmarkEnd() | Cast node to BookmarkEnd. (Inherited from Node) |
asBookmarkStart() | Cast node to BookmarkStart. (Inherited from Node) |
asBuildingBlock() | Cast node to BuildingBlock. (Inherited from Node) |
asCell() | Cast node to Cell. (Inherited from Node) |
asComment() | Cast node to Comment. (Inherited from Node) |
asCommentRangeEnd() | Cast node to CommentRangeEnd. (Inherited from Node) |
asCommentRangeStart() | Cast node to CommentRangeStart. (Inherited from Node) |
asCompositeNode() | Cast node to CompositeNode. (Inherited from Node) |
asDocument() | Cast node to Node.document. (Inherited from Node) |
asEditableRangeEnd() | Cast node to EditableRangeEnd. (Inherited from Node) |
asEditableRangeStart() | Cast node to EditableRangeStart. (Inherited from Node) |
asFieldEnd() | Cast node to FieldEnd. (Inherited from Node) |
asFieldSeparator() | Cast node to FieldSeparator. (Inherited from Node) |
asFieldStart() | Cast node to FieldStart. (Inherited from Node) |
asFootnote() | Cast node to Footnote. (Inherited from Node) |
asFormField() | Cast node to FormField. (Inherited from Node) |
asGlossaryDocument() | Cast node to GlossaryDocument. (Inherited from Node) |
asGroupShape() | Cast node to GroupShape. (Inherited from Node) |
asHeaderFooter() | Cast node to HeaderFooter. (Inherited from Node) |
asOfficeMath() | Cast node to OfficeMath. (Inherited from Node) |
asParagraph() | Cast node to Paragraph. (Inherited from Node) |
asRow() | Cast node to Row. (Inherited from Node) |
asRun() | Cast node to Run. (Inherited from Node) |
asSection() | Cast node to Section. (Inherited from Node) |
asShape() | Cast node to Shape. (Inherited from Node) |
asSmartTag() | Cast node to SmartTag. (Inherited from Node) |
asSpecialChar() | Cast node to SpecialChar. (Inherited from Node) |
asStructuredDocumentTag() | Cast node to StructuredDocumentTag. (Inherited from Node) |
asStructuredDocumentTagRangeEnd() | Cast node to StructuredDocumentTagRangeEnd. (Inherited from Node) |
asStructuredDocumentTagRangeStart() | Cast node to StructuredDocumentTagRangeStart. (Inherited from Node) |
asSubDocument() | Cast node to SubDocument. (Inherited from Node) |
asTable() | Cast node to Table. (Inherited from Node) |
autoFit(behavior) | Resizes the table and cells according to the specified auto fit behavior. |
clearBorders() | Removes all table and cell borders on this table. |
clearShading() | Removes all shading on the table. |
clone(isCloneChildren) | Creates a duplicate of the node. (Inherited from Node) |
convertToHorizontallyMergedCells() | Converts cells horizontally merged by width to cells merged by CellFormat.horizontalMerge. |
ensureMinimum() | If the table has no rows, creates and appends one Row. |
getAncestor(ancestorType) | Gets the first ancestor of the specified NodeType. (Inherited from Node) |
getBuildingBlock(index, isDeep) | Returns an Nth child BuildingBlock node. (Inherited from CompositeNode) |
getChild(nodeType, index, isDeep) | Returns an Nth child node that matches the specified type. (Inherited from CompositeNode) |
getChildNodes(nodeType, isDeep) | Returns a live collection of child nodes that match the specified type. (Inherited from CompositeNode) |
getComment(index, isDeep) | Returns an Nth child Comment node. (Inherited from CompositeNode) |
getEditableRangeStart(index, isDeep) | Returns an Nth child EditableRangeStart node. (Inherited from CompositeNode) |
getFootnote(index, isDeep) | Returns an Nth child Footnote node. (Inherited from CompositeNode) |
getGroupShape(index, isDeep) | Returns an Nth child GroupShape node. (Inherited from CompositeNode) |
getOfficeMath(index, isDeep) | Returns an Nth child OfficeMath node. (Inherited from CompositeNode) |
getParagraph(index, isDeep) | Returns an Nth child Paragraph node. (Inherited from CompositeNode) |
getRun(index, isDeep) | Returns an Nth child Run node. (Inherited from CompositeNode) |
getSdt(index, isDeep) | Returns an Nth child StructuredDocumentTag node. (Inherited from CompositeNode) |
getSdtRangeEnd(index, isDeep) | Returns an Nth child StructuredDocumentTagRangeEnd node. (Inherited from CompositeNode) |
getSdtRangeStart(index, isDeep) | Returns an Nth child StructuredDocumentTagRangeStart node. (Inherited from CompositeNode) |
getShape(index, isDeep) | Returns an Nth child Shape node. (Inherited from CompositeNode) |
getSmartTag(index, isDeep) | Returns an Nth child SmartTag node. (Inherited from CompositeNode) |
getTable(index, isDeep) | Returns an Nth child Table node. (Inherited from CompositeNode) |
getText() | Gets the text of this node and of all its children. (Inherited from Node) |
indexOf(child) | Returns the index of the specified child node in the child node array. (Inherited from CompositeNode) |
insertAfter(newChild, refChild) | Inserts the specified node immediately after the specified reference node. (Inherited from CompositeNode) |
insertBefore(newChild, refChild) | Inserts the specified node immediately before the specified reference node. (Inherited from CompositeNode) |
nextPreOrder(rootNode) | Gets next node according to the pre-order tree traversal algorithm. (Inherited from Node) |
nodeTypeToString(nodeType) | A utility method that converts a node type enum value into a user friendly string. (Inherited from Node) |
prependChild(newChild) | Adds the specified node to the beginning of the list of child nodes for this node. (Inherited from CompositeNode) |
previousPreOrder(rootNode) | Gets the previous node according to the pre-order tree traversal algorithm. (Inherited from Node) |
referenceEquals(other) | (Inherited from Node) |
remove() | Removes itself from the parent. (Inherited from Node) |
removeAllChildren() | Removes all the child nodes of the current node. (Inherited from CompositeNode) |
removeChild(oldChild) | Removes the specified child node. (Inherited from CompositeNode) |
removeSmartTags() | Removes all SmartTag descendant nodes of the current node. (Inherited from CompositeNode) |
selectNodes(xpath) | Selects a list of nodes matching the XPath expression. (Inherited from CompositeNode) |
selectSingleNode(xpath) | Selects the first Node that matches the XPath expression. (Inherited from CompositeNode) |
setBorder(borderType, lineStyle, lineWidth, color, isOverrideCellBorders) | Sets the specified table border to the specified line style, width and color. |
setBorders(lineStyle, lineWidth, color) | Sets all table borders to the specified line style, width and color. |
setShading(texture, foregroundColor, backgroundColor) | Sets shading to the specified values on whole table. |
toString(saveFormat) | Exports the content of the node into a string in the specified format. (Inherited from Node) |
toString(saveOptions) | Exports the content of the node into a string using the specified save options. (Inherited from Node) |
Examples
Shows how to build a formatted 2x2 table.
let doc = new aw.Document();
let builder = new aw.DocumentBuilder(doc);
let table = builder.startTable();
builder.insertCell();
builder.cellFormat.verticalAlignment = aw.Tables.CellVerticalAlignment.Center;
builder.write("Row 1, cell 1.");
builder.insertCell();
builder.write("Row 1, cell 2.");
builder.endRow();
// While building the table, the document builder will apply its current RowFormat/CellFormat property values
// to the current row/cell that its cursor is in and any new rows/cells as it creates them.
expect(table.rows.at(0).cells.at(0).cellFormat.verticalAlignment).toEqual(aw.Tables.CellVerticalAlignment.Center);
expect(table.rows.at(0).cells.at(1).cellFormat.verticalAlignment).toEqual(aw.Tables.CellVerticalAlignment.Center);
builder.insertCell();
builder.rowFormat.height = 100;
builder.rowFormat.heightRule = aw.HeightRule.Exactly;
builder.cellFormat.orientation = aw.TextOrientation.Upward;
builder.write("Row 2, cell 1.");
builder.insertCell();
builder.cellFormat.orientation = aw.TextOrientation.Downward;
builder.write("Row 2, cell 2.");
builder.endRow();
builder.endTable();
// Previously added rows and cells are not retroactively affected by changes to the builder's formatting.
expect(table.rows.at(0).rowFormat.height).toEqual(0);
expect(table.rows.at(0).rowFormat.heightRule).toEqual(aw.HeightRule.Auto);
expect(table.rows.at(1).rowFormat.height).toEqual(100);
expect(table.rows.at(1).rowFormat.heightRule).toEqual(aw.HeightRule.Exactly);
expect(table.rows.at(1).cells.at(0).cellFormat.orientation).toEqual(aw.TextOrientation.Upward);
expect(table.rows.at(1).cells.at(1).cellFormat.orientation).toEqual(aw.TextOrientation.Downward);
doc.save(base.artifactsDir + "DocumentBuilder.BuildTable.docx");
Shows how to create a table.
let doc = new aw.Document();
let table = new aw.Tables.Table(doc);
doc.firstSection.body.appendChild(table);
// Tables contain rows, which contain cells, which may have paragraphs
// with typical elements such as runs, shapes, and even other tables.
// Calling the "EnsureMinimum" method on a table will ensure that
// the table has at least one row, cell, and paragraph.
let firstRow = new aw.Tables.Row(doc);
table.appendChild(firstRow);
let firstCell = new aw.Tables.Cell(doc);
firstRow.appendChild(firstCell);
let paragraph = new aw.Paragraph(doc);
firstCell.appendChild(paragraph);
// Add text to the first cell in the first row of the table.
let run = new aw.Run(doc, "Hello world!");
paragraph.appendChild(run);
doc.save(base.artifactsDir + "Table.CreateTable.docx");
Shows how to iterate through all tables in the document and print the contents of each cell.
let doc = new aw.Document(base.myDir + "Tables.docx");
let tables = doc.firstSection.body.tables;
expect(tables.toArray().length).toEqual(2);
for (let i = 0; i < tables.count; i++)
{
console.log(`Start of Table ${i}`);
let rows = tables.at(i).rows;
for (let j = 0; j < rows.count; j++)
{
console.log(`\tStart of Row ${j}`);
let cells = rows.at(j).cells;
for (let k = 0; k < cells.count; k++)
{
let cellText = cells.at(k).toString(aw.SaveFormat.Text).trim();
console.log(`\t\tContents of Cell:${k} = \"${cellText}\"`);
}
console.log(`\tEnd of Row ${j}`);
}
console.log(`End of Table ${i}\n`);
}
Shows how to build a nested table without using a document builder.
test('CreateNestedTable', () => {
let doc = new aw.Document();
// Create the outer table with three rows and four columns, and then add it to the document.
let outerTable = createTable(doc, 3, 4, "Outer Table");
doc.firstSection.body.appendChild(outerTable);
// Create another table with two rows and two columns and then insert it into the first table's first cell.
let innerTable = createTable(doc, 2, 2, "Inner Table");
outerTable.firstRow.firstCell.appendChild(innerTable);
doc.save(base.artifactsDir + "Table.CreateNestedTable.docx");
});
/// <summary>
/// Creates a new table in the document with the given dimensions and text in each cell.
/// </summary>
function createTable(doc, rowCount, cellCount, cellText)
{
let table = new aw.Tables.Table(doc);
for (let rowId = 1; rowId <= rowCount; rowId++)
{
let row = new aw.Tables.Row(doc);
table.appendChild(row);
for (let cellId = 1; cellId <= cellCount; cellId++)
{
let cell = new aw.Tables.Cell(doc);
cell.appendChild(new aw.Paragraph(doc));
cell.firstParagraph.appendChild(new aw.Run(doc, cellText));
row.appendChild(cell);
}
}
// You can use the "Title" and "Description" properties to add a title and description respectively to your table.
// The table must have at least one row before we can use these properties.
// These properties are meaningful for ISO / IEC 29500 compliant .docx documents (see the OoxmlCompliance class).
// If we save the document to pre-ISO/IEC 29500 formats, Microsoft Word ignores these properties.
table.title = "Aspose table title";
table.description = "Aspose table description";
return table;
}
See Also
- module Aspose.Words
- class CompositeNode